Exemplo n.º 1
0
        /// <summary>
        /// depth first search preorder and postorder in a digraph
        /// </summary>
        /// <param name="g"></param>
        public DepthFirstOrder(Digraph g)
        {
            pre       = new int[g.V];
            post      = new int[g.V];
            postorder = new Queue <int>();
            preorder  = new Queue <int>();
            marked    = new bool[g.V];

            for (int v = 0; v < g.V; v++)
            {
                if (!marked[v])
                {
                    dfs(g, v);
                }
            }
        }
Exemplo n.º 2
0
        public KosarajuSharirSCC(Digraph g)
        {
            //compute reverse postorder of reverse graph
            DepthFirstOrder dfo = new DepthFirstOrder(g.Reverse());

            //run DFS on g, using reverse postorder to guide calculation
            marked = new bool[g.V];
            id     = new int[g.V];

            foreach (int v in dfo.ReversePost())
            {
                if (!marked[v])
                {
                    dfs(g, v);
                    count++;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// run DFS in digraph G from vertex v and compute preorder/postorder
        /// </summary>
        /// <param name="g"></param>
        /// <param name="v"></param>
        private void dfs(Digraph g, int v)
        {
            if (marked[v])
            {
                return;
            }
            marked[v] = true;
            pre[v]    = preCounter++;
            preorder.Enqueue(v);

            foreach (int w in g.Adj(v))
            {
                if (!marked[v])
                {
                    dfs(g, w);
                }
            }
            postorder.Enqueue(v);
            post[v] = postCounter++;
        }
 public DirectedDFS(Digraph g, int s)
 {
     marked = new bool[g.V];
     dfs(g, s);
 }