Intermediate Topic 34 of 51 Algorithms
Dijkstra's Algorithm
Shortest paths from a source with non-negative weights.
How it works
Dijkstra's algorithm finds the shortest paths from a source vertex to all others in a graph with non-negative edge weights. It keeps tentative distances, repeatedly settles the unvisited vertex with the smallest distance, taken from a priority queue, and relaxes its outgoing edges. With a binary heap it runs in O((V + E) log V).
Where it is used
GPS and map routing, network routing protocols such as OSPF, game pathfinding, flight itineraries and any shortest-path query on weighted graphs use Dijkstra's algorithm. A* is a goal-directed extension.
Common pitfall
Negative edge weights make it incorrect; use Bellman-Ford. Processing stale entries from the queue wastes time unless skipped. Using a visited flag at the wrong moment, or not storing parents, loses the path. For unweighted graphs BFS is simpler and faster.
Where it fits in the course
Dijkstra's Algorithm is topic 34 of 51 in Algorithms, rated Intermediate. It follows Topological Sort. The next topic is Bellman-Ford Algorithm.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Algorithms quiz.
- Explore the Graph Traversal visualizer: BFS, DFS and Dijkstra on a weighted graph.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Dijkstra's Algorithm?
Where do people use Dijkstra's Algorithm?
What is the most common mistake with Dijkstra's Algorithm?
What should I learn before this topic?
How can I practice it?
Is Dijkstra's Algorithm hard to learn?
How long does it take to learn Dijkstra's Algorithm?
Which topics are related to Dijkstra's Algorithm?
What should I study after Dijkstra's Algorithm?
Related topics
- Topological Sort (Algorithms)
- Bellman-Ford Algorithm (Algorithms)
- Depth-First Search (Algorithms)
- Floyd-Warshall Algorithm (Algorithms)
- Shor's Algorithm (Emerging & Frontier Topics)
- Grover's Algorithm (Emerging & Frontier Topics)