What is difference between BFS and Dijkstra's algorithms when looking for shortest path?

后端 未结 3 914
醉酒成梦
醉酒成梦 2020-12-12 16:37

I was reading about Graph algorithms and I came across these two algorithms.

I searched a lot about this but didn\'t get any satisfactory answer!

I have a do

3条回答
  •  感动是毒
    2020-12-12 17:09

    Blockquote while using BFS for finding shortest path in a graph what we do is We discover all the connected vertices, add them in the queue and also maintain the distance from source to that vertex. Now if we find a path from source to that vertex with still less distance then we update it!

    We do not maintain a distance in BFS. It is for discovery of nodes. So we put them in a general queue and pop them. unlike in Dijikstra where we put accumulative weight of node (after relaxation) in a priority queue and pop the min distance.

    So BFS would work like dijikstra in equal weight graph because.

    complexity varies because of the use of simple queue and priority queue.

提交回复
热议问题