Greedy Best-First Search
Prerequisites
- Heuristic Functions — h(n) notation, domain knowledge
- Uninformed Search — Priority queues, BFS concept
🎯 Learning Objectives
- Understand the evaluation function f(n) = h(n)
- Write the pseudocode using a priority queue
- Analyze completeness, optimality, time, and space complexity
- Trace Greedy BFS on the Romania map
1. Overview
Greedy Best-First Search expands the node that appears to be closest to the goal according to the heuristic function. Its evaluation function is: f(n) = h(n).
The algorithm uses a priority queue (min-heap) ordered by h(n). At each step, it removes the node with the smallest h-value and expands it. The key idea: always chase the node that “looks” nearest to the goal, ignoring the cost already incurred to reach the current node.
This is called “greedy” because it only considers the estimated future cost (h), completely ignoring the path cost so far (g). It is “best-first” because it selects the best node according to its evaluation function.
2. Pseudocode
function GREEDY-BEST-FIRST-SEARCH(problem):
node = MAKE-NODE(problem.initial_state)
frontier = PRIORITY-QUEUE ordered by h(node)
frontier.INSERT(node)
explored = empty set
while frontier is not empty:
node = frontier.POP() // lowest h(n)
if problem.GOAL-TEST(node.state):
return SOLUTION(node)
explored.ADD(node.state)
for each action in problem.ACTIONS(node.state):
child = CHILD-NODE(problem, node, action)
if child.state not in explored and child not in frontier:
frontier.INSERT(child)
else if child in frontier with higher h:
frontier.REPLACE(child) // update with lower h
return FAILURE
3. Properties
| Property | Greedy Best-First Search |
|---|---|
| Complete? | No (can get stuck in loops in graph version without repeated-state checking; with repeated-state checking, complete in finite spaces) |
| Optimal? | No. It finds a goal quickly but not necessarily via the cheapest path. |
| Time Complexity | O(bm) worst case, but a good heuristic can reduce this dramatically |
| Space Complexity | O(bm) — keeps all nodes in memory |
Where b is the branching factor and m is the maximum depth of the search space.
Greedy BFS ignores path cost g(n). It may find a path to the goal that passes through nodes with low h-values but high actual edge costs, while a cheaper path exists through nodes with higher h-values but lower cumulative cost.
4. Step-by-Step Trace: Romania Map
Goal: Travel from Arad to Bucharest using Greedy BFS with straight-line distance to Bucharest as h(n).
h_SLD values:
Arad=366, Sibiu=253, Timisoara=329, Zerind=374,
Oradea=380, Fagaras=176, Rimnicu Vilcea=193,
Pitesti=100, Bucharest=0, Craiova=160, Giurgiu=77
| Step | Expand | Frontier (node: h) | Action |
|---|---|---|---|
| 0 | — | Arad: 366 | Initialize |
| 1 | Arad | Sibiu:253, Timisoara:329, Zerind:374 | Expand Arad (lowest h=366, only node) |
| 2 | Sibiu | Fagaras:176, RimnicuVilcea:193, Timisoara:329, Zerind:374, Oradea:380 | Expand Sibiu (h=253, lowest) |
| 3 | Fagaras | Bucharest:0, RimnicuVilcea:193, Timisoara:329, Zerind:374, Oradea:380 | Expand Fagaras (h=176, lowest) |
| 4 | Bucharest | — | Goal found! h=0 |
Path found: Arad → Sibiu → Fagaras → Bucharest
Path cost: 140 + 99 + 211 = 450 km
Is this optimal? No! The optimal path is Arad → Sibiu → Rimnicu Vilcea → Pitesti → Bucharest = 418 km. Greedy BFS was “tricked” by Fagaras having a lower h-value than Rimnicu Vilcea.
5. Worked Examples
Consider a graph with nodes S (start), A, B, C, G (goal):
Edges: S→A (cost 1), S→B (cost 5), A→C (cost 3), B→G (cost 2), C→G (cost 6)
Heuristics: h(S)=7, h(A)=5, h(B)=2, h(C)=4, h(G)=0
Greedy BFS trace:
- Frontier: {S:7}. Expand S → children A(h=5), B(h=2)
- Frontier: {B:2, A:5}. Expand B (h=2) → child G(h=0)
- Frontier: {G:0, A:5}. Expand G → Goal found!
Path: S → B → G, cost = 5 + 2 = 7
But path S → A → C → G has cost 1 + 3 + 6 = 10 (more expensive, so Greedy got lucky here).
However, consider if edge S→B cost 50: Greedy still picks B first (h=2), giving cost = 50+2 = 52, while S→A→C→G = 10. Greedy is not optimal.
Graph: S→A (cost 1, h(A)=3), A→B (cost 1, h(B)=2), B→A (cost 1, h(A)=3), B→G (cost 10, h(G)=0).
Without repeated-state detection, after expanding S and reaching A, then B, the algorithm could re-add A (h=3) and oscillate between A and B. With repeated-state detection (explored set), this is avoided and G is eventually found.
Takeaway: Without an explored set, Greedy BFS is not complete — it can loop forever.
5×5 grid. Start = (0,0), Goal = (4,4). h(n) = Manhattan distance. No obstacles.
h(0,0)=8, h(1,0)=7, h(0,1)=7, h(1,1)=6, ... h(4,4)=0
Greedy BFS expands nodes diagonally toward the goal (always picking the neighbor with smallest Manhattan distance). It finds the path in just 8 expansions (moving right or down each step). On a grid with no obstacles, Greedy BFS performs very well because h is highly informative.
If we add a wall across row 2 (blocking direct diagonal path), Greedy might explore many nodes heading toward the wall before backing up, showing its weakness in environments where the heuristic is misleading.
Summary
- Greedy BFS uses f(n) = h(n) — only the heuristic estimate, no path cost.
- Expands the node that appears closest to the goal.
- Not optimal and not complete without repeated-state checking.
- Time and space: O(bm) worst case.
- Can be very fast with a good heuristic but easily misled.
- Foundation for understanding A* (which fixes the optimality problem).
References
- Russell, S. & Norvig, P. Artificial Intelligence: A Modern Approach, 4th Ed. — Section 3.5.1