Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Tutorials DSA Data Science Web Tech Courses Sign In

DSA Data Structures Array String Linked List Stack Queue Tree Binary Tree Binary Search Tree Heap Hashing Graph Trie Segment Tree Disjoint Set Union Fenwick Tree AVL Tree Red-Black Tree Advanced Data Structures

What is Dijkstra’s Algorithm? | Introduction to Dijkstra's


Shortest Path Algorithm Why does Dijkstra’s Algorithm fail on negative weights?
Last Updated : 08 Sep, 2023
How to find Shortest Paths from Source to all Vertices
using Dijkstra's Algorithm Dijkstra’s Algorithm: It is a graph searching algorithm that uses a Greedy Approach to find the shortest path from the source node to all other remaining
nodes. It solves the single-source shortest path problem for a weighted graph. This algorithm keeps track of the weights of the edges for finding the path
Dijkstra’s Algorithm for Adjacency List Representation |
Greedy Algo-8 that minimizes the total distance.

Time Complexity: O(V + E*log(V)), when priority queue is used (where V are the nodes and E are the edges)
Printing Paths in Dijkstra's Shortest Path Algorithm
Limitations of Dijkstra’s Algorithm: For this algorithm to function properly:
Why does Dijkstra's Algorithm fail on negative weights?
The graph should be weighted.
Applications of Dijkstra's shortest path algorithm The weights should be non-negative.

Dijkstra's Algorithm in different language


Advantages of Dijkstra’s Algorithm:

Different ways to implement Dijkstra's algorithm

Variations of Dijkstra's algorithm

Find minimum weight cycle in an undirected graph

Some problems on Dijkstra's algorithm

It has a linear time complexity so it can be easily used for large problems.
It is useful in finding the shortest distance, so it is also used in google maps and calculating traffic.
It has its use in areas such as telephone networks and geographical maps.

Disadvantages of Dijkstra’s Algorithm:

It is unable to handle negative weights.


It follows a kind of blind approach so there is a waste of time.

Why does Dijkstra’s Algorithm fail on negative weights?

Let’s take a simple example for a better understanding of why Dijkstra’s Algorithm fails for negative weights.

Consider acyclic directed graph with nodes A, B, and C which is connected by edges having weights that represent the cost to use this edge. The following
are the weights as mentioned in the above diagram:

A –>B = 5, A –>C = 6, C –>B = -3. Here one weight C -> B is negative.

Consider node A as the source node and the task is to find the shortest distance from source node A to all the other nodes present in the graph i.e., nodes
B and C.

So, first mark the distance as 0, at node A (as the distance from A to A is 0), and then mark this node as visited meaning that it has been included in the
shortest path.
Since in the beginning, the distance of the source node to all other nodes is not known so initialize it as infinity. Update this distance if any distance
shorter than infinity is found (which is basically the greedy approach)

Then, update the distance from source node A to B with the weight of the edge that connects it with A which is 5 (because 5 < infinity).
In a similar way, also update the distance from A to C which was previously infinity to 6 (as 6 < infinity).
Now check for the shortest distance from the source node A and as 5 is the least distance from A to B, so mark node B as ‘visited‘.
Similarly, the next shortest is 6 so mark node C also as visited. At this point, all three nodes of the graph are visited.
Now the most important step arises here, as it can be seen that by following this algorithm, the shortest distance from A –> B is 5 but if traveled the
distance via node C that is the path A –> C –> B the distance will be as 3 (as A–>C = 6 and C–>B = -3 ), so 6 + (-3) = 3. As 3 is less than 5, but Dijkstra’s
algorithm gives the incorrect answer as 5, which is not the shortest distance. Therefore Dijkstra’s Algorithm fails for negative cases.

Conclusion:

Since Dijkstra follows a Greedy Approach, once a node is marked as visited it cannot be reconsidered even if there is another path with less cost or
distance. This issue arises only if there exists a negative weight or edge in the graph.
Skip to content
So this algorithm fails to find the minimum distance in case of negative weights, so as an alternative Bellman-Ford algorithm is used to find the
shortest distance in case of negative weights, as it stops the loop when it encounters a negative cycle.

"The DSA course helped me a lot in clearing the interview rounds. It was really very helpful in setting a strong foundation for my problem-solving skills.
Really a great investment, the passion Sandeep sir has towards DSA/teaching is what made the huge difference." - Gaurav | Placed at Amazon

Before you move on to the world of development, master the fundamentals of DSA on which every advanced algorithm is built upon. Choose your preferred
language and start learning today:

DSA In JAVA/C++
DSA In Python
DSA In JavaScript
Trusted by Millions, Taught by One- Join the best DSA Course Today!

Recommended Problems
Solve Problems
Frequently asked DSA Problems

18 Suggest improvement

Previous Next

Printing Paths in Dijkstra's Shortest Path Algorithm Applications of Dijkstra's shortest path algorithm

Share your thoughts in the comments Add Your Comment

Similar Reads

Dial's Algorithm (Optimized Dijkstra for small range weights) Assign weights to edges such that longest path in terms of weights is minimized

Find optimal weights which can be used to weigh all the weights in the range [1, X] Edge Relaxation Property for Dijkstra’s Algorithm and Bellman Ford's Algorithm

Dijkstra's shortest path algorithm in Java using PriorityQueue Java Program for Dijkstra's shortest path algorithm | Greedy Algo-7

Python Program for Dijkstra's shortest path algorithm | Greedy Algo-7 C# Program for Dijkstra's shortest path algorithm | Greedy Algo-7

Widest Path Problem | Practical application of Dijkstra's Algorithm Applications of Dijkstra's shortest path algorithm

tausifsidd…

Article Tags : Algorithms-Graph Shortest Paths Quiz , Dijkstra , Shortest Path , Analysis of Algorithms , DSA , Graph , Greedy
Practice Tags : Graph, Greedy, Shortest Path

Company Explore Languages DSA Data Science & ML HTML & CSS
A-143, 9th Floor, Sovereign Corporate About Us Hack-A-Thons Python Data Structures Data Science With Python HTML
Tower, Sector-136, Noida, Uttar Pradesh - Legal GfG Weekly Contest Java Algorithms Data Science For Beginner CSS
201305 Careers DSA in JAVA/C++ C++ DSA for Beginners Machine Learning Tutorial Web Templates
In Media Master System Design PHP Basic DSA Problems ML Maths CSS Frameworks
Contact Us Master CP GoLang DSA Roadmap Data Visualisation Tutorial Bootstrap
Advertise with us GeeksforGeeks Videos SQL Top 100 DSA Interview Problems Pandas Tutorial Tailwind CSS
GFG Corporate Solution Geeks Community R Language DSA Roadmap by Sandeep Jain NumPy Tutorial SASS
Placement Training Program Android Tutorial All Cheat Sheets NLP Tutorial LESS
Tutorials Archive Deep Learning Tutorial Web Design
Django Tutorial

Python Tutorial Computer Science DevOps Competitive Programming System Design JavaScript
Python Programming Examples Operating Systems Git Top DS or Algo for CP High Level Design JavaScript Examples
Python Projects Computer Network AWS Top 50 Tree Low Level Design TypeScript
Python Tkinter Database Management System Docker Top 50 Graph UML Diagrams ReactJS
Web Scraping Software Engineering Kubernetes Top 50 Array Interview Guide NextJS
OpenCV Tutorial Digital Logic Design Azure Top 50 String Design Patterns AngularJS
Python Interview Question Engineering Maths GCP Top 50 DP OOAD NodeJS
DevOps Roadmap Top 15 Websites for CP System Design Bootcamp Lodash
Interview Questions Web Browser

Preparation Corner School Subjects Management & Finance Free Online Tools More Tutorials GeeksforGeeks Videos
Company-Wise Recruitment Process Mathematics Management Typing Test Software Development DSA
Resume Templates Physics HR Management Image Editor Software Testing Python
Aptitude Preparation Chemistry Finance Code Formatters Product Management Java
Puzzles Biology Income Tax Code Converters SAP C++
Company-Wise Preparation Social Science Organisational Behaviour Currency Converter SEO - Search Engine Optimization Data Science
English Grammar Marketing Random Number Generator Linux CS Subjects
World GK Random Password Generator Excel

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

You might also like