英文:
How to get this code to do a breadth first search?
问题
/**
* 返回一个节点的未访问过的邻居
* @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) { // 可能是未访问的邻居
if (v[col] == false) {
neighbor = nodes[col];
}
}
col++;
}
return neighbor;
}
请注意,这是代码部分的中文翻译。如果您还有其他问题或需要进一步的帮助,请随时提问。
英文:
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?
专注分享java语言的经验与见解,让所有开发者获益!
评论