static void Main(string[] args) { TextReader reader = File.OpenText("C:\\Users\\Saumya\\Downloads\\Project3-SS\\Tarjan\\GraphInput.txt"); int vertexCount = int.Parse(reader.ReadLine()); DepGraph graph = DepGraph.CreateGraph(vertexCount); for (int i = 0; i < vertexCount; i++) { String[] edges = reader.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); for (int j = 0; j < edges.Length - 1; j++) { int d = int.Parse(edges[j]); graph.Vertices[i].Dependencies.Add(graph.Vertices[d - 1]); } } TarjanCycleDetect tarjan = new TarjanCycleDetect(); List <List <Vertex> > nodes = tarjan.DetectCycle(graph); nodes[2].ForEach(Console.WriteLine); foreach (object elem in nodes) { Console.WriteLine(elem); } Console.WriteLine(nodes); Console.ReadLine(); }
public static DepGraph CreateGraph(int nodeCount) { DepGraph graph = new DepGraph(); for (int i = 1; i <= nodeCount; i++) { Vertex v = new Vertex(); v.ID = i; graph.Vertices.Add(v); } return(graph); }
public List <List <Vertex> > DetectCycle(DepGraph g) { output = new List <List <Vertex> >(); index = 0; nodeStatck = new Stack <Vertex>(); foreach (Vertex v in g.Vertices) { if (v.index < 0) { StrongConnect(v); } } return(output); }