Artificial IntelligenceUnit 314 min read
Search Algorithms: Trees, States, and Optimal Paths
Unit 3 of Artificial Intelligence explores systematic problem-solving techniques using search algorithms—how agents explore state spaces, game trees, and graphs to find solutions efficiently, with visual traces of explored paths, costs, and optimizations.
TAKEAWAYS:
- Search spaces are modeled as trees/graphs where nodes represent states and edges represent actions; state spaces grow exponentially with problem complexity.
- Uninformed (blind) search (BFS, DFS, UCS) explores without heuristics, while informed search (Greedy, A*) uses heuristics to guide efficiency.
- Game trees alternate between player moves (minimax with alpha-beta pruning) to predict optimal strategies.
- Costs (path length, branching factor) determine algorithm choice; trade-offs exist between completeness, optimality, and time/space complexity.
- Real-world applications include pathfinding (Pathao delivery routes), game AI (chess engines), and automated planning (robotics).
- Exam focus: Trace search steps visually, compare algorithms via tables, and justify choices for given problems (e.g., "Why BFS over DFS for a maze?").
1. Search Problems: States, Actions, and Goals
A search problem defines how an agent solves a problem by moving through a state space:
- Initial state: Starting configuration (e.g., robot at
(0,0)). - Actions: Legal moves (e.g.,
UP,DOWN,LEFT,RIGHT). - Transition model: How actions change states (e.g.,
UPfrom(0,0)→(0,1)). - Goal test: Checks if a state is the solution (e.g., reach
(3,3)). - Path cost: Numerical value of traversing an edge (e.g., time/distance).
Visual: State Space as a Graph
Example: A robot in a grid must navigate from (0,0) to (3,2) avoiding obstacles (e.g., (2,0) is blocked). The state space above shows all possible positions.
2. Search Algorithms: Uninformed vs. Informed
Algorithms differ in how they explore the space. Uninformed search uses no problem-specific knowledge; informed search uses heuristics.
A. Uninformed Search
| Algorithm | Expands Nodes Level-by-Level | Uses Stack | Time Complexity (Branching b, Depth d) | Space Complexity | Optimal? | Complete? |
|---|---|---|---|---|---|---|
| Breadth-First (BFS) | ✅ Yes | ❌ Queue | ✅ (if uniform cost) | ✅ | ||
| Depth-First (DFS) | ❌ No (depth-first) | ✅ Stack | (m = max depth) | ❌ | ❌ (unless finite) | |
| Uniform Cost (UCS) | ✅ Yes | ❌ Priority queue (by cost) | (c = min cost) | ✅ | ✅ |
Worked Example: BFS for a Maze
Problem: Find the shortest path from S to G in this grid (1 = wall, 0 = path):
S 0 0 0
0 1 0 0
0 0 0 0
0 0 0 G
Steps:
- Start at
S, explore neighbors:(1,0),(1,1)(blocked),(0,1). - Enqueue
(1,0)and(0,1)withcost=1. - Dequeue
(1,0), explore(2,0),(1,1)(blocked),(0,0)(visited). - Enqueue
(2,0)withcost=2. - Dequeue
(0,1), explore(0,2),(-1,1)(invalid),(1,1)(blocked). - Enqueue
(0,2)withcost=2. - Dequeue
(2,0), explore(3,0),(2,1),(1,0)(visited). - Enqueue
(3,0)and(2,1)withcost=3. - Dequeue
(0,2), explore(0,3),(-1,2)(invalid),(1,2). - Enqueue
(0,3)and(1,2)withcost=3. - Dequeue
(3,0), explore(3,1),(2,0)(visited),(4,0)(invalid). - Enqueue
(3,1)withcost=4. - Dequeue
(2,1), explore(2,2),(3,1),(1,1)(blocked). - Enqueue
(2,2)and(3,1)withcost=4(but(3,1)already in queue). - Dequeue
(0,3), explore(0,4)(invalid),(1,3),(-1,3)(invalid). - Enqueue
(1,3)withcost=4. - Dequeue
(1,2), explore(1,3),(0,2)(visited),(2,2). - Enqueue
(2,2)withcost=5. - Dequeue
(3,1), explore(3,2),(2,1)(visited),(4,1)(invalid). - Enqueue
(3,2)withcost=5→ Goal found!
Path: S → (1,0) → (2,0) → (3,0) → (3,1) → (3,2) = G
Cost: 5 steps.
B. Informed Search: Heuristics Guide the Way
Heuristics (h(n)) estimate the cost from state n to the goal. Admissible heuristics never overestimate.
| Algorithm | Heuristic Used | Time Complexity | Optimal? | Complete? |
|---|---|---|---|---|
| Greedy | h(n) | ❌ | ❌ (unless h(n) perfect) | |
| A* | f(n) = g(n) + h(n) | ✅ (if h(n) admissible) | ✅ |
Worked Example: A for Pathao Delivery*
Scenario: A Pathao rider at (0,0) must deliver to (4,4). Traffic costs:
- Moving horizontally/vertically: 1 unit.
- Moving diagonally: 1.414 units (Pythagorean theorem). Heuristic: Manhattan distance (h(n) = |x_G − x_n| + |y_G − y_n|). Trace:
- Start at
(0,0),g(n)=0,h(n)=8,f(n)=8. - Expand
(1,0):g(n)=1,h(n)=7,f(n)=8→ tied with parent; keep both. - Expand
(0,1):g(n)=1,h(n)=7,f(n)=8. - Expand
(1,1)diagonally:g(n)=1.414,h(n)=6,f(n)=7.414→ better path. - Continue until
(4,4)is reached via(3,3) → (4,4)withg(n)=5.656(optimal).
Visual: A Search Tree*
3. Game Playing: Minimax and Alpha-Beta Pruning
Games involve adversarial search where players alternate moves. Minimax assumes opponents play optimally.
Minimax Algorithm
- Tree structure: Alternate between maximizing player (MAX) and minimizing player (MIN).
- Evaluation: Leaf nodes get a score (e.g., material advantage in chess).
- Backpropagation: Propagate the best score up the tree.
Example: Tic-Tac-Toe
Trace:
- If the current player is MAX (X), choose the move with the highest MIN value.
- If MIN (O), choose the lowest MAX value.
- Optimal move: The root’s best child (e.g.,
X winsif available).
Alpha-Beta Pruning
Reduces search by eliminating branches that cannot affect the final decision.
- Alpha: Best value MAX can guarantee so far.
- Beta: Best value MIN can guarantee so far.
- Prune if
MAX ≥ BetaorMIN ≤ Alpha.
Example: Chess Endgame
4. Real-World Applications
A. Pathfinding in Delivery Apps (Pathao, Daraz)
- Problem: Find the shortest route avoiding traffic.
- Algorithm: A* with dynamic heuristics (real-time traffic data as edge costs).
- Example: A Daraz order in Kathmandu from Thapathali to Lakshmi Narayan might use:
- Heuristic: Euclidean distance + traffic delays.
- Cost: Time = distance + (traffic_weight × congestion_factor).
B. Game AI (Chess Engines, Poker Bots)
- Problem: Predict opponent moves and counter optimally.
- Algorithm: Minimax with alpha-beta pruning (e.g., Stockfish in chess).
- Example: Ncell’s poker bot uses minimax to evaluate hand strengths and bluff probabilities.
C. Robotics (Nepal’s NRC’s Autonomous Vehicles)
- Problem: Navigate unknown environments (e.g., disaster zones).
- Algorithm: DFS for exploration + A* for pathfinding.
- Example: A robot mapping a collapsed building uses:
- DFS to explore uncharted corridors.
- A* to return to base with minimal battery use.
5. Comparing Search Algorithms
| Criteria | BFS | DFS | UCS | Greedy | A* |
|---|---|---|---|---|---|
| Optimality | ✅ (uniform cost) | ❌ | ✅ | ❌ | ✅ (admissible h(n)) |
| Completeness | ✅ | ❌ (unless finite) | ✅ | ❌ | ✅ |
| Time Complexity | |||||
| Space Complexity | |||||
| Use Case | Shortest path, puzzles | Deep but narrow spaces | Non-uniform costs | Fast but suboptimal | Balanced (best for most) |
6. Exam Tips
- Trace searches step-by-step: For any algorithm, show the explored nodes, queue/stack order, and path cost.
- Example: For BFS, list nodes in the order they’re dequeued.
- Justify algorithm choice: Explain why BFS is better than DFS for a maze (avoids infinite loops) or why A* is better than Greedy (guarantees optimality).
- Draw trees/graphs: Label nodes with
(state, g(n), h(n), f(n))for A* or(player, value)for minimax. - Calculate costs: For UCS or A*, show how
g(n)accumulates (e.g.,g(n) = g(parent) + edge_cost). - Alpha-beta pruning: Highlight pruned branches in game trees and explain why they’re cut.
7. Common Pitfalls
- Assuming DFS is complete: It fails in infinite spaces (e.g., cycles).
- Using non-admissible heuristics in A*: Leads to suboptimal paths.
- Ignoring edge costs: UCS and A* require summing costs; BFS assumes uniform cost.
- Misapplying minimax: Forgetting to alternate players (MAX/MIN) leads to incorrect scores.
8. Practice Problems
BFS Trace: Apply BFS to find the shortest path in this graph (edges = weights):
A --2-- B | \ | 1 3 1 | \ | C --2-- DAnswer:
A → C → D(cost = 3).A Heuristic*: For the grid below, design an admissible heuristic to reach
(4,4)from(0,0).0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0Hint: Use Manhattan distance but account for walls.
Minimax Tree: Draw the minimax tree for this game and find the optimal move for MAX:
Root (MAX) ├── Child1 (MIN): [Leaf1 = 3, Leaf2 = -1] └── Child2 (MIN): [Leaf3 = 2, Leaf4 = 4]Answer: Choose
Child2(value = 2).
9. Key Formulas
- Branching Factor (b): Average number of successors per node.
- Depth (d): Number of actions to reach the goal.
- A Cost Function*:
- : Cost from start to n.
- : Heuristic estimate from n to goal.
- Minimax Value:
10. Summary Table
| Concept | Key Idea | When to Use |
|---|---|---|
| State Space | All possible configurations. | Modeling problems as graphs. |
| BFS | Explore all nodes at depth d before d+1. | Shortest path in unweighted graphs. |
| DFS | Explore as far as possible along a branch. | Deep, narrow spaces (e.g., mazes). |
| UCS | Like BFS but prioritizes lowest cost. | Non-uniform edge costs. |
| Greedy | Follow the "most promising" heuristic. | Fast but not optimal. |
| A* | Balances cost and heuristic. | Optimal pathfinding (e.g., GPS). |
| Minimax | Assume opponents play optimally. | Two-player games (chess, poker). |
| Alpha-Beta | Prune irrelevant branches. | Speed up minimax in large trees. |
Based on the PU BE Computer (PU) syllabus for Artificial Intelligence (CMP346), unit 3.
Discussion
Loading…