Open In App

Can we use Simple Queue instead of Priority queue to implement Dijkstra’s Algorithm?

Last Updated : 15 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

What is Dijkstra’s Algorithm?

Dijkstra’s Algorithm is used for finding the shortest path between any two vertices of a graph. It uses a priority queue for finding the shortest path. For more detail, about Dijkstra’s Algorithm, you can refer to this article.

Why Dijkstra’s Algorithm uses a Priority Queue?

We use min heap in Dijkstra’s Algorithm because a node can have multiple weighted edges but for the shortest path, we have to consider the smallest weighted edge associated with a node. For this, we use a priority queue (min-heap) which gives us the minimum element on the top of the priority queue.

For the implementation of Dijkstra’s Algorithm using Priority Queue, you can check out this GFG Article.

The Time Complexity of Dijkstra’s Algorithm is O((V+E)* logV), here V = no. of nodes in the graph, E = no. of edges in the graph

Can we use a Queue instead of a Priority Queue?

The answer to this question is yes, we can use a queue instead of Priority Queue. 

The only difference between a queue and a priority queue is that we have to traverse all connected nodes of a current node and find the minimum among them when we use a normal queue which takes time of O(V). But using the priority queue we can optimize it to O(log V). 

The Time Complexity of Dijkstra’s Algorithm using a normal queue is O(V2).


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads