- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion

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

- Related Articles
- C++ Program to Implement Priority Queue
- Should we declare it as Queue or Priority Queue while using Priority Queue in Java?
- How to Implement Priority Queue in Python?
- Turn a Queue into Priority Queue
- Meldable Priority Queue Operations
- Difference Between Priority Queue and Queue Implementation in Java?
- How can we Implement a Stack using Queue in Java?
- The Priority Queue in Javascript
- Double Ended Priority Queue (DEPQ)
- Multithreaded Priority Queue in Python
- How can we Implement a Queue using Stack in Java?\n
- Why can’t a Priority Queue wrap around like an ordinary Queue?
- Creating a Priority Queue using Javascript
- Dequeue and Priority Queue in C++
- Python Heap Queue Algorithm