예제 #1
0
        internal void GetTopologicalSort()
        {
            Queues.DoublyLinkedListGenericQueue <int> queue = new Queues.DoublyLinkedListGenericQueue <int>();
            //bool ZeroIndegNodePresent = false;
            //int indegNodeIndex = -1;
            int[]  indegreesForNodes = new int[adjMatrix.Length];
            bool[] nodesAddedToQueue = new bool[adjMatrix.Length];
            //WIP
            for (int i = 0; i < adjMatrix.Length; i++)
            {
                int indeg = GetIndegreeOfNode(i, adjMatrix);
                indegreesForNodes[i] = indeg;
                if (indeg == 0)
                {
                    queue.push(i);
                    nodesAddedToQueue[i] = true;
                }
            }

            while (queue.peek() != null)
            {
                int NodeIndex = (int)queue.pop();
                Console.Write(((char)('A' + NodeIndex)) + "\t");
                //Getting neighbours of the node
                for (int i = 0; i < adjMatrix[NodeIndex].Length; i++)
                {
                    if (adjMatrix[NodeIndex][i] > 0)
                    {
                        indegreesForNodes[i]--;
                        if (indegreesForNodes[i] == 0 && !nodesAddedToQueue[i])
                        {
                            queue.push(i);
                            nodesAddedToQueue[i] = true;
                        }
                    }
                }
            }
        }
예제 #2
0
        private static void BreadthFirstSearch(int[][] adjMatrix, int n, int origin, int dest)
        {
            bool[] visited         = new bool[n];
            char   lastvisitedNode = '0';

            Dictionary <char, char> nodeOriginMapping = new Dictionary <char, char>();

            //Introduced the below array for avoiding Dictionary
            int[] originArray = new int[n];
            for (int z = 0; z < n; z++)
            {
                originArray[z] = -1;
            }

            Queues.DoublyLinkedListGenericQueue <int> queue = new Queues.DoublyLinkedListGenericQueue <int>();
            int i = origin;

            while (true)
            {
                for (int j = 0; j < n; j++)
                {
                    if (adjMatrix[i][j] != 0)
                    {
                        if (visited[j] != true)
                        {
                            if (!nodeOriginMapping.ContainsKey((char)(j + 65)))
                            {
                                nodeOriginMapping.Add((char)(j + 65), (char)(i + 65));
                                originArray[j] = i;
                                visited[j]     = true;
                                queue.push(j);
                                visited[i]      = true;
                                lastvisitedNode = (char)(j + 65);
                            }
                            else
                            {
                                Console.WriteLine("Unexpected Code block BreadthFirstSearch 1");
                            }
                        }
                    }
                }

                if (queue.peek() != null)
                {
                    i = (int)queue.pop();
                }
                else
                {
                    break;
                }
            }


            //while (nodeOriginMapping.ContainsKey(lastvisitedNode))
            //{
            //    Console.Write(lastvisitedNode + "<- ");
            //    lastvisitedNode = nodeOriginMapping[lastvisitedNode];
            //}
            while (originArray[(int)lastvisitedNode - 65] != -1)
            {
                Console.Write(lastvisitedNode + "<- ");
                lastvisitedNode = (char)(originArray[(int)lastvisitedNode - 65] + 65);
            }
            Console.Write((char)(origin + 65));
        }