Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
865 views
in Technique[技术] by (71.8m points)

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

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

What is the difference between Dijkstra's algorithm and BFS while looking for the shortest-path between nodes?

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


The rules for BFS for finding shortest-path in a graph are:

  1. We discover all the connected vertices,
  2. Add them in the queue and also
  3. Store the distance (weight/length) from source u to that vertex v.
  4. Update with path from source u to that vertex v with shortest distance and we have it!

This is exactly the same thing we do in Dijkstra's algorithm!


So why are the time complexities of these algorithms so different?

If anyone can explain it with the help of a pseudo code then I will be very grateful!

I know I am missing something! Please help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Breadth-first search is just Dijkstra's algorithm with all edge weights equal to 1.

Dijkstra's algorithm is conceptually breadth-first search that respects edge costs.

The process for exploring the graph is structurally the same in both cases.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...