Beispiel #1
0
        private void Dfs(Digraph dg, int v)
        {
            preQueue.Enqueue(v);
            marked[v] = true;

            foreach (int w in dg.Adj(v))
            {
                if (!marked[w])
                {
                    Dfs(dg, w);
                }
            }

            postQueue.Enqueue(v);
            reversePost.Push(v);
        }
Beispiel #2
0
        public KosarajuSCC(Digraph g)
        {
            marked = new bool[g.V()];
            id     = new int[g.V()];
            // 以g的 逆后序 遍历g中的节点,然后记录s可达的点
            //原理: { s可达的Vi } 交 { 逆后序中比s早结束的Vj } =》 强连通分量
            DepthFirstOrder order = new DepthFirstOrder(g.Reverse());

            foreach (int s in order.Reverse)
            {
                if (!marked[s])
                {
                    Dfs(g, s);
                    count++;
                }
            }
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            Digraph g = new Digraph(9);

            //g.Visualize()
            g.AddEdge(0, 2);
            g.AddEdge(0, 6);
            g.AddEdge(1, 0);
            g.AddEdge(2, 3);
            g.AddEdge(2, 4);
            g.AddEdge(3, 4);
            g.AddEdge(3, 2);
            g.AddEdge(4, 5);
            g.AddEdge(4, 6);
            g.AddEdge(5, 0);
            g.AddEdge(5, 3);
            g.AddEdge(6, 7);
            g.AddEdge(7, 8);
            g.AddEdge(8, 7);

            var topo = new Topological(g);

            Console.WriteLine("Order: ");
            foreach (int s in topo.Order)
            {
                Console.Write(s + " ");
            }

            Console.WriteLine("\nReverOrder: ");
            DepthFirstOrder order = new DepthFirstOrder(g.Reverse());

            foreach (int s in order.Reverse)
            {
                Console.Write(s + " ");
            }

            KosarajuSCC ks = new KosarajuSCC(g);

            Console.WriteLine($"\nks:{ks.Count()}");
        }
Beispiel #4
0
 public DirectedDFS(Digraph g, int s)
 {
     _marked = new bool[g.V()];
     Dfs(g, s);
 }
Beispiel #5
0
        public Topological(Digraph dg)
        {
            DepthFirstOrder dfo = new DepthFirstOrder(dg);

            _order = dfo.Reverse;
        }