Networks as weighted graphs
A graph is a set of nodes joined by edges. In route planning, nodes usually stand for junctions or stations, and edges stand for road or track segments between them. Each edge carries a weight, which is the cost of using it.
The weight can be length, travel time, fuel use, or a toll. Many road networks use directed edges, because one-way streets and turn bans make travel in one direction different from the other.
How Dijkstra's algorithm finds the cheapest path
Dijkstra's algorithm works from the origin outward. It keeps a best-known cost for every node, starting at 0 for the origin and infinity for the rest. It repeatedly picks the unvisited node with the lowest cost, then checks whether going through that node gives a cheaper route to each neighbour.
Once a node is picked, its cost is final. This holds only when no edge weight is negative. With negative weights, the Bellman-Ford algorithm is the usual alternative.
A-star search is a related method. It adds a heuristic estimate of the remaining cost to the destination, such as straight-line distance, so it usually explores fewer nodes. It returns the cheapest path if the estimate never overstates the true remaining cost.
Worked example with four junctions
Take junctions A, B, C and D. The edges and travel times in minutes are A to B 4, A to C 2, C to B 1, B to D 5, and C to D 8. The aim is the quickest route from A to D.
Start with A at 0. Visit A: B becomes 4 and C becomes 2. Visit C, the lowest at 2: B via C costs 2 + 1 = 3, which beats 4, so B becomes 3. D via C costs 2 + 8 = 10. Visit B at 3: D via B costs 3 + 5 = 8, which beats 10, so D becomes 8.
The quickest route is A, C, B, D with a total of 2 + 1 + 5 = 8 minutes. The direct looking route A, B, D would take 4 + 5 = 9 minutes.
Common misconceptions
The shortest path is not always the fastest path. A short road with a low speed limit or heavy congestion can cost more time than a longer motorway. The answer depends on which weight is used.
Another mistake is to think the algorithm tries every route. Dijkstra's algorithm avoids this by fixing the best cost of each node in turn, which is far quicker than listing all paths.
Where route planning appears
Satellite navigation and online map services use shortest path methods, often with extra speed-up techniques for very large road networks, and with travel times updated from current traffic. Delivery firms use them as a building block for vehicle routing, where many stops must be ordered. Data networks use related methods, such as the OSPF protocol, to choose paths for packets.
Rail and air operators also apply them to timetables and flight paths, though extra rules on capacity and timing usually apply. Freight planning links closely to this topic, since containers move across networks of ports, rail lines and roads.
Key terms
- Graph
- A set of nodes joined by edges, used to model a network.
- Edge weight
- The cost of travelling along one edge, such as distance or time.
- Directed edge
- An edge that can be used in one direction only, like a one-way street.
- Dijkstra's algorithm
- A method that finds lowest-cost paths from one origin when no edge weight is negative.
- A-star search
- A variant of Dijkstra's algorithm that uses a heuristic estimate to guide the search toward the destination.
- Heuristic
- An estimate of the remaining cost to the destination, such as straight-line distance.
