In a weighted graph, each edge has a number, a distance, time or cost. The shortest path problem asks: what's the smallest total to get from one vertex to another? It's how a GPS picks your route.
1. Inspection (small graphs, 4 to 5 vertices): list every path from start to end, add up the weights, pick the smallest total.
2. Dijkstra's algorithm (bigger graphs): build up the confirmed shortest distance to each vertex, one at a time. The golden rule:
Edges: A–B (3), A–C (5), B–D (2), C–D (4). Find the shortest path from A to D.
| Path | Add the weights | Total |
|---|---|---|
| A → B → D | 3 + 2 | 5 |
| A → C → D | 5 + 4 | 9 |
Smallest total is 5, so the shortest path is A → B → D, length 5.
• Add the weights along the path, don't count the number of edges.
• List every path, the most obvious route isn't always the shortest.
• In Dijkstra, always box the smallest tentative distance next, never skip ahead.
• Once a vertex is boxed, its distance is final, don't change it.
The top route (A to B to D) totals 3 + 2 = 5. The bottom route (A to C to D) totals 5 + 4 = 9. Gold wins.
List → add → pick the smallest.
A,B,D = 5 vs A,C,D = 9 → choose 5.
Box the start at 0, then keep boxing the nearest unboxed vertex. The numbers only ever shrink, and a boxed number never changes.
Don't read yet, just have a go in your head:
A,B,D = 3 + 2 = 5. A,C,D = 5 + 4 = 9. Smallest = A,B,D = 5.
A,B,D = 3 + 2 = ?. A,C,D = 5 + 4 = ?. Shortest = ?
Edges A–B(2), A–C(4), B–C(1), B–D(7), C–D(3). Find the shortest path from A to D. (Hint: there are three routes.) Check below.
In one sentence, out loud: how do you find the shortest path on a small graph? If you can say it, you've got it.