Uniform-Cost Search (UCS)
Prerequisites
- BFS — Understanding of level-by-level expansion and optimality for unit costs
- State Space — Node data structure with path-cost g(n)
🎯 Learning Objectives
- Implement UCS using a priority queue ordered by g(n)
- Understand why the goal test is applied at expansion, not generation
- Prove UCS is complete (if step cost ≥ ε > 0) and optimal
- Analyze time/space: O(b1+⌊C*/ε⌋)
- Recognize when UCS reduces to BFS
1. Intuition
BFS is optimal only when all step costs are equal. What if driving from Arad to Sibiu costs 140 km but Arad to Timisoara costs 118 km? BFS would explore both at the same depth, ignoring costs. UCS fixes this by always expanding the node with the lowest path cost g(n)—the cheapest unexpanded node, regardless of depth.
Think of UCS as “BFS on cost contours” rather than depth contours. It explores all nodes with cost < C* before any node with cost = C* (the optimal solution cost).
2. UCS Algorithm
function UNIFORM-COST-SEARCH(problem):
node = Node(problem.INITIAL-STATE, PATH-COST=0)
frontier = PriorityQueue(node, key=PATH-COST) // min-heap
explored = {}
loop:
if frontier is empty: return FAILURE
node = frontier.POP-MIN() // lowest g(n)
if problem.GOAL-TEST(node.STATE): return SOLUTION(node)
add node.STATE to explored
for each child in EXPAND(problem, node):
if child.STATE not in explored AND child.STATE not in frontier:
frontier.INSERT(child)
else if child.STATE in frontier with higher PATH-COST:
frontier.REPLACE(child) // decrease-key
Unlike BFS, UCS must test for goals when a node is removed from the frontier (expanded), NOT when it is generated. Why? Because a cheaper path to the goal may still be in the frontier when the goal is first generated via a more expensive route.
3. Step-by-Step Trace
Weighted graph:
S --1-- A --3-- G
| |
4 1
| |
B ------5------/
Edges: S→A(1), S→B(4), A→G(3), B→G(5). Start: S, Goal: G.
| Step | Pop (g) | Frontier [state, g] | Explored |
|---|---|---|---|
| 0 | — | [S:0] | {} |
| 1 | S (0) | [A:1, B:4] | {S} |
| 2 | A (1) | [G:4, B:4] | {S, A} |
| 3 | G (4) or B(4) | Pop min: tie-break. Suppose G popped first. | — |
| — | G (4) | GOAL TEST → SUCCESS! | {S, A, G} |
Path: S → A → G, cost = 1 + 3 = 4. Optimal! (S→B→G would cost 4+5=9.)
Why Goal Test at Expansion Matters
Modified graph: S→A(1), S→G(10), A→G(2). If we tested at generation:
- Expand S: generate A(1) and G(10). If we goal-test G here → returns S→G cost=10.
- But the optimal path is S→A→G cost=3!
UCS waits until G is the minimum-cost node in the frontier. G(10) stays in the frontier while A(1) is expanded first, generating G(3). The frontier now updates G from 10 to 3. Then G(3) is popped and the optimal solution is found.
4. Properties of UCS
| Property | Value | Explanation |
|---|---|---|
| Complete? | Yes, if step cost ≥ ε > 0 | Every step adds at least ε to the cost. UCS will eventually explore all costs up to C*. Fails with zero-cost actions (infinite loop). |
| Optimal? | Yes | UCS always expands the cheapest unexpanded node first. Any path it returns is guaranteed to have the minimum total cost. |
| Time | O(b1+⌊C*/ε⌋) | C* = optimal solution cost, ε = minimum step cost. This can be much larger than O(bd) if step costs are small. |
| Space | O(b1+⌊C*/ε⌋) | Same as time—all generated nodes may be stored. |
5. Connection to Dijkstra’s Algorithm
UCS is essentially Dijkstra’s shortest-path algorithm adapted for search problems:
- Both use a priority queue ordered by path cost.
- Both expand the cheapest node first.
- Both are optimal for non-negative edge weights.
- Difference: Dijkstra’s finds shortest paths to all nodes. UCS stops when the goal is expanded.
6. When UCS Reduces to BFS
If all step costs are equal (say, all = 1), then g(n) = depth(n). The priority queue orders by depth, which is the same as a FIFO queue. Thus, UCS with unit costs = BFS. BFS is a special case of UCS.
7. More Worked Examples
Example 2: UCS on Romania Sub-Graph
Arad→Sibiu(140), Arad→Zerind(75), Arad→Timisoara(118), Zerind→Oradea(71), Sibiu→Fagaras(99), Fagaras→Bucharest(211), Sibiu→RV(80), RV→Pitesti(97), Pitesti→Bucharest(101).
UCS from Arad to Bucharest:
- Expand Arad(0) → Zerind(75), Timisoara(118), Sibiu(140)
- Expand Zerind(75) → Oradea(146)
- Expand Timisoara(118) → [no new useful nodes towards Bucharest]
- Expand Sibiu(140) → Fagaras(239), RV(220)
- Expand Oradea(146) → [dead end for this example]
- Expand RV(220) → Pitesti(317)
- Expand Fagaras(239) → Bucharest(450)
- Expand Pitesti(317) → Bucharest(418). Update: 418 < 450, so replace Bucharest in frontier.
- Expand Bucharest(418) → GOAL! Path: Arad→Sibiu→RV→Pitesti→Bucharest, cost=418.
Example 3: Zero-Cost Problem
If some edges have cost 0: S→A(0), A→B(0), B→A(0)… UCS can loop forever between A and B with cost 0. This is why the requirement is step cost ≥ ε > 0.
Summary
- UCS uses a priority queue ordered by path cost g(n).
- Goal test at expansion, not generation (critical for optimality).
- Complete if step cost ≥ ε > 0. Optimal always (with this condition).
- Time/Space: O(b1+⌊C*/ε⌋).
- Equivalent to Dijkstra’s algorithm that stops at the goal.
- UCS reduces to BFS when all step costs are equal.
References
- Russell & Norvig, AIMA, Chapter 3.4.2.