π Shortest path
The rule
- A weighted network has a number on each edge. It can be a distance, a time or a cost.
- The length of a route is the sum of the numbers along it.
- The shortest path from A to B is the route with the smallest total.
- Fewest edges does not mean shortest. Always add the numbers.
- Taking the cheapest next edge each time can trap you in a dear road later. List the routes.
- List routes in order: leave A along each edge in turn, and never visit a dot twice.
- Write each total as a sum, like 3 + 4 + 5 = 12, so you can check it.
Examples
Find the shortest path from A to C. β 8
- A β B β C: 5 + 3 = 8
- A β D β C: 6 + 4 = 10
- The smallest is 8
Add along each route. The smaller total wins.
Find the shortest path from A to C. Does the cheapest first road win? β 7
- A β B β C: 1 + 8 = 9
- A β D β C: 3 + 4 = 7
- The cheapest first road (1) gave 9, not the shortest
- The smallest is 7
The cheapest first road leads on to a dear one.
Find the shortest path from A to E. β 9
- A β E direct: 10
- A β B β C β D β E: 2 + 2 + 3 + 2 = 9
- More stops, but less total
- The smallest is 9
Fewer stops, but a bigger total.
Find the shortest path from A to E. β 12
- A β C β E: 3 + 10 = 13
- A β C β D β E: 3 + 4 + 5 = 12
- A β B β C β E: 7 + 2 + 10 = 19
- A β B β C β D β E: 7 + 2 + 4 + 5 = 18
- The smallest is 12
List every route and add each one.
Home is A and school is C. Via the shop B it is 2 km then 6 km. Via the park D it is 4 km then 3 km. Which way is shorter? β Via the park, 7 km
- Via the shop: 2 + 6 = 8 km
- Via the park: 4 + 3 = 7 km
- The park way is shorter: 7 km
Add each way. 7 is less than 8.
Watch out
- βFewest roads is quickest.β No: add the numbers. More roads can add up to less.
- βTake the cheapest road each time.β No: it can lead to a dear road later. Check every route.
- Missing a route or adding a wrong number. Write the sum, like 3 + 4 + 5 = 12.