The Wayback Machine - https://web.archive.org/web/20230421132756/https://www.geeksforgeeks.org/can-we-use-simple-queue-instead-of-priority-queue-to-implement-dijkstras-algorithm/
Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

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

Improve Article
Save Article
Like Article
author
himanshuparihar1600
contributor
7 published articles
Improve Article
Save Article
Like Article

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).

My Personal Notes arrow_drop_up
Last Updated : 15 Sep, 2022
Like Article
Save Article
Similar Reads
Related Tutorials