Exemplo n.º 1
0
        public KosarajuSharirSCC(Graph g)
        {
            marked = new bool[g.V];
            id     = new int[g.V];
            var order = new DepthFirstOrder(g);

            foreach (var v in order.ReversePostorder())
            {
                if (!marked[v])
                {
                    DFS(g, v);
                    count++;
                }
            }
        }
Exemplo n.º 2
0
        /**
         * Unit tests the {@code DepthFirstOrder} data type.
         *
         * @param args the command-line arguments
         */
        public static void test()
        {
            string tiny            = $@"
13
22
 4  2
 2  3
 3  2
 6  0
 0  1
 2  0
11 12
12  9
 9 10
 9 11
 7  9
10 12
11  4
 4  3
 3  5
 6  8
 8  6
 5  4
 0  5
 6  4
 6  9
 7  6
";
            Digraph <IntWrapper> G = new Digraph <IntWrapper>(tiny, (string s) => new IntWrapper(int.Parse(s)));

            System.Console.Error.WriteLine(G);

            DepthFirstOrder <IntWrapper, DirectedEdge <IntWrapper> > dfs = new DepthFirstOrder <IntWrapper, DirectedEdge <IntWrapper> >(G);

            System.Console.WriteLine("   v  pre post");
            System.Console.WriteLine("--------------");
            foreach (var v in G.Vertices)
            {
                System.Console.Write("{0} {1} {2}\n", v, dfs.Pre(v), dfs.Post(v));
            }

            System.Console.Write("Preorder:  ");
            foreach (IntWrapper v in dfs.Pre())
            {
                System.Console.Write(v + " ");
            }
            System.Console.WriteLine();

            System.Console.Write("Postorder: ");
            foreach (IntWrapper v in dfs.Post())
            {
                System.Console.Write(v + " ");
            }
            System.Console.WriteLine();

            System.Console.Write("Reverse postorder: ");
            foreach (IntWrapper v in dfs.reversePost())
            {
                System.Console.Write(v + " ");
            }
            System.Console.WriteLine();
        }