Search Algorithms Lecture
Search, Learning, and Efficient Search Algorithms
Review of Search Algorithms
The lecture begins with a review of fundamental search algorithms using a city-block problem. The problem involves traveling from City 1 to City n moving only forward, then returning from City n to City 1 moving only backward, with costs cij≥0 between cities.
Depth-First Search (DFS)
DFS assumes all edge costs are zero, allowing termination upon finding any solution.
Since cij≥0 and not necessarily zero, DFS cannot be used here.
Breadth-First Search (BFS)
BFS requires all edge costs to be identical (e.g., unit costs) because it explores level by level.
The costs cij are not guaranteed equal, so BFS is not applicable.
Dynamic Programming (DP)
Dynamic programming works with any non-negative costs and no cycles in the state space.
Although the forward-backward trip appears to create a cycle, it can be eliminated by extending the state to include both city and direction (e.g., "City 4 going forward" vs. "City 4 going backward"). This breaks the cycle, making DP valid.
DP is suitable here.
Uniform Cost Search (UCS)
Uniform cost search handles cycles and requires non-negative costs. Since cij≥0, UCS is applicable.
UCS explores states in order of increasing path cost (past cost).
Important: Dynamic programming and uniform cost search are both viable, whereas DFS and BFS are not due to cost constraints.
State: A Key Concept
A state is a summary of all past actions that is sufficient to choose future actions optimally.
In the city problem, the state must include both the current city and the direction of travel (forward or backward) to avoid cycles and ensure optimal planning.
This concept is crucial for avoiding exponential blow-up in search, as it enables memoization.
Uniform Cost Search: Implementation and Correctness
Data Structure: Priority Queue
UCS uses a priority queue (min-heap) to manage the frontier. Key operations:
update(state, cost): adds a state with given cost or updates if a better cost is found.removeMin(): removes and returns the state with the smallest cost.
Pseudocode
Initialize
frontieras priority queue containing start state with cost 0.While frontier is not empty:
Pop the state
swith minimal past cost from frontier.If
sis an end state, return the cost.For each action
afroms:Compute new state
s'and costcost(s,a).New path cost = past cost +
cost(s,a).If the new cost is better than any previously recorded cost for
s', update frontier.
This process moves states from unexplored → frontier → explored.
Correctness Theorem
If a state s is popped from the frontier (moved to explored set), its priority (past cost) is the minimum possible cost to reach s. Proof sketch:
Assume there exists a cheaper path to
s(the green path) that leaves the explored set at some statet, goes tou, then tos.The cost of the green path ≥
priority(t) + cost(t,u)≥priority(u)(sinceuis in frontier and its priority ≤ that sum).Since
sis popped next,priority(u) ≥ priority(s).Hence the green path cost ≥
priority(s)(the black path cost), contradicting the assumption of a cheaper path.
Thus, UCS returns the optimal solution.
Complexity
UCS: O(ElogV) where V is number of explored states, E is edges. The log factor comes from priority queue operations.
DP: O(V) (linear in number of states), but cannot handle cycles.
Comparing Dynamic Programming and Uniform Cost Search
Feature | Dynamic Programming | Uniform Cost Search |
|---|---|---|
Cycles | Not allowed | Allowed |
Cost sign | Any (positive, negative, zero) | Non-negative required |
Complexity | O(n) | O(nlogn) |
Use case | Acyclic graphs with arbitrary costs | Cyclic graphs with non-negative costs |
For graphs with negative costs and cycles, algorithms like Bellman-Ford exist (not covered in this course).
Learning: The Inverse Problem of Search
In many real-world scenarios, the cost function of a search problem is unknown, but we may have access to optimal paths (e.g., human demonstrations). The goal is to learn the cost weights from these demonstrations.
Problem Setup
Input: Search problem without costs (only actions defined).
Output: Optimal path (sequence of actions).
Goal: Find cost weights w such that the demonstrated path is optimal under those weights.
Example: Transportation Problem (Walk or Tram)
Actions:
walk(cost w1) andtram(cost w2).True costs: w1=1, w2=2.
Training data: optimal paths generated using true costs (e.g., path for n blocks using DP).
Learning algorithm: Structured perceptron.
Structured Perceptron Algorithm
Initialize weights w to zero.
For each training example (input state, true path y):
Compute prediction y′ = optimal path under current weights (using DP or UCS).
If y′=y (mistake):
For each action in true path y: decrease its weight by 1 (to favor it).
For each action in predicted path y′: increase its weight by 1 (to disfavor it).
Repeat until no mistakes (or for a fixed number of iterations).
Walkthrough
Initial weights: walk=3, tram=2.
True path (for a small example): walk, walk, walk.
Predicted path under current weights: walk, tram (costs 3+2=5 vs 3+3+3=9).
Update: subtract 1 from walk for each walk in true path → walk becomes 3-3=0. Then add 1 for each action in predicted path: add 1 for walk (0→1) and add 1 for tram (2→3).
New weights: walk=1, tram=3.
Re-run prediction: walk, walk, walk cost=3; walk, tram cost=1+3=4 → prediction matches true path.
No more mistakes; algorithm converges.
Note: The algorithm may not recover exact true weights but finds weights that reproduce the training data (e.g., walk=1, tram=3 is acceptable if only walk paths appear in data).
Generalization: Feature-Based Costs
Cost can depend on state-action features: cost(s,a)=w⋅ϕ(s,a).
Cost of a path: w⋅∑ϕ(edges).
Update rule: w←w+∑ϕ(predicted path)−∑ϕ(true path).
This generalized algorithm is known as Collins' algorithm (used in NLP for part-of-speech tagging, machine translation, etc.).
A* Search
Motivation
Uniform cost search explores uniformly in all directions, wasting effort on states far from the goal. A* directs search toward the goal using a heuristic estimate of remaining cost.
Key Idea
Past cost g(s): cost from start to s.
Future cost h∗(s): true optimal cost from s to goal (unknown).
Heuristic h(s): estimate of h∗(s).
A* orders states by f(s)=g(s)+h(s) instead of just g(s), guiding search toward promising directions.
Implementation
A* is uniform cost search with modified edge costs. Define new edge cost:
where s′ is the successor state. Then run UCS using cost'.
Example
Graph: A—B—C—D—E, all edges cost 1, start at C, goal at E. Heuristic values: h(A)=4, h(B)=3, h(C)=2, h(D)=1, h(E)=0.
Original UCS explores: C → B,D → A,E then reaches E (explores 5 states).
With heuristic, new costs: C→B: 1+3-2=2; C→D: 1+1-2=0.
UCS with new costs explores only C→D→E (explores 3 states).
Properties of Heuristics
Consistency
A heuristic is consistent if:
Triangle inequality: cost(s,a)+h(s′)−h(s)≥0 for all s,a (new costs non-negative).
Goal condition: h(sgoal)=0.
Consistency ensures A* is correct (returns optimal solution) because it is equivalent to UCS on non-negative transformed costs.
Why Consistency Leads to Correctness
Summing transformed costs along a path from start to goal:
Since h(sgoal)=0, minimizing transformed costs is equivalent to minimizing original costs (shifted by constant). UCS on transformed costs finds optimal path.
Admissibility
A heuristic is admissible if h(s)≤h∗(s) (never overestimates future cost). Consistency implies admissibility, so admissible heuristics are often obtained from consistency.
Efficiency
A* is more efficient than UCS: it explores states with g(s)<g(sgoal)−h(s) (assuming consistency). Larger h(s) (closer to true future cost) yields greater efficiency. UCS is A* with h(s)=0.
Designing Heuristics via Relaxation
General Principle
To obtain a heuristic, relax constraints of the original problem to make it easier. The optimal cost of the relaxed problem provides an estimate h(s) for the original.
Examples
Grid with walls: Original problem has walls blocking direct paths. Relaxation: remove all walls. Heuristic: Manhattan distance (closed-form solution).
Tram problem with constraint: "Cannot have more tram actions than walk actions." Relaxation: remove constraint. Heuristic: optimal cost from the unconstrained problem (easier to compute via DP or UCS for all states).
8-puzzle: Original problem forbids tile overlap. Relaxation: allow tiles to overlap. Heuristic: sum of Manhattan distances for each tile (eight independent subproblems, closed-form).
Engineering Considerations
To compute h(s) for all states, one may need to solve the relaxed problem on a reversed graph (e.g., compute future costs from goal backward using DP or UCS on a reverse problem).
Relaxation reduces infinite-cost edges (constraints) to finite costs, making the relaxed problem tractable.
Summary
Search algorithms range from tree search (DFS, BFS) to graph search (UCS, DP) with trade-offs in handling cycles and cost signs.
State design is critical for efficient and correct search.
Learning costs from optimal paths can be done via structured perceptron, updating weights to match demonstrated behavior.
A* accelerates search by incorporating heuristic estimates, provided the heuristic is consistent (and thus admissible).
Heuristics are typically derived by relaxing problem constraints, yielding simpler problems with known solutions (e.g., Manhattan distance, unconstrained DP).

Ask anything
We've read this whole study set, so questions about it get the best answers.