public List <List <Vertex <int> > > SimpleCycles(Graph <int> graph) { blockedSet = new HashSet <Vertex <int> >(); blockedMap = new Dictionary <Vertex <int>, HashSet <Vertex <int> > >(); stack = new Stack <Vertex <int> >(); allCycles = new List <List <Vertex <int> > >(); long startIndex = 1; TarjanStronglyConnectedComponent tarjan = new TarjanStronglyConnectedComponent(); while (startIndex <= graph.GetAllVertex().Count()) { Graph <int> subGraph = createSubGraph(startIndex, graph); List <HashSet <Vertex <int> > > sccs = tarjan.scc(subGraph); Vertex <int> maybeLeastVertex = leastIndexSCC(sccs, subGraph); if (maybeLeastVertex != null) { Vertex <int> leastVertex = maybeLeastVertex; blockedSet.Clear(); blockedMap.Clear(); findCyclesInSCG(leastVertex, leastVertex); startIndex = leastVertex.GetId() + 1; } else { break; } } return(allCycles); }
public static void Test(string[] args) { Graph <int> graph = new Graph <int>(true); graph.addEdge(1, 2); graph.addEdge(2, 3); graph.addEdge(3, 1); graph.addEdge(3, 4); graph.addEdge(4, 5); graph.addEdge(5, 6); graph.addEdge(6, 4); graph.addEdge(7, 6); graph.addEdge(7, 8); graph.addEdge(8, 7); TarjanStronglyConnectedComponent tarjanStronglyConnectedComponent = new TarjanStronglyConnectedComponent(); List <HashSet <Vertex <int> > > result = tarjanStronglyConnectedComponent.scc(graph); result.ForEach(scc => { foreach (var vertex in scc) { Console.Write(vertex + " "); } Console.WriteLine(); }); }