public static bool IsRoutePresentBFS(DirectedGaph grph, int source, int destination)
        {
            if (source == destination)
            {
                return(true);
            }
            grph.visited[source] = true;
            Queue <int> verticesQueue = new Queue <int>();

            verticesQueue.Enqueue(source);
            while (verticesQueue.Count > 0)
            {
                int vertice = verticesQueue.Dequeue();
                foreach (int adjVertice in grph.edges[vertice])
                {
                    if (adjVertice == destination)
                    {
                        return(true);
                    }
                    if (!grph.visited[adjVertice])
                    {
                        grph.visited[adjVertice] = true;
                        verticesQueue.Enqueue(adjVertice);
                    }
                }
            }
            return(false);
        }
Beispiel #2
0
        public static void BuildOrderMain()
        {
            DirectedGaph graph = new DirectedGaph(6);

            graph.AddEdge(0, 3);
            graph.AddEdge(5, 1);
            graph.AddEdge(5, 0);
            graph.AddEdge(1, 3);
            graph.AddEdge(3, 2);
            //graph.AddEdge(3, 0);
            GetBuildOrder(graph);
        }
Beispiel #3
0
        public static Stack <int> GetBuildOrder(DirectedGaph graph)
        {
            HashSet <int> addedToBuild = new HashSet <int>(); // Store which are added to build order
            Stack <int>   buildOrder   = new Stack <int>();   // Store what is the build order

            for (int i = 0; i < graph.edges.Length; i++)
            {                                  //GO DFS for all edges
                if (!addedToBuild.Contains(i)) // If not already added to build then DFS
                {
                    DFS(graph, i, buildOrder, addedToBuild, new HashSet <int>());
                }
            }
            return(buildOrder);
        }
        public static void RouteBetweenTwoNodesMain()
        {
            DirectedGaph grph = new DirectedGaph(7);

            grph.AddEdge(0, 1);
            grph.AddEdge(1, 2);
            grph.AddEdge(2, 0);
            grph.AddEdge(2, 3);
            grph.AddEdge(3, 2);
            grph.AddEdge(4, 6);
            grph.AddEdge(5, 4);
            grph.AddEdge(6, 5);

            //bool isRoute = IsRoutePresentDFS(grph, 0, 3);
            bool isRoute = IsRoutePresentBFS(grph, 0, 5);
        }
        public static bool IsRoutePresentDFS(DirectedGaph grph, int source, int destination)
        {
            grph.visited[source] = true;
            if (source == destination) // Reached Destination node
            {
                return(true);
            }
            foreach (int vertice in grph.edges[source])
            {
                if (!grph.visited[vertice])
                {
                    return(IsRoutePresentDFS(grph, vertice, destination));
                }
            }

            return(false);
        }
Beispiel #6
0
 public static void DFS(DirectedGaph graph, int position, Stack <int> buildOrder, HashSet <int> addedToBuild, HashSet <int> visiting)
 {
     if (visiting.Contains(position))
     {//If visited in this cycle then its Cyclic
         throw new Exception("Cyclic Dependency");
     }
     else
     {//Add to check for cycles
         visiting.Add(position);
     }
     foreach (int dependent in graph.edges[position])
     {//GO DFS for all dependents
         if (!addedToBuild.Contains(dependent))
         {
             DFS(graph, dependent, buildOrder, addedToBuild, visiting);
         }
     }
     buildOrder.Push(position);
     addedToBuild.Add(position);
 }