Ejemplo n.º 1
0
        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();
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
 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);
 }