使用广度优先搜索找到最短路径节点
Finding the shortest path nodes with breadth first search
我在上图运行广度优先搜索中寻找从Node 0
到Node 6
的最短路径。
我的代码
public List<Integer> shortestPathBFS(int startNode, int nodeToBeFound){
boolean shortestPathFound = false;
Queue<Integer> queue = new LinkedList<Integer>();
Set<Integer> visitedNodes = new HashSet<Integer>();
List<Integer> shortestPath = new ArrayList<Integer>();
queue.add(startNode);
shortestPath.add(startNode);
while (!queue.isEmpty()) {
int nextNode = queue.peek();
shortestPathFound = (nextNode == nodeToBeFound) ? true : false;
if(shortestPathFound)break;
visitedNodes.add(nextNode);
System.out.println(queue);
Integer unvisitedNode = this.getUnvisitedNode(nextNode, visitedNodes);
if (unvisitedNode != null) {
queue.add(unvisitedNode);
visitedNodes.add(unvisitedNode);
shortestPath.add(nextNode); //Adding the previous node of the visited node
shortestPathFound = (unvisitedNode == nodeToBeFound) ? true : false;
if(shortestPathFound)break;
} else {
queue.poll();
}
}
return shortestPath;
}
我需要追踪 BFS 算法通过的节点。遍历到节点6,如[0,3,2,5,6]
。为此,我创建了一个名为 shortestPath
的列表,并尝试存储已访问节点的先前节点,以获取节点列表。 Referred
不过好像不行。最短路径是[0,3,2,5,6]
在列表中我得到的是Shortest path: [0, 0, 0, 0, 1, 3, 3, 2, 5]
它部分正确,但给出了额外的 1
.
如果我再次从 shortestPath
列表的第一个元素 0
开始并开始遍历和回溯。就像 1
没有 3
的优势,所以我回溯并从 0
移动到 3
再到 5
,我会得到答案但不是确定这是否是正确的方法。
获取最短路径节点的理想方法是什么?
将所有访问过的节点存储在一个列表中无助于找到最短路径,因为最终您无法知道哪些节点是通向目标节点的节点,哪些节点是死胡同.
你需要做的是对每个节点存储路径中从起始节点开始的前一个节点。
所以,创建一个地图 Map<Integer, Integer> parentNodes
,而不是这个:
shortestPath.add(nextNode);
这样做:
parentNodes.put(unvisitedNode, nextNode);
到达目标节点后,您可以遍历该地图找到返回起始节点的路径:
if(shortestPathFound) {
List<Integer> shortestPath = new ArrayList<>();
Integer node = nodeToBeFound;
while(node != null) {
shortestPath.add(node)
node = parentNodes.get(node);
}
Collections.reverse(shortestPath);
}
除了用户 3290797 已经给出的答案。
看起来你正在处理一个未加权的图。我们将其解释为每条边的权重为 1。在这种情况下,一旦您将到根节点的距离与图中的每个节点相关联(breadth-first 遍历),重建最短路径就变得微不足道了来自任何节点,甚至检测是否有多个节点。
您需要做的只是广度-(如果您想要每条最短路径)或depth-first从目标节点开始遍历同一个图,并且只考虑深度值恰好为 1 的邻居较少的。
因此我们需要从距离 4(节点 6)跳到 3、2、1、0,而且只有一种方法(在本例中)。
如果我们对到节点 4 的最短路径感兴趣,结果将是距离 2-1-0 或节点 4-3-0 或 4-8-0。
顺便说一句,这种方法也可以很容易地修改为也适用于加权图(具有 non-negative 权重):有效邻居是距离等于当前减去边缘权重的那些——这涉及一些实际的计算并沿最短路径直接存储先前的节点可能会更好。
如您在 acheron55 answer 中所见:
"It has the extremely useful property that if all of the edges in a graph are unweighted (or the same weight) then the first time a node is visited is the shortest path to that node from the source node"
因此,您所要做的就是跟踪已达到目标的路径。
一种简单的方法是将用于到达节点的整个路径推入 Queue
,而不是节点本身。
这样做的好处是,当到达目标时,队列保存用于到达它的路径。
这是一个简单的实现:
/**
* unlike common bfs implementation queue does not hold a nodes, but rather collections
* of nodes. each collection represents the path through which a certain node has
* been reached, the node being the last element in that collection
*/
private Queue<List<Node>> queue;
//a collection of visited nodes
private Set<Node> visited;
public boolean bfs(Node node) {
if(node == null){ return false; }
queue = new LinkedList<>(); //initialize queue
visited = new HashSet<>(); //initialize visited log
//a collection to hold the path through which a node has been reached
//the node it self is the last element in that collection
List<Node> pathToNode = new ArrayList<>();
pathToNode.add(node);
queue.add(pathToNode);
while (! queue.isEmpty()) {
pathToNode = queue.poll();
//get node (last element) from queue
node = pathToNode.get(pathToNode.size()-1);
if(isSolved(node)) {
//print path
System.out.println(pathToNode);
return true;
}
//loop over neighbors
for(Node nextNode : getNeighbors(node)){
if(! isVisited(nextNode)) {
//create a new collection representing the path to nextNode
List<Node> pathToNextNode = new ArrayList<>(pathToNode);
pathToNextNode.add(nextNode);
queue.add(pathToNextNode); //add collection to the queue
}
}
}
return false;
}
private List<Node> getNeighbors(Node node) {/* TODO implement*/ return null;}
private boolean isSolved(Node node) {/* TODO implement*/ return false;}
private boolean isVisited(Node node) {
if(visited.contains(node)) { return true;}
visited.add(node);
return false;
}
这也适用于循环图,其中一个节点可以有多个父节点。
我在上图运行广度优先搜索中寻找从Node 0
到Node 6
的最短路径。
我的代码
public List<Integer> shortestPathBFS(int startNode, int nodeToBeFound){
boolean shortestPathFound = false;
Queue<Integer> queue = new LinkedList<Integer>();
Set<Integer> visitedNodes = new HashSet<Integer>();
List<Integer> shortestPath = new ArrayList<Integer>();
queue.add(startNode);
shortestPath.add(startNode);
while (!queue.isEmpty()) {
int nextNode = queue.peek();
shortestPathFound = (nextNode == nodeToBeFound) ? true : false;
if(shortestPathFound)break;
visitedNodes.add(nextNode);
System.out.println(queue);
Integer unvisitedNode = this.getUnvisitedNode(nextNode, visitedNodes);
if (unvisitedNode != null) {
queue.add(unvisitedNode);
visitedNodes.add(unvisitedNode);
shortestPath.add(nextNode); //Adding the previous node of the visited node
shortestPathFound = (unvisitedNode == nodeToBeFound) ? true : false;
if(shortestPathFound)break;
} else {
queue.poll();
}
}
return shortestPath;
}
我需要追踪 BFS 算法通过的节点。遍历到节点6,如[0,3,2,5,6]
。为此,我创建了一个名为 shortestPath
的列表,并尝试存储已访问节点的先前节点,以获取节点列表。 Referred
不过好像不行。最短路径是[0,3,2,5,6]
在列表中我得到的是Shortest path: [0, 0, 0, 0, 1, 3, 3, 2, 5]
它部分正确,但给出了额外的 1
.
如果我再次从 shortestPath
列表的第一个元素 0
开始并开始遍历和回溯。就像 1
没有 3
的优势,所以我回溯并从 0
移动到 3
再到 5
,我会得到答案但不是确定这是否是正确的方法。
获取最短路径节点的理想方法是什么?
将所有访问过的节点存储在一个列表中无助于找到最短路径,因为最终您无法知道哪些节点是通向目标节点的节点,哪些节点是死胡同.
你需要做的是对每个节点存储路径中从起始节点开始的前一个节点。
所以,创建一个地图 Map<Integer, Integer> parentNodes
,而不是这个:
shortestPath.add(nextNode);
这样做:
parentNodes.put(unvisitedNode, nextNode);
到达目标节点后,您可以遍历该地图找到返回起始节点的路径:
if(shortestPathFound) {
List<Integer> shortestPath = new ArrayList<>();
Integer node = nodeToBeFound;
while(node != null) {
shortestPath.add(node)
node = parentNodes.get(node);
}
Collections.reverse(shortestPath);
}
除了用户 3290797 已经给出的答案。
看起来你正在处理一个未加权的图。我们将其解释为每条边的权重为 1。在这种情况下,一旦您将到根节点的距离与图中的每个节点相关联(breadth-first 遍历),重建最短路径就变得微不足道了来自任何节点,甚至检测是否有多个节点。
您需要做的只是广度-(如果您想要每条最短路径)或depth-first从目标节点开始遍历同一个图,并且只考虑深度值恰好为 1 的邻居较少的。
因此我们需要从距离 4(节点 6)跳到 3、2、1、0,而且只有一种方法(在本例中)。
如果我们对到节点 4 的最短路径感兴趣,结果将是距离 2-1-0 或节点 4-3-0 或 4-8-0。
顺便说一句,这种方法也可以很容易地修改为也适用于加权图(具有 non-negative 权重):有效邻居是距离等于当前减去边缘权重的那些——这涉及一些实际的计算并沿最短路径直接存储先前的节点可能会更好。
如您在 acheron55 answer 中所见:
"It has the extremely useful property that if all of the edges in a graph are unweighted (or the same weight) then the first time a node is visited is the shortest path to that node from the source node"
因此,您所要做的就是跟踪已达到目标的路径。
一种简单的方法是将用于到达节点的整个路径推入 Queue
,而不是节点本身。
这样做的好处是,当到达目标时,队列保存用于到达它的路径。
这是一个简单的实现:
/**
* unlike common bfs implementation queue does not hold a nodes, but rather collections
* of nodes. each collection represents the path through which a certain node has
* been reached, the node being the last element in that collection
*/
private Queue<List<Node>> queue;
//a collection of visited nodes
private Set<Node> visited;
public boolean bfs(Node node) {
if(node == null){ return false; }
queue = new LinkedList<>(); //initialize queue
visited = new HashSet<>(); //initialize visited log
//a collection to hold the path through which a node has been reached
//the node it self is the last element in that collection
List<Node> pathToNode = new ArrayList<>();
pathToNode.add(node);
queue.add(pathToNode);
while (! queue.isEmpty()) {
pathToNode = queue.poll();
//get node (last element) from queue
node = pathToNode.get(pathToNode.size()-1);
if(isSolved(node)) {
//print path
System.out.println(pathToNode);
return true;
}
//loop over neighbors
for(Node nextNode : getNeighbors(node)){
if(! isVisited(nextNode)) {
//create a new collection representing the path to nextNode
List<Node> pathToNextNode = new ArrayList<>(pathToNode);
pathToNextNode.add(nextNode);
queue.add(pathToNextNode); //add collection to the queue
}
}
}
return false;
}
private List<Node> getNeighbors(Node node) {/* TODO implement*/ return null;}
private boolean isSolved(Node node) {/* TODO implement*/ return false;}
private boolean isVisited(Node node) {
if(visited.contains(node)) { return true;}
visited.add(node);
return false;
}
这也适用于循环图,其中一个节点可以有多个父节点。