Lecture Videos
  1  public class TestBFS {
  2    public static void main(String[] args) {
  3      String[] vertices = {"Seattle", "San Francisco", "Los Angeles",
  4        "Denver", "Kansas City", "Chicago", "Boston", "New York",
  5        "Atlanta", "Miami", "Dallas", "Houston"};
  6  
  7      int[][] edges = {
  8        {0, 1}, {0, 3}, {0, 5},
  9        {1, 0}, {1, 2}, {1, 3},
 10        {2, 1}, {2, 3}, {2, 4}, {2, 10},
 11        {3, 0}, {3, 1}, {3, 2}, {3, 4}, {3, 5},
 12        {4, 2}, {4, 3}, {4, 5}, {4, 7}, {4, 8}, {4, 10},
 13        {5, 0}, {5, 3}, {5, 4}, {5, 6}, {5, 7},
 14        {6, 5}, {6, 7},
 15        {7, 4}, {7, 5}, {7, 6}, {7, 8},
 16        {8, 4}, {8, 7}, {8, 9}, {8, 10}, {8, 11},
 17        {9, 8}, {9, 11},
 18        {10, 2}, {10, 4}, {10, 8}, {10, 11},
 19        {11, 8}, {11, 9}, {11, 10}
 20      };
 21  
 22      Graph<String> graph = new UnweightedGraph<>(vertices, edges);
 23      UnweightedGraph<String>.SearchTree bfs = 
 24        graph.bfs(graph.getIndex("Chicago")); // Get a dfs starting at Chicago
 25  
 26      java.util.List<Integer> searchOrders = bfs.getSearchOrder();
 27      System.out.println(bfs.getNumberOfVerticesFound() +
 28        " vertices are searched in this order:");
 29      for (int i = 0; i < searchOrders.size(); i++)
 30        System.out.println(graph.getVertex(searchOrders.get(i)));
 31  
 32      for (int i = 0; i < searchOrders.size(); i++)
 33        if (bfs.getParent(i) != -1)
 34          System.out.println("parent of " + graph.getVertex(i) + 
 35            " is " + graph.getVertex(bfs.getParent(i)));
 36    }
 37  }