For my computer science class we need to use the following template code and modify it to do a breadth first search. The code, untouched, already performs a DFS. I know that a breadth first search involves listing all unvisited neighbors of a node before moving on to the next node and repeating. We are supposed to be using a Queue
class with peek()
, enqueue()
and dequeue()
methods -- assume these all work correctly.
My code:
/**
* returns an unvisited neighbor of a node
* @param n
* @param v
* @return
*/
public String getUnvisitedNeighbor (String n, boolean [] v) {
String neighbor = "";
int row = contains(n), col = 0;
while (neighbor == "" && col < nodes.length) {
if (matrix[row][col] == true) // potentially unvisited neighbor found
if (v[col] == false)
neighbor = nodes[col];
col++;
}
return neighbor;
}
Any ideas as to what I could do about this?