I am confused why we can't simply use a normal queue instead of a priority queue for Dijkstra's. I agree that you can find the shortest path in fewer iterations of the while loop using a priority queue. However, the runtime will still be O((E + V) log V). However, with a queue, that runtime will be O(E + V). Can anybody find a simple example of where Dijkstra's would fail if we used a queue instead of a Priority Queue?
3 Answers
The whole point of Dijkstra is that you visit the nodes in order of their distance from the source. If you use a queue that isn't a priority queue, then you visit the nodes in whatever "random" order the implementation happens to enqueue them.
- 82,470
- 26
- 145
- 239
Actually never mind I just realized when you are trying to find A -> F:
A -> C 3
C - > D 3
D -> E 1
E -> F 1
A -> E 10
We will first assign an incorrect value to F (11) based on a longer distance value to E (10). When E is corrected, we would rely on backtracking to edit F's distance which would be more inefficient than using the Priority queue.
- 31
- 3
yes indeed you can use Queue, but there will be unnecessary push of element in the Queue as compared to Priority Queue.
There is a Tradeoff between iteration(in Queue) and GetTop (Log(V)) in Priority queue. Dijkstra was smart enough, he calculated this Tradeoff and gave the Dijkstra's Algorithm.
Also, Dijkstra's Algorithm make sure that each node will relax it's child at most once. This is not the case with Queue, in the simple Queue each node can relax its child multiple times leading to worst case which will be much greater than O(V+E), the worst case complexity can be exponential as well (not confirmed exactly, it can be N^2 as well). But for sure much greater than O(V+E).
you can just refer the below graph and Dry run for Simple Queue and Dijkstra's Algo. You will get it, what is Unnecessary push of nodes.
try to find A -> F:
A -> C 3
C - > D 3
D -> E 1
E -> F 1
A -> E 10
- 1
- 1