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

For homework 9, you will need to implement the following three functions:

- (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

- (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.

- (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.

- Graph.java A simple adjacency list implementation of a graph
- Edge.java A simple edge class, used by Graph.java
- Graphtest.java Some skeleton code to get you started

What to turn in:

- Electronic copy of your modified version of
Graphtest.java, plus any
other files required for Graphtest.java to compile and
run(including Graph.java, Edge.java, and any Queue code that
you use), to subversion:

https://www.cs.usfca.edu/svn/<username>/cs245/homework9/