How does a Breadth-First Search work when looking for Shortest Path?

前端 未结 8 1442
太阳男子
太阳男子 2020-11-30 16:52

I\'ve done some research, and I seem to be missing one small part of this algorithm. I understand how a Breadth-First Search works, but I don\'t understand how exactly it wi

8条回答
  •  有刺的猬
    2020-11-30 17:05

    Based on acheron55 answer I posted a possible implementation here.
    Here is a brief summery of it:

    All you have to do, is to keep track of the path through which the target has been reached. A simple way to do it, is to push into the Queue the whole path used to reach a node, rather than the node itself.
    The benefit of doing so is that when the target has been reached the queue holds the path used to reach it.
    This is also applicable to cyclic graphs, where a node can have more than one parent.

提交回复
热议问题