DSA Notes
Master Dijkstra\
The Problem
Given a weighted graph with non-negative edge weights and a source vertex, find the shortest path from the source to every other vertex. BFS cannot solve this because edges have different weights — a path with more edges might be shorter if those edges have small weights.
The Intuition
Imagine you are at a city and want to find the shortest drive time to all other cities. Dijkstra's approach is greedy: always expand the closest unvisited city next. Why does this work? Because all edge weights are non-negative, so once you have found the shortest path to a city, no future discovery can improve it.
Think of it like a spreading wavefront — but instead of spreading at uniform speed (like BFS), it spreads faster along lighter edges.
The Algorithm
- Initialize distances: source = 0, all others = infinity
- Use a priority queue (min-heap) to always process the closest vertex first
- For the current vertex u, examine each neighbor v:
- If
dist[u] + weight(u,v) < dist[v], update dist[v] (this is called relaxation) - Add v with new distance to priority queue
- Repeat until all vertices are processed
Step-by-Step Example
Graph
A --4-- B --2-- E
| | /
2 1 3
| | /
C --5-- D
Find shortest paths from A
Initial: dist = {A:0, B:inf, C:inf, D:inf, E:inf}
PQ = [(0, A)]
Step 1: Process A (dist=0)
Relax A→B: 0+4=4 < inf → dist[B]=4
Relax A→C: 0+2=2 < inf → dist[C]=2
PQ = [(2,C), (4,B)]
Step 2: Process C (dist=2)
Relax C→D: 2+5=7 < inf → dist[D]=7
PQ = [(4,B), (7,D)]
Step 3: Process B (dist=4)
Relax B→D: 4+1=5 < 7 → dist[D]=5 (improved!)
Relax B→E: 4+2=6 < inf → dist[E]=6
PQ = [(5,D), (6,E), (7,D_old)]
Step 4: Process D (dist=5)
Relax D→E: 5+3=8 > 6 → no improvement
PQ = [(6,E)]
Step 5: Process E (dist=6)
No unvisited neighbors.
Final distances from A: {A:0, B:4, C:2, D:5, E:6}
Shortest paths: A→C (2), A→B (4), A→B→D (5), A→B→E (6)
Python Implementation
C++ Implementation
Time and Space Complexity
- With binary heap: O((V + E) log V) — each vertex extracted once, each edge relaxed once, heap operations are O(log V)
- With Fibonacci heap: O(V log V + E) — theoretical improvement, rarely used in practice
- Space: O(V + E) for graph storage + O(V) for distance array and heap
Why Non-Negative Weights Only?
Dijkstra's greedy property depends on this fact: once a vertex is processed (popped from PQ), its distance is final. This holds because all future paths to it would go through unvisited vertices with equal or greater distance, plus non-negative edges.
If negative edges exist, a longer path through an unvisited vertex could end up shorter. In that case, use Bellman-Ford.
Applications
- GPS Navigation: Finding shortest driving route between locations
- Network routing: OSPF protocol uses Dijkstra for IP routing
- Game pathfinding: A* algorithm is Dijkstra with a heuristic
- Flight booking: Finding cheapest flight connections
- Social networks: Finding shortest chain of connections
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Dijkstra\.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, graphs, dijkstra, algorithm
Related Data Structures & Algorithms Topics