Exemplo n.º 1
0
    void Start()
    {
        Digraph G = new Digraph(txt);

        int s = 2; //1  2  6
        BreadthFirstDirectedPaths bfs = new BreadthFirstDirectedPaths(G, s);

        for (int v = 0; v < G.V(); v++)
        {
            if (bfs.hasPathTo(v))
            {
                string str = (s + " to " + v + "\t(distance=" + bfs.DistTo(v) + "):\t");

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

            else
            {
                print(s + " to " + v + ":  not connected\n");
            }
        }
    }
Exemplo n.º 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]);
        BreadthFirstDirectedPaths breadthFirstDirectedPaths = new BreadthFirstDirectedPaths(digraph, num);

        for (int j = 0; j < digraph.V(); j++)
        {
            if (breadthFirstDirectedPaths.hasPathTo(j))
            {
                StdOut.printf("%d to %d (%d):  ", new object[]
                {
                    Integer.valueOf(num),
                    Integer.valueOf(j),
                    Integer.valueOf(breadthFirstDirectedPaths.distTo(j))
                });
                Iterator iterator = breadthFirstDirectedPaths.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)
                });
            }
        }
    }