简单的 bfs 示例...我不明白
我试图了解 BFS 如何与队列一起工作来找出最短路径。假设我有一个网格:
1--2--3
| | |
4--5--6
| | |
7--8--9
|
0
起始点是“9”,目标是“0”。
所以...我按下开始...
push 9 {9}
pop 9 {}
push 6 {6}
push 8 {6,8}
pop 6 {8}
push 3 {8,3}
push 5 {8,3,5}
pop 8 {3,5}
push 7 {3,5,7}
pop 3 {5,7}
push 2 {5,7,2}
pop 5 {7,2}
push 4 {7,2,4}
pop 7 {2,5}
found 0
我怎样才能从这个混乱中提取最短路径?我不明白这如何给我最短路径。难道是我想错了?
谢谢!
I'm trying to understand how BFS works with a queue to figure out the shortest path. Let's say I have a grid:
1--2--3
| | |
4--5--6
| | |
7--8--9
|
0
Starting spot is '9' and target is '0'.
So...I push the start...
push 9 {9}
pop 9 {}
push 6 {6}
push 8 {6,8}
pop 6 {8}
push 3 {8,3}
push 5 {8,3,5}
pop 8 {3,5}
push 7 {3,5,7}
pop 3 {5,7}
push 2 {5,7,2}
pop 5 {7,2}
push 4 {7,2,4}
pop 7 {2,5}
found 0
How can I extract the shortest path from this mess? I don't see how this gives me the shortest path. Am I thinking about it wrong?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了找到最短路径,每个节点还应该“记住”在 BFS 期间如何到达它(哪个顶点导致发现它)。
在 cpp 中,对于您的示例,您可以使用
map
。简单的例子:
要获得最短路径,只需沿着从 0 到源的路径 [当值为 -1 时]。
To find the shortest path, each node should also "remember" how you reached it during your BFS [which vertex led to discovering it].
In cpp, for your example, you can use a
map<int,int>
for it.Simple example:
To get the shortest path, just follow the path from 0 to the source [when value is -1].
您需要做的是记住对于每个节点,您是如何到达那里的。这涉及向每个节点添加数据成员(如果您使用结构或类来表示节点),或者以侵入性较小的方式保留整数或节点指针的并行列表。由于您用 C++ 标记了它,我假设您正在寻找 C++ 解决方案。像这样的事情是有效的:
在这段代码中,向量reach_by用于跟踪每个节点是从哪个节点到达的。一旦找到目标,您就可以使用该向量向后追踪路径到起点。
运行该程序的输出是
What you need to do is to remember, for each node, how you got there. This involves either adding a data member to each node (if you are using structs or classes to represent nodes) or, less invasively, keep a parallel list of integers or node pointers. Since you tagged this with C++ I assume that you are looking for a C++ solution. Something like this works:
In this code the vector reached_by is used to keep track of from which node every node was reached. Once the target is found you can just trace the path backwards to the starting point using that vector.
The output of running this program is