Computer Science 245
Spring 2009
Homework 7: Sorting, Hash Tables & Disjoint Sets
Due Friday, April 10th 3:30 p.m.
For homework 7, you will need to implement the following three functions:
- public static void BFS(Graph G, int Parent[], int startVertex, boolean Visited[])
A standard Breadth-First Search, except that 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 provided code for Queues.
- public static void DFS(Graph G, int Parent[], int startVertex, boolean Visited[])
A standard Depth-First Search, except that 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.
- 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:
- Hardcopy of your BFS and PrintPath, and any helper functions that you created. Do not turn in hardcopies of any of my
code (cut and paste the functions into a new document). Please
put your subversion directory at the top of the hardcopy, to make
grading easier for the TA.
- Electronic copy of your modified version of Graphtest.java, plus any other files required for Graphtest.java to compile and run, to subversion:
https://www.cs.usfca.edu/svn/<username>/cs245/Homework7/