Example #1
0
  public static void main(String[] args) {
    In in = new In(args[0]);
    Graph G = new Graph(in);
    int s = Integer.parseInt(args[1]);
    BreadthFirstSearch search = new BreadthFirstSearch(G, s);
    for (int v = 0; v < G.vertices(); v++) {
      StdOut.print(s + " to  " + v + ": ");

      if (search.hasPathTo(v))
        for (int x : search.pathTo(v))
          if (x == s) StdOut.print(x);
          else StdOut.print("-" + x);
      StdOut.println();
    }
  }
  public static void main(String[] args) {
    // 0 1 2 3 4 5 6 7 8
    // ===================================================
    int[][] conn = {
      {0, 1, 0, 1, 0, 0, 0, 0, 1}, // 0
      {1, 0, 0, 0, 0, 0, 0, 1, 0}, // 1
      {0, 0, 0, 1, 0, 1, 0, 1, 0}, // 2
      {1, 0, 1, 0, 1, 0, 0, 0, 0}, // 3
      {0, 0, 0, 1, 0, 0, 0, 0, 1}, // 4
      {0, 0, 1, 0, 0, 0, 1, 0, 0}, // 5
      {0, 0, 0, 0, 0, 1, 0, 0, 0}, // 6
      {0, 1, 1, 0, 0, 0, 0, 0, 0}, // 7
      {1, 0, 0, 0, 1, 0, 0, 0, 0}
    }; // 8

    BreadthFirstSearch G = new BreadthFirstSearch(conn);

    G.bfs();
  }