AI Beyond Language Models · Search and Games

A* Search: Finding the Shortest Path

Maps, games and robots all need to find routes. A* always explores the most promising place next: the distance so far plus a guess of the distance left.

a_star.py

import heapq

# Find the shortest way to school. A* tries the most promising place first:
# distance so far + a guess of the distance left.
roads = {"home": {"park": 2, "shop": 5}, "park": {"shop": 2, "school": 6}, "shop": {"school": 2}, "school": {}}
guess = {"home": 5, "park": 4, "shop": 2, "school": 0}   # straight-line distance to school

frontier = [(guess["home"], 0, "home", ["home"])]
while frontier:
    estimate, cost, place, path = heapq.heappop(frontier)
    if place == "school":
        break
    for nxt, dist in roads[place].items():
        heapq.heappush(frontier, (cost + dist + guess[nxt], cost + dist, nxt, path + [nxt]))
print(path, "length", cost)

Step by step

  1. heapq keeps a list ordered so the smallest item can always be taken first.
  2. The road map: from each place, which places you can drive to, and how far.
  3. A guess of the distance left from each place to school, like a straight line on a map.
  4. The frontier: places waiting to be explored, each stored as (cost so far + guess, cost so far, place, path). It starts with home.
  5. Python checks frontier to decide whether the loop keeps going.
  6. Take the most promising entry: home (0 + 5).
  7. Python checks place == "school" to decide which way the code goes.
  8. nxt is created with the value 'park'. dist is created with the value 2.
  9. From home, drive to the park: cost 2, guess 4, estimate 6.
  10. nxt changes from 'park' to 'shop'. dist changes from 2 to 5.
  11. Or to the shop directly: cost 5, guess 2, estimate 7.
  12. Python moves to the next item of the loop: for nxt, dist in roads[place].items().
  13. Python checks frontier to decide whether the loop keeps going.
  14. The park has the lowest estimate (6), so explore it next.
  15. Python checks place == "school" to decide which way the code goes.
  16. dist changes from 5 to 2.
  17. Park → shop: cost 4 (cheaper than the direct road's 5), estimate 6.
  18. nxt changes from 'shop' to 'school'. dist changes from 2 to 6.
  19. Park → school: cost 8, estimate 8. School is reached, but not yet taken off the frontier.
  20. Python moves to the next item of the loop: for nxt, dist in roads[place].items().
  21. Python checks frontier to decide whether the loop keeps going.
  22. The shop via the park (estimate 6) comes out first.
  23. Python checks place == "school" to decide which way the code goes.
  24. dist changes from 6 to 2.
  25. Shop → school: cost 6, estimate 6.
  26. Python moves to the next item of the loop: for nxt, dist in roads[place].items().
  27. Python checks frontier to decide whether the loop keeps going.
  28. The cheapest entry now is school, at cost 6.
  29. The goal came off the frontier: this is the shortest route.
  30. Line runs: break.
  31. home → park → shop → school, length 6, beating the obvious home → park → school (8). A* checked only a few routes because the guess kept it heading the right way.