Shortest Paths
Shortest paths
Many real problems use shortest-path algorithms, including finding a fast metro journey. Stations are vertices, connections are edges, and weights may represent distance, time or cost.
Among all routes between two vertices in a network, the shortest path minimizes total edge weight. Its first vertex is the source and its last is the destination.
There are two types of problem:
-
Single source: find shortest paths from one fixed source to all other vertices.
-
All pairs: find shortest paths between every pair of vertices.
Single-source paths in an unweighted graph
An unweighted single-source shortest-path algorithm discovers distances in nondecreasing order, like breadth-first traversal.
For example, from source 3, vertices 1 and 6 have distance 1; vertices 2 and 4 distance 2; vertices 5 and 7 distance 3.
Unweighted shortest paths: discover layers and predecessors
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
dist[v] stores the shortest distance from source S to v, with dist[S] = 0. path[v] stores the vertex immediately preceding v on the route.
| Vertex | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| dist | 1 | 2 | 0 | 2 | 3 | 1 | 3 |
| path | 3 | 1 | -1 | 1 | 2 | 3 | 4 |
Unweighted single-source shortest paths
unweightedSSSP(Vertex S)
dist[S] = 0; dist[other vertices] = -1; path[*] = NULL
enqueue(Q, S)
while !isEmpty(Q)
V = dequeue(Q)
for v in neighbors(V)
if dist[v] == -1
dist[v] = dist[V] + 1
path[v] = V
enqueue(Q, v)Single-source paths in a weighted graph
The shortest weighted route need not use the fewest edges. A reachable negative-cost cycle prevents a finite shortest distance for vertices reachable from that cycle: following it repeatedly reduces cost without bound.
Dijkstra's algorithm solves the single-source problem when every edge weight is nonnegative. It cannot be used directly with negative edges.
Dijkstra—what a name!
The algorithm continually updates a table of distances from the source. It uses a greedy choice: settle the unprocessed vertex with the smallest dist. Settling one vertex may improve another vertex's tentative distance.
For example, find shortest paths from A.
Dijkstra: settle vertices and relax edges
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
First initialize the table. The source A has value 0; all other vertices start at because no route is known.
| B | C | D | E | F | G |
|---|---|---|---|---|---|
Process A's neighbors: B is 5 units away and C is 2.
| B | C | D | E | F | G |
|---|---|---|---|---|---|
| 5 | 2 |
Settle the closest candidate C. Its edges to D and F give candidate distances and .
| B | C | D | E | F | G |
|---|---|---|---|---|---|
| 5 | 2 | 8 | 10 |
Next settle B. Its edge to D improves D to , below 8. Its edge to E gives .
| B | C | D | E | F | G |
|---|---|---|---|---|---|
| 5 | 2 | 6 | 11 | 10 |
Settle D. Improve E to and F to .
| B | C | D | E | F | G |
|---|---|---|---|---|---|
| 5 | 2 | 6 | 7 | 8 |
Settle E. The edge to G gives .
| B | C | D | E | F | G |
|---|---|---|---|---|---|
| 5 | 2 | 6 | 7 | 8 | 14 |
Settle F. Improve G to rather than 14.
| B | C | D | E | F | G |
|---|---|---|---|---|---|
| 5 | 2 | 6 | 7 | 8 | 11 |
Finally the table contains shortest distances from A to all vertices.
KNOWLEDGE QUIZ
All-pairs shortest paths
How can we compute the shortest distance between every pair of vertices?
One approach is to run Dijkstra once from every source.
This works for nonnegative weights, but is there a simpler algorithm?
Floyd–Warshall solves the all-pairs problem provided there is no negative cycle. Its idea is that a route between two vertices can become shorter by going through an intermediate vertex.
Let be the minimum length of a route from i to j whose intermediate vertices are among those numbered at most k. Given :
-
If the optimum does not use k, retain .
-
If it uses k, join the shortest i-to-k and k-to-j routes. Combine both cases by taking the minimum:
For example, a traveler wants to know the shortest road distances between every pair of cities before planning a trip.
A detour through a third city may shorten a route. Sometimes the best route needs two or more intermediate cities.
With no intermediate city permitted, the initial distances come from the adjacency matrix.
Floyd: introduce intermediate vertices one at a time
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
| A | B | C | D | |
|---|---|---|---|---|
| A | 0 | 2 | 6 | 4 |
| B | 0 | 3 | ||
| C | 7 | 0 | 1 | |
| D | 5 | 12 | 0 |
Allow city 1 as an intermediate:
| A | B | C | D | |
|---|---|---|---|---|
| A | 0 | 2 | 6 | 4 |
| B | 0 | 3 | ||
| C | 7 | 9 | 0 | 1 |
| D | 5 | 7 | 11 | 0 |
Allow cities 1 and 2:
| A | B | C | D | |
|---|---|---|---|---|
| A | 0 | 2 | 5 | 4 |
| B | 0 | 3 | ||
| C | 7 | 9 | 0 | 1 |
| D | 5 | 7 | 10 | 0 |
Allow cities 1, 2 and 3:
| A | B | C | D | |
|---|---|---|---|---|
| A | 0 | 2 | 5 | 4 |
| B | 10 | 0 | 3 | 4 |
| C | 7 | 9 | 0 | 1 |
| D | 5 | 7 | 10 | 0 |
Finally allow every city:
| A | B | C | D | |
|---|---|---|---|---|
| A | 0 | 2 | 5 | 4 |
| B | 9 | 0 | 3 | 4 |
| C | 6 | 8 | 0 | 1 |
| D | 5 | 7 | 10 | 0 |
Floyd shortest paths
floyd(Graph G)
for k = 0 to G.vertexNum - 1
for i = 0 to G.vertexNum - 1
for j = 0 to G.vertexNum - 1
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])KNOWLEDGE QUIZ
Relax distances and reconstruct a route
Run Dijkstra from S. Show distance and predecessor changes after each settled vertex and draw the final S-to-T path. Then change S–A to 10 and determine whether the shortest route changes.