A* search
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Dijkstra's algorithm finds the shortest route by searching outward in every direction. A* adds a guess of the distance left, so it heads for the goal and leaves most of the map alone. Watch it on a 4 by 5 grid with a wall: every heap entry carries f = g + h, and the search pops 9 cells where Dijkstra's algorithm pops all 18. The loop is eight lines of Python with heapq. The lesson "Dijkstra's algorithm" covers the heap-based loop this lesson starts from, and "Multi-source and 0-1 BFS on grids" covers grids as graphs.
Find the shortest route
Start at S and reach G. Each move goes up, down, left or right and costs 1. The tall blocks are walls. Dijkstra's algorithm would find the shortest route, but it searches in every direction. A* aims at G.
grid: 4 rows x 5 columns, (row, column) from the top left . # . . . . # . . . . . . . . . . . . . S = (0,0), G = (0,4) each move: 1 step, up / down / left / right open cells: 18, walls: 2
اقرأ الدرس خطوة بخطوة
Find the shortest route
Start at S and reach G. Each move goes up, down, left or right and costs 1. The tall blocks are walls. Dijkstra's algorithm would find the shortest route, but it searches in every direction. A* aims at G.
grid: 4 rows x 5 columns, (row, column) from the top left . # . . . . # . . . . . . . . . . . . . S = (0,0), G = (0,4) each move: 1 step, up / down / left / right open cells: 18, walls: 2h: a guess of moves left
Give each cell a guess h of the moves still needed: rows apart plus columns apart, ignoring walls. This is the Manhattan distance. S is 4 from G, so h(S) = 4. Walls can only make the real route longer, never shorter.
h(v) = |row - row of G| + |col - col of G| 4 # 2 1 0 5 # 3 2 1 6 5 4 3 2 7 6 5 4 3 walls (#) are ignored by the guess h(S) = 4, h(G) = 0The score: f = g + h
g counts the moves made from S; h guesses the rest. Their sum f estimates a route through the cell. S has g = 0 and h = 4, so (4, S) goes into a heap, a priority queue that hands out the smallest f.
f(v) = g(v) + h(v) push (0,0) g=0 h=4 f=4 heap: [(4, (0,0))]Pop the smallest f
heappop takes out the entry with the smallest f: S. Its only open neighbour is the cell below. That move leads away from G, so g rises by 1 and h rises by 1: f = 1 + 5 = 6.
pop (0,0) g=0 h=4 f=4 push (1,0) g=1 h=5 f=6 heap: [(6, (1,0))] a move away from G: g + 1, h + 1, so f + 2Around the wall, f climbs
The wall pushes the search down. Each move away from G adds 2 to f: the next cell down gets 8, the corner below it 10. The cell right of the 8 is a move toward G, so it also gets 8.
pop (1,0) g=1 h=5 f=6 push (2,0) g=2 h=6 f=8 pop (2,0) g=2 h=6 f=8 push (3,0) g=3 h=7 f=10 push (2,1) g=3 h=5 f=8 heap: [(8, (2,1)) (10, (3,0))]Toward G, f stays flat
A move toward G adds 1 to g and takes 1 from h, so f stays 8. A* keeps popping 8s and walks along the open row. The cells scored 10 lead away from G; they wait at the back of the heap.
pop (2,1) g=3 h=5 f=8 push (3,1) g=4 h=6 f=10 push (2,2) g=4 h=4 f=8 pop (2,2) g=4 h=4 f=8 push (1,2) g=5 h=3 f=8 push (3,2) g=5 h=5 f=10 push (2,3) g=5 h=3 f=8 heap: [(8, (1,2)) (8, (2,3)) (10, (3,0)) (10, (3,1)) (10, (3,2))] a move toward G: g + 1, h - 1, so f stays the sameStop when G is popped
Up the far side, the 8s lead to G. A* stops when G comes out of the heap, not when it goes in. g(G) = 8: the shortest route takes 8 moves. A* popped only 9 of the 18 open cells.
pop (1,2) g=5 h=3 f=8 push (0,2) g=6 h=2 f=8 push (1,3) g=6 h=2 f=8 pop (0,2) g=6 h=2 f=8 push (0,3) g=7 h=1 f=8 pop (0,3) g=7 h=1 f=8 push (0,4) g=8 h=0 f=8 pop (0,4) g=8 h=0 f=8 heap: [(8, (1,3)) (8, (2,3)) (10, (3,0)) (10, (3,1)) (10, (3,2))] return g[G] = 8 route: (0,0) (1,0) (2,0) (2,1) (2,2) (1,2) (0,2) (0,3) (0,4)Dijkstra pops all 18
Set h = 0 and the same code is Dijkstra's algorithm: f is just g. It pops cells in order of g: all 18 open cells, G last. The dashed cells are the 9 extra pops that A* skipped.
h = 0 (Dijkstra): pop order by g (0,0) g=0 (1,0) g=1 (2,0) g=2 (2,1) g=3 (3,0) g=3 (2,2) g=4 (3,1) g=4 (1,2) g=5 (2,3) g=5 (3,2) g=5 (0,2) g=6 (1,3) g=6 (2,4) g=6 (3,3) g=6 (0,3) g=7 (1,4) g=7 (3,4) g=7 (0,4) g=8 pops: Dijkstra 18, A* 9Never guess too high
Manhattan distance never guesses more than the real moves left: it is admissible. So once G is popped, nothing left in the heap can lead to a shorter route. With a guess that can overshoot, A* no longer promises the shortest route.
admissible: h(v) <= true moves from v to G, for every v cell h true (0,0) 4 8 (0,2) 2 2 (0,3) 1 1 (0,4) 0 0 (1,0) 5 7 (1,2) 3 3 (1,3) 2 2 (1,4) 1 1 (2,0) 6 6 (2,1) 5 5 (2,2) 4 4 (2,3) 3 3 (2,4) 2 2 (3,0) 7 7 (3,1) 6 6 (3,2) 5 5 (3,3) 4 4 (3,4) 3 3 when G is popped, every entry left has f >= g(G)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01A cell is 3 moves from S along the best route found so far, and its Manhattan distance to G is 4. What is its f?
f = g + h = 3 + 4 = 7. g counts the moves already made; h guesses the moves still to come.
02On a grid where each move costs 1, A* moves from a cell to a neighbour that is one step closer to G. What happens to f?
g goes up by 1 and h goes down by 1, so f is unchanged. A move away from G raises both, so f goes up by 2.
03When does A* stop with the shortest route to G?
A push only means some route to G is known. When G comes out of the heap, every entry left has f at least g(G), and with an admissible h none of them can lead to a shorter route.
04What does the same code do if h(v) always returns 0?
With h = 0, f is just g and the loop is Dijkstra's algorithm. In this lesson's grid it still finds 8 moves, but after 18 pops instead of 9.
05Why is Manhattan distance a safe guess on this grid?
A guess that never overestimates is called admissible, and it keeps A* exact. Walls can make the real route longer than the guess, never shorter.
المسرد
- A* search
- A shortest-path search that pops the entry with the smallest f = g + h, so it explores toward the goal first.
- g(v)
- The number of moves (or the cost) of the best known route from the start S to v.
- Heuristic, h(v)
- A guess of the cost still needed from v to the goal G. Here it is the Manhattan distance.
- f(v)
- g(v) + h(v): an estimate of the cost of a whole route from S to G through v.
- Manhattan distance
- Rows apart plus columns apart. On a grid with four move directions it is the fewest moves when there are no walls.
- Admissible
- A heuristic that never guesses more than the true remaining cost. With one, A* returns a shortest route.
- Consistent
- A heuristic with h(v) ≤ cost(v, w) + h(w) for every move from v to w. Then a cell's g is final when it is popped.
- Priority queue (heap)
- A collection that always hands out its smallest item next. Python's heapq keeps one in a plain list.
الملاحظات والمصادر
- Several entries can share the smallest f. The heap holds (f, cell) tuples, and Python compares tuples item by item, so a tie goes to the cell with the smaller (row, column). A different tie rule pops other cells but finds a route of the same length.
- The tray shows the entries in the order heappop would return them. Inside, heapq keeps a binary heap in a plain list, which is only partly sorted; heap[0] is always the smallest.
- Manhattan distance is also consistent: for neighbours v and w, h(v) ≤ 1 + h(w). With a consistent guess, a cell's g is final when it is popped, just as in Dijkstra's algorithm.
- MIT 6.006 explains A* as Dijkstra's algorithm on changed edge weights w′(u, v) = w(u, v) − h(u) + h(v). Every route from S to G changes by the same amount, so shortest routes stay shortest, and every w′ is ≥ 0 exactly when h is consistent.
- The card returns only the length. To get the route itself, also store came[w] = v whenever g[w] improves, then follow came back from G to S.
- The code is real Python 3: the full program prints 8. With h(v) returning 0 it is Dijkstra's algorithm and still prints 8, after 18 pops instead of 9.
- MIT 6.006 Introduction to Algorithms (Fall 2011), Lecture 18: Speeding up Dijkstra (goal-directed search, A*)
- Sedgewick and Wayne, Algorithms 4th ed., §4.4 Shortest Paths
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), §22.3 Dijkstra's algorithm and ch. 6 Heapsort
- Amit Patel, Amit's A* Pages: Heuristics (hosted at theory.stanford.edu)
- Python standard library: heapq, heap queue algorithm
اختبار يتاح بعد الخطوة الأخيرة.