如何使这段代码执行广度优先搜索?

huangapple 未分类评论50阅读模式
英文:

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 = &quot;&quot;;
	int row = contains(n), col = 0;
	while (neighbor == &quot;&quot; &amp;&amp; col &lt; 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?

huangapple
  • 本文由 发表于 2020年4月6日 22:12:02
  • 转载请务必保留本文链接:https://java.coder-hub.com/61061792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定