What is Dijkstra's Algorithm?
Dijkstra's algorithm finds the shortest-distance path from a single start vertex to every other vertex in a weighted graph — "shortest" meaning the smallest total edge weight along the path, not the fewest edges (which is what plain BFS finds on an unweighted graph). It requires every edge weight to be non-negative; a single negative weight can break its core assumption and produce wrong answers.
How Does It Work?
The algorithm keeps a tentative distance for every vertex, starting at 0 for the source and infinity for everything else. At each step, it finalizes whichever unvisited vertex currently has the smallest tentative distance — once a vertex is finalized, its distance is guaranteed correct and will never be revised again. Then it "relaxes" every edge out of that vertex: for each neighbor, if going through the just-finalized vertex would produce a shorter distance than what's currently recorded, the neighbor's distance is updated.
The key insight behind why picking the smallest unvisited distance is always safe: since every edge weight is non-negative, any path to that vertex through a still-unvisited (and therefore farther-or-equal) vertex could only be equal or longer. There's no way a shortcut could still be waiting to be discovered. That guarantee is exactly what breaks down if a negative edge weight is allowed — a path through a vertex that currently looks farther away could later turn out shorter, and algorithms like Bellman-Ford exist specifically to handle that case.
Algorithm Steps
- Set the start vertex's distance to 0, and every other vertex's distance to infinity
- While unvisited vertices remain, repeat:
- Pick the unvisited vertex with the smallest tentative distance and mark it finalized
- For each of its neighbors, if the path through this vertex is shorter than the neighbor's current recorded distance, update it (this is a "relaxation")
- Once every reachable vertex is finalized, each vertex's recorded distance is its true shortest distance from the start
- To reconstruct the actual shortest path to any vertex, follow the chain of "came from" pointers recorded during relaxation, back to the start
Time Complexity
- Time Complexity: O((V + E) log V) with a binary heap priority queue — each vertex is extracted once and each edge triggers at most one relaxation, both at logarithmic cost.
- Space Complexity: O(V) — for the distance array, the previous-vertex pointers, and the priority queue.
Time Complexity Analysis
Dijkstra's algorithm (typically implemented with a min-priority-queue for efficiency) is the standard tool behind GPS and mapping route-finding, network routing protocols that pick the cheapest path between routers, and any scenario where "cheapest route through a weighted network" needs an exact answer rather than an approximation.