Computer Science 245: Data Structures and Algorithms

Homework 9: BFS and DFS (due 4/21/2017)


For homework 9, you will need to implement the following three functions:
  1. (10 points) public static void BFS(Graph G, int Parent[], int startVertex, boolean Visited[])

    A standard Breadth-First Search, starting at the given startVertex. The BFS search tree is created in the Parent[] array, using the same parent pointer representation as disjoint sets (that is, Parent[x] = index of the parent of x, or -1 if x has no parent). You can assume that all elements of the array Visited[] are false, and all elements of the array Parent[] are -1 when the function is first called. Feel free to use the one of the queue classes discussed in lecture: ListQueue.java, ArrayQueue.java

  2. (10 points) public static void DFS(Graph G, int Parent[], int startVertex, boolean Visited[])

    A standard Depth-First Search, starting from startVertex. Note that we are only running one DFS from the start vertex, so some vertices may not be visited, if there are vertices not reachable from the start vertex. The DFS search tree is created in the Parent[] array, using the same parent pointer representation as disjoint sets (that is, Parent[x] = index of the parent of x, or -1 if x has no parent). You can assume that all elements of the array Visited[] are false, and all elements of the array Parent[] are -1 when the function is first called.

  3. (10 points) public static void PrintPath(int Parent[], int endvertex)

    Prints out the path from the root of the tree to endvertex, all on one line, followed by an end-of-line. If there is no path from the root of the tree to the end vertex (or if the root vertex is passed in), just print out the end vertex, followed by a end-of-line. Recursion is your friend here. You may need to use a helper function to get the end-of-line printed out correctly.
You are provided with the following files:
All you need to do is download the above three files, and then fill in the bodies of BFS, DFS, and PrintPath in Graphtest.java (as well as adding any helper functions that you might need)
What to turn in: