コード例 #1
0
        public Topological(EdgeWeightedDigraph ewd)
        {
            EdgeWeightedDirectedCycle edgeWeightedDirectedCycle = new EdgeWeightedDirectedCycle(ewd);

            if (!edgeWeightedDirectedCycle.hasCycle())
            {
                DepthFirstOrder depthFirstOrder = new DepthFirstOrder(ewd);
                this.order = depthFirstOrder.reversePost();
            }
        }
コード例 #2
0
ファイル: Topological.cs プロジェクト: kismy/Algorithms
    public Topological(EdgeWeightedDigraph G)
    {
        EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(G);

        if (!finder.hasCycle())
        {
            DepthFirstOrder dfs = new DepthFirstOrder(G);
            order = dfs.reversePost();
        }
    }
コード例 #3
0
    /**/ public static void main(string[] strarr)
    {
        int num  = Integer.parseInt(strarr[0]);
        int num2 = Integer.parseInt(strarr[1]);
        int num3 = Integer.parseInt(strarr[2]);
        EdgeWeightedDigraph edgeWeightedDigraph = new EdgeWeightedDigraph(num);

        int[] array = new int[num];
        for (int i = 0; i < num; i++)
        {
            array[i] = i;
        }
        StdRandom.shuffle(array);
        for (int i = 0; i < num2; i++)
        {
            int num4;
            int num5;
            do
            {
                num4 = StdRandom.uniform(num);
                num5 = StdRandom.uniform(num);
            }while (num4 >= num5);
            double d = java.lang.Math.random();
            edgeWeightedDigraph.addEdge(new DirectedEdge(num4, num5, d));
        }
        for (int i = 0; i < num3; i++)
        {
            int    num4 = ByteCodeHelper.d2i(java.lang.Math.random() * (double)num);
            int    num5 = ByteCodeHelper.d2i(java.lang.Math.random() * (double)num);
            double d    = java.lang.Math.random();
            edgeWeightedDigraph.addEdge(new DirectedEdge(num4, num5, d));
        }
        StdOut.println(edgeWeightedDigraph);
        EdgeWeightedDirectedCycle edgeWeightedDirectedCycle = new EdgeWeightedDirectedCycle(edgeWeightedDigraph);

        if (edgeWeightedDirectedCycle.hasCycle())
        {
            StdOut.print("Cycle: ");
            Iterator iterator = edgeWeightedDirectedCycle.cycle().iterator();
            while (iterator.hasNext())
            {
                DirectedEdge obj = (DirectedEdge)iterator.next();
                StdOut.print(new StringBuilder().append(obj).append(" ").toString());
            }
            StdOut.println();
        }
        else
        {
            StdOut.println("No directed cycle");
        }
    }