Esempio n. 1
0
    public Topological(EdgeWeightedDigraph G)
    {
        EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(G);

        if (!finder.hasCycle())
        {
            DepthFirstOrder dfs = new DepthFirstOrder(G);
            order = dfs.reversePost();
        }
    }
        public Topological(Digraph d)
        {
            DirectedCycle directedCycle = new DirectedCycle(d);

            if (!directedCycle.hasCycle())
            {
                DepthFirstOrder depthFirstOrder = new DepthFirstOrder(d);
                this.order = depthFirstOrder.reversePost();
            }
        }
        public Topological(EdgeWeightedDigraph ewd)
        {
            EdgeWeightedDirectedCycle edgeWeightedDirectedCycle = new EdgeWeightedDirectedCycle(ewd);

            if (!edgeWeightedDirectedCycle.hasCycle())
            {
                DepthFirstOrder depthFirstOrder = new DepthFirstOrder(ewd);
                this.order = depthFirstOrder.reversePost();
            }
        }
Esempio n. 4
0
    private int[] rank;         // rank[v] = position of vertex v in topological order


    public Topological(Digraph G)
    {
        DirectedCycle finder = new DirectedCycle(G);

        if (!finder.hasCycle())
        {
            DepthFirstOrder dfs = new DepthFirstOrder(G);
            order = dfs.reversePost();
            rank  = new int[G.V()];
            int i = 0;
            foreach (int v in order)
            {
                rank[v] = i++;
            }
        }
    }
Esempio n. 5
0
    private int count;         // number of strongly-connected components

    public KosarajuSharirSCC(Digraph G)
    {
        // compute reverse postorder of reverse graph
        DepthFirstOrder dfs = 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 dfs.reversePost())
        {
            if (!marked[v])
            {
                DFS(G, v);
                count++;
            }
        }
    }
    /**/ public static void main(string[] strarr)
    {
        In              i               = new In(strarr[0]);
        Digraph         digraph         = new Digraph(i);
        DepthFirstOrder depthFirstOrder = new DepthFirstOrder(digraph);

        StdOut.println("   v  pre post");
        StdOut.println("--------------");
        for (int j = 0; j < digraph.V(); j++)
        {
            StdOut.printf("%4d %4d %4d\n", new object[]
            {
                Integer.valueOf(j),
                Integer.valueOf(depthFirstOrder.pre(j)),
                Integer.valueOf(depthFirstOrder.post(j))
            });
        }
        StdOut.print("Preorder:  ");
        Iterator iterator = depthFirstOrder.pre().iterator();

        while (iterator.hasNext())
        {
            int i2 = ((Integer)iterator.next()).intValue();
            StdOut.print(new StringBuilder().append(i2).append(" ").toString());
        }
        StdOut.println();
        StdOut.print("Postorder: ");
        iterator = depthFirstOrder.post().iterator();
        while (iterator.hasNext())
        {
            int i2 = ((Integer)iterator.next()).intValue();
            StdOut.print(new StringBuilder().append(i2).append(" ").toString());
        }
        StdOut.println();
        StdOut.print("Reverse postorder: ");
        iterator = depthFirstOrder.reversePost().iterator();
        while (iterator.hasNext())
        {
            int i2 = ((Integer)iterator.next()).intValue();
            StdOut.print(new StringBuilder().append(i2).append(" ").toString());
        }
        StdOut.println();
    }
Esempio n. 7
0
    void Start()
    {
        Digraph G = new Digraph(txt);

        DepthFirstOrder dfs = new DepthFirstOrder(G);

        print("   v  pre post");
        print("--------------");
        string str = null;

        for (int v = 0; v < G.V(); v++)
        {
            str += (v + " " + dfs.Pre(v) + " " + dfs.Post(v) + "\n");
        }
        print(str);

        str = "Preorder:  ";

        foreach (int v in dfs.Pre())
        {
            str += (v + " ");
        }
        print(str);

        str = "Postorder: ";

        foreach (int v in dfs.Post())
        {
            str += (v + " ");
        }
        print(str);

        str = "Reverse postorder: ";

        foreach (int v in dfs.reversePost())
        {
            str += (v + " ");
        }
        print(str);
    }
    public KosarajuSharirSCC(Digraph d)
    {
        DepthFirstOrder depthFirstOrder = new DepthFirstOrder(d.reverse());

        this.marked = new bool[d.V()];
        this.id     = new int[d.V()];
        Iterator iterator = depthFirstOrder.reversePost().iterator();

        while (iterator.hasNext())
        {
            int num = ((Integer)iterator.next()).intValue();
            if (!this.marked[num])
            {
                this.dfs(d, num);
                this.count++;
            }
        }
        if (!KosarajuSharirSCC.s_assertionsDisabled && !this.check(d))
        {
            throw new AssertionError();
        }
    }