Sub topic 3
Greedy Best-First Search: Expanding by Estimated Distance
This chapter assumes you can read Python and know how a binary heap acts as a priority queue. It also assumes you have read the previous chapter on uniform-cost search, which ranks nodes by g(n), the cost paid so far.
The idea: chase the goal
Uniform-cost search looks backward. It always expands the node that was cheapest to reach, so it spreads out evenly in every direction, including away from the goal. Greedy best-first search looks forward. It expands the node that seems closest to the goal, using a heuristic h(n). A heuristic is a function that estimates the cost from node n to the goal. On a grid map, a typical h(n) is the straight-line or Manhattan distance to the goal.
Greedy best-first search keeps a priority queue ordered by h(n) alone. It ignores the cost already paid.
Implementation
The structure matches uniform-cost search. Only the priority key changes. A node is recorded the first time it is discovered and is never updated afterwards, because greedy search makes no attempt to find a cheaper route to it.
import heapq
from itertools import count
def greedy_best_first(start, goal, neighbors, h):
tie = count()
frontier = [(h(start), next(tie), start)]
parent = {start: None}
g = {start: 0}
while frontier:
_, _, node = heapq.heappop(frontier)
if node == goal:
path = []
while node is not None:
path.append(node)
node = parent[node]
return path[::-1], g[goal]
for nxt, cost in neighbors(node):
if nxt not in parent:
parent[nxt] = node
g[nxt] = g[node] + cost
heapq.heappush(frontier, (h(nxt), next(tie), nxt))
return None, float('inf')
graph = {
'S': [('A', 1), ('B', 4)],
'A': [('G', 2)],
'B': [('G', 10)],
'G': [],
}
h_values = {'S': 3, 'A': 2, 'B': 1, 'G': 0}
path, cost = greedy_best_first('S', 'G', lambda n: graph[n], lambda n: h_values[n])
print(path, cost) # ['S', 'B', 'G'] 14The g dictionary has no role in the search. It is there only so we can report what the returned path costs.
Where it goes wrong
Here is the trace. The search pops S and pushes A (h=2) and B (h=1). It pops B because B looks closer, discovers G through B, and stops. The path S→B→G costs 14, while S→A→G costs 3.
Every estimate in this graph is at or below the true remaining cost. The heuristic is reasonable. The failure comes from the ranking rule: greedy search never asks how much it has already spent.
Comparing the two strategies
| Property | Uniform-cost search | Greedy best-first |
|---|---|---|
| Priority key | g(n) | h(n) |
| Optimal path | Yes (non-negative costs) | No |
| Typical nodes expanded | Many, in all directions | Few, aimed at the goal |
| Needs a heuristic | No | Yes |
| Worst-case time | O(b^(1+C*/ε)) | O(b^m), m = max depth |
Greedy search is often very fast on open maps, where the straight-line direction really is the best direction. Around obstacles, such as a U-shaped wall, it walks into dead ends that look promising, and the path it returns can be much longer than the optimum.
The two strategies fail in opposite ways. Uniform-cost search is correct but has no sense of direction. Greedy search has direction but no regard for cost. The next chapter combines their keys.
Exercise
In the example graph, change h(B) from 1 to 6 and rerun greedybestfirst. Which path does it return, and what does it cost? Is the new h(B) still a non-overestimate?
Show answer
It returns ['S', 'A', 'G'] with cost 3, because A (h=2) now ranks ahead of B (h=6). The true cost from B to G is 10, so h(B)=6 still does not overestimate. The search got the right answer only because the estimates happened to rank A first, not because greedy search guarantees it.
Exercise
Why does the implementation skip a neighbor that is already in parent, when uniform-cost search would update it if a cheaper g was found?
Show answer
Greedy search ranks nodes by h alone, and h(n) does not depend on the route taken to n. Finding a cheaper route would not change a node's priority, so the algorithm makes no effort to track one. This is one reason its paths are not optimal.