Solution: Detect a Cycle in a Graph
Learn how to detect a cycle in a graph.
We'll cover the following...
Solution
class Program{/// <summary>/// Detects a cycle in a given graph./// </summary>/// <param name="graph">The graph.</param>/// <returns>True if there is a cycle in the given graph, otherwise False.</returns>public static bool DetectCycle(Graph graph){// visited list to keep track of the nodes that have been visited since the beginning of the algorithmbool[] visited = new bool[graph.V];// stack keeps track of the nodes which are part of the current recursive callbool[] myStack = new bool[graph.V];for (int node = 0; node < graph.V; node++){// DFSif (DetectCycleRecursive(graph, node, visited, myStack)){return true;}}return false;}/// <summary>/// Detects a cycle in a given graph./// </summary>/// <param name="graph">The graph.</param>/// <param name="node">A source vertex.</param>/// <param name="visited">A list to track visited nodes.</param>/// <param name="stack">A helper stack.</param>/// <returns>True if there is a cycle in the given graph, otherwise False.</returns>static bool DetectCycleRecursive(Graph graph, int node, bool[] visited, bool[] myStack){// Node was already in recursion stack. Cycle found.if (myStack[node]){return true;}// It has been visited before this recursionif (visited[node]){return false;}// Mark current node as visited and// add to recursion stackvisited[node] = true;myStack[node] = true;AdjNode head = graph.graph[node];while (head != null){// Pick adjacent node and call it recursivelyint adjacent = head.Vertex;// If the node is visited again in the same recursion => Cycle foundif (DetectCycleRecursive(graph, adjacent, visited, myStack)){return true;}head = head.Next;}// remove the node from the recursive callmyStack[node] = false;return false;}// Main program to test the above codepublic static void Main(string[] args){Graph g1 = new Graph(4);g1.AddEdge(0, 1);g1.AddEdge(1, 2);g1.AddEdge(1, 3);g1.AddEdge(3, 0);Graph g2 = new Graph(3);g2.AddEdge(0, 1);g2.AddEdge(1, 2);Console.WriteLine(DetectCycle(g1)); // Output: TrueConsole.WriteLine(DetectCycle(g2)); // Output: False}}
Explanation
The solution might look confusing at ...