public void CycleTests_Directed_StronglyConnected_DFS() { var graph = new Graph(4); graph.AddEdge(0, 1); graph.AddEdge(0, 2); graph.AddEdge(1, 2); graph.AddEdge(2, 0); graph.AddEdge(2, 3); graph.AddEdge(3, 3); var result = DirectedGraphCycle.DoesCycleExistsUsingDFS(graph); Assert.IsTrue(result); if (result) { Console.WriteLine("Graph contains cycle"); } else { Console.WriteLine("Graph doesn't " + "contain cycle"); } var g = new Graph(6); g.AddEdge(0, 1); g.AddEdge(0, 2); g.AddEdge(0, 3); g.AddEdge(3, 4); g.AddEdge(4, 5); g.AddEdge(3, 5); result = DirectedGraphCycle.DoesCycleExistsUsingDFS(graph); Assert.IsTrue(result); if (result) { Console.WriteLine("Graph contains cycle"); } else { Console.WriteLine("Graph doesn't " + "contain cycle"); } }
public void CycleTests_Directed_WeaklyConnected_BFS() { // No cycle var g = new Graph(6); g.AddEdge(0, 1); g.AddEdge(0, 2); g.AddEdge(3, 4); g.AddEdge(4, 5); g.AddEdge(3, 5); if (DirectedGraphCycle.DoesCycleExistsUsingBFS(g)) { Console.WriteLine("Graph contains cycle"); } else { Console.WriteLine("Graph doesn't " + "contain cycle"); } // Yes cycle g = new Graph(6); g.AddEdge(0, 1); g.AddEdge(0, 2); g.AddEdge(3, 4); g.AddEdge(4, 5); g.AddEdge(5, 3); if (DirectedGraphCycle.DoesCycleExistsUsingBFS(g)) { Console.WriteLine("Graph contains cycle"); } else { Console.WriteLine("Graph doesn't " + "contain cycle"); } }
public void CycleTests_Directed_StronglyConnected_BFS() { var graph = new Graph(4); graph.AddEdge(0, 1); graph.AddEdge(0, 2); graph.AddEdge(1, 2); graph.AddEdge(2, 0); graph.AddEdge(2, 3); graph.AddEdge(3, 3); if (DirectedGraphCycle.DoesCycleExistsUsingBFS(graph)) { Console.WriteLine("Graph contains cycle"); } else { Console.WriteLine("Graph doesn't " + "contain cycle"); } var g = new Graph(6); g.AddEdge(0, 1); g.AddEdge(0, 2); g.AddEdge(0, 3); g.AddEdge(3, 4); g.AddEdge(4, 5); g.AddEdge(3, 5); if (DirectedGraphCycle.DoesCycleExistsUsingBFS(g)) { Console.WriteLine("Graph contains cycle"); } else { Console.WriteLine("Graph doesn't " + "contain cycle"); } }