Example #1
0
    void Start()
    {
        Digraph G = new Digraph(txt);

        int s = 2;    // 1、2、6
        DepthFirstDirectedPaths dfs = new DepthFirstDirectedPaths(G, s);

        for (int v = 0; v < G.V(); v++)
        {
            if (dfs.hasPathTo(v))
            {
                string str = (s + " to " + v + " :  ");

                foreach (int x in dfs.pathTo(v))
                {
                    if (x == s)
                    {
                        str += x;
                    }
                    else
                    {
                        str += ("-" + x);
                    }
                }
                print(str);
            }

            else
            {
                print(s + " to " + v + " :  not connected\n");
            }
        }
    }
Example #2
0
    /**/ public static void main(string[] strarr)
    {
        In      i       = new In(strarr[0]);
        Digraph digraph = new Digraph(i);
        int     num     = Integer.parseInt(strarr[1]);
        DepthFirstDirectedPaths depthFirstDirectedPaths = new DepthFirstDirectedPaths(digraph, num);

        for (int j = 0; j < digraph.V(); j++)
        {
            if (depthFirstDirectedPaths.hasPathTo(j))
            {
                StdOut.printf("%d to %d:  ", new object[]
                {
                    Integer.valueOf(num),
                    Integer.valueOf(j)
                });
                Iterator iterator = depthFirstDirectedPaths.pathTo(j).iterator();
                while (iterator.hasNext())
                {
                    int num2 = ((Integer)iterator.next()).intValue();
                    if (num2 == num)
                    {
                        StdOut.print(num2);
                    }
                    else
                    {
                        StdOut.print(new StringBuilder().append("-").append(num2).toString());
                    }
                }
                StdOut.println();
            }
            else
            {
                StdOut.printf("%d to %d:  not connected\n", new object[]
                {
                    Integer.valueOf(num),
                    Integer.valueOf(j)
                });
            }
        }
    }