Пример #1
0
        public SCC(Digraph g)
        {
            dg = g;
            Digraph gReverse = g.Reverse();//get the reverse of the orinal graph

            isMarked = new bool[g.V()];

            //we will need do two passes of DFS

            //the first pass is to get the post reverse order of the reverse graph
            DepthFirstOrder dfs = new DepthFirstOrder(gReverse);
            //the reverse order will put the sink vertex of the orginal at the top of the stack returned
            IEnumerable <int> postReverse = dfs.PostReverseOrder();


            foreach (int v in postReverse)
            {
                if (!isMarked[v]) //now we remove one sink at a time, and find the SCC component
                {                 //the number of the SCC is equal to the number of the times we get isMarked[v]==true;
                    DFS(g, v);
                    count++;
                    Console.WriteLine(0);
                }
            }
        }
Пример #2
0
        public TopologicalOrder(EdgeWeightedDiagraph ewg)
        {
            DirectedCycle directedCycle = new DirectedCycle(ewg);

            if (!directedCycle.HasCycle()) // if it's a DAG, then we can continue to get the Topological Order
            {
                DepthFirstOrder dfsOrder = new DepthFirstOrder(ewg);
                topologicalOder = dfsOrder.PostReverseOrder();
            }
        }