Sub topic 11
When Moves Have Different Costs
This chapter assumes you can write a breadth-first search (BFS) on a grid, use a neighbour function, and read a Python dictionary and a queue. Here you will see why BFS stops being correct once moves cost different amounts, and how to model those costs.
Why BFS is not enough
BFS counts edges. It finds the path with the fewest moves because it explores cells in order of step count. That is only the cheapest path when every move costs the same.
Suppose a grid has road cells and mud cells. Entering a road cell costs 1. Entering a mud cell costs 5. A path of 3 mud cells has fewer moves than a path of 6 road cells, but it costs 15 instead of 6. BFS returns the 3-move path, which is the wrong answer to "what is cheapest?".
Putting costs on edges
A weighted graph gives each edge a number, its weight or cost. For grids there are two common ways to store this.
- Cost of entering a cell. Each cell holds a number, and moving into it pays that number. A wall is marked with
None. This is the model the Dijkstra example in this tutorial uses. - Cost per edge. Each pair of neighbouring cells has its own cost, for example a steep slope going uphill but not downhill. Store these in a dictionary keyed by
(from_cell, to_cell), or compute them in a function.
With entering-cost, the cost of a path is the sum of the costs of every cell after the start. The start cell is free because you are already standing on it.
cost = [
[1, 1, 5, 1],
[1, 9, 5, 1],
[1, 1, 1, 1],
]
def path_cost(cost, path):
return sum(cost[r][c] for r, c in path[1:])
print(path_cost(cost, [(0, 0), (0, 1), (0, 2), (0, 3)]))
print(path_cost(cost, [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2), (2, 3), (1, 3), (0, 3)]))The first path has 3 moves and costs 1 + 5 + 1 = 7. The second has 7 moves and costs 1 + 1 + 1 + 1 + 1 + 1 + 1 = 7. Both cost 7, although one has more than twice as many moves.
Diagonal moves and costs
With 8-direction movement, a diagonal step covers more distance than a straight step. If you give both the same cost, the model treats a diagonal as free extra reach. A common fix is to charge about 1.414 (the square root of 2) for diagonals when straight moves cost 1. If your cost is "entering a cell", decide deliberately whether a diagonal multiplies it.
Choosing the algorithm
The next chapter covers Dijkstra's algorithm itself. The choice rule is simple.
| Situation | Use |
|---|---|
| All moves cost the same | BFS |
| Moves have different non-negative costs | Dijkstra |
| Different costs and a good distance estimate to the goal | A* |
BFS is cheaper than Dijkstra, because a plain queue is faster than a heap. If all costs are equal, do not pay for the heap.
Common errors
- Counting moves when the question asks for cost. Check what the answer is measured in.
- Charging the cost of the start cell. Decide once and keep it consistent.
- Using
0for walls. A cost of 0 is a valid free cell, so useNoneor a separate wall marker. - Using BFS on weighted edges and trusting the result.
Exercise
A grid has two routes from start to goal. Route A has 4 moves through cells costing 3, 3, 3, 3. Route B has 6 moves through cells costing 1, 1, 1, 1, 1, 1. Which route does BFS return, which route is cheapest, and what are the two costs?
Show answer
BFS returns Route A because it has fewer moves. Route B is cheapest. Route A costs 12 and Route B costs 6.