Greedy Best-First Search
This is the first informed (heuristic) search algorithm in the course. Unlike Module 1's blind strategies, informed search uses problem-specific knowledge - a heuristic function h(n) - to estimate how close a state is to the goal, allowing the search to prioritize promising directions.
h(n) is an estimate of the cost of the cheapest path from node n to a goal state. It encodes domain knowledge in a single number - the smaller h(n), the closer n is believed to be to the goal.
Greedy Best-First Search expands the node that appears closest to the goal, by always selecting the node in the frontier with the lowest h(n) value. It is "greedy" because it ignores the cost already spent reaching n (g(n)) and looks only at the estimated remaining cost.
Worked Example - Romania Map Problem
Using straight-line distance to Bucharest as h(n): from Arad, the agent greedily picks the neighbor with smallest h (e.g., Sibiu, h=253), then from Sibiu picks Fagaras (h=176) over Rimnicu Vilcea (h=193), then Fagaras→Bucharest. This finds a solution fast (Arad-Sibiu-Fagaras-Bucharest, 450 km) but it is not the optimal 418 km route via Rimnicu Vilcea & Pitesti - illustrating Greedy's key weakness.
Fig 2.1 - Greedy always expands the lowest-h node; picks Fagaras (176) over the actually-cheaper route through Rimnicu Vilcea (193)
import heapq
def greedy_best_first(graph, start, goal, h):
frontier = [(h[start], start, [start])]
visited = set()
while frontier:
_, node, path = heapq.heappop(frontier)
if node == goal:
return path
if node in visited:
continue
visited.add(node)
for neighbor, _cost in graph.get(node, []):
if neighbor not in visited:
heapq.heappush(frontier, (h[neighbor], neighbor, path + [neighbor]))
return None
Properties
| Property | Result | Explanation |
|---|---|---|
| Completeness | No (in general); Yes in finite spaces with repeated-state checking | Can follow a misleading h(n) into an infinite or dead-end path |
| Optimality | No | Ignores accumulated cost g(n) entirely - can settle for an expensive path that merely "looked close" |
| Time Complexity | O(bm) worst case | Good heuristic can make this close to O(bm) in practice |
| Space Complexity | O(bm) | Stores frontier, same concern as BFS/UCS |
✓ Advantages
- Often very fast in practice with a good heuristic - explores far fewer nodes than uninformed search
- Simple to implement once h(n) is defined
- Useful when a quick, "good enough" solution is preferred over the optimal one
✗ Disadvantages
- Not optimal - can be misled by an inaccurate heuristic
- Not complete in infinite spaces without cycle checking
- Behaves like poorly-guided DFS in the worst case
Applications
- Pathfinding in video games where near-optimal (not perfect) routes are acceptable
- Web crawling prioritized by relevance score
- Quick approximate solutions in robotics navigation
- f(n) = h(n) only - ignores path cost so far.
- Fast but not optimal and not complete in general - A* (next topic) fixes this.
Because it selects nodes based solely on the estimated remaining cost h(n), completely disregarding the cost g(n) already incurred to reach that node. This can lead it to commit early to a path that "looks" close to the goal but turns out to have a higher total cost than an alternative the algorithm overlooked.