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


Introduction

Dijkstra’s Algorithm is used to find the shortest possible distance between two objects. To implement this algorithm we mostly use Priority Queue. In this tutorial, we will find the answer to whether can we use a simple queue to implement Dijkstra’s algorithm instead of a Priority Queue.

What is a Priority Queue and A Queue?

A queue is a linear array of data. It represents the Queue of real life. A simple Queue uses FIFO (First In First Out) approach for its deQueue and enQueue operations.

A priority Queue is a type of Queue that deQueue its elements with their priority. It removes its elements with the highest or lowest priority based on the type of Priority Queue.

There are two types of Priority Queues −

  • Ascending Order Priority Queue or Min Queue

  • Descending Order Priority Queue or Max Queue

Dijkstra’s Algorithm

Dijkstra Algorithm is a single source shortest path algorithm. This algorithm aims to determine the smallest distance between two objects. It uses directed or undirected graphs to find the shortest path from one node to multiple nodes. It traverses several vertices from source to destination to get the minimum distance.

Dijkstra Algorithm uses a greedy approach to get the most optimal solution. There are various real-life applications of the Dijkstra Algorithm; the one we all mostly use is Google Maps. The other applications of this algorithm are Social Networking and Telephonic Lines.

The Time Complexity of Dijkstra Algorithm is O (E log V)

Where,

E = Edges of the Graph

V = Vertices of the Graph

The space Complexity of the Dijkstra Algorithm is O (V)

Why we Use Priority Queue to Implement Dijkstra Algorithm?

The important feature of the priority Queue is arranging elements as per priority. Priority Queue is used to implement the Dijkstra algorithm as all vertices have distances and we traverse the graph to find the shortest distance. The distance will act as a priority for the Queue and will deQueue the nodes with the smallest priority.

By using the min priority queue, the queue will arrange the shortest distance nodes on the queue top. The graph undergoes several insertions and deletions of the nodes with changing priorities.

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

Yes, we can use Simple Queue for Dijkstra Algorithm implementation but it is not feasible. To find the shortest path, we have to traverse the graph and this will take the time complexity of O(V) with Queue.

The problems with implementing Dijkstra Algorithm using Simple Graph are −

  • Simple Queue uses the FIFO principle to insert and remove its elements. Finding the shortest weights of the node is not easy.

  • Its elements are arranged in the order of first come first served. Not in increasing or decreasing order.

  • Result accuracy is very low.

  • It consumes more time.

  • The time complexity of the Dijkstra Algorithm using a simple Queue is O(V2). The time complexity using Priority Queue is O (log V).

Conclusion

A simple Queue has more traversing time, which will increase the time complexity of the Dijkstra Algorithm. Resulting in the slow working of the Algorithm.

Using the min priority queue, we can easily remove the shortest node from the queue top in a small amount of time.

Updated on: 22-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements