AI Notes
Compare BFS, DFS, UCS, A*, and search algorithms. Learn when to use each algorithm, complexity analysis, practical applications in AI systems.
Introduction
Search algorithms are fundamental to artificial intelligence. They provide the mechanisms by which an AI agent explores possible states, plans actions, and finds solutions to problems. Choosing the right search algorithm for a given problem can mean the difference between finding an optimal solution in milliseconds and waiting hours for a suboptimal answer. This guide compares the major search algorithms across multiple dimensions to help you understand when to use each one.
Classification of Search Algorithms
Comprehensive Comparison Table
| Algorithm | Complete? | Optimal? | Time | Space | Uses Heuristic? |
|---|---|---|---|---|---|
| BFS | Yes (finite) | Yes (unit cost) | O(b^d) | O(b^d) | No |
| DFS | No (infinite) | No | O(b^m) | O(bm) | No |
| UCS | Yes | Yes | O(b^(1+⌊C*/ε⌋)) | O(b^(1+⌊C*/ε⌋)) | No |
| IDDFS | Yes | Yes (unit cost) | O(b^d) | O(bd) | No |
| Greedy | No | No | O(b^m) | O(b^m) | Yes |
| A* | Yes | Yes (admissible h) | O(b^d) | O(b^d) | Yes |
Where: b = branching factor, d = solution depth, m = max depth, C* = optimal cost, ε = minimum edge cost
Detailed Algorithm Characteristics
Breadth-First Search (BFS)
| Strategy | Explore all nodes at depth k before depth k+1 |
| Data structure | FIFO Queue |
| Best for | Shortest path in unweighted graphs |
| Weakness | Enormous memory usage for deep solutions |
| Example | Finding shortest route in a maze (all moves cost 1) |
| Nodes expanded | 1 + 2 + 4 + 8 = 15 (all levels up to solution) |
| Nodes in memory | ~8 (entire frontier at depth 3) |
Depth-First Search (DFS)
| Strategy | Explore deepest unexpanded node first |
| Data structure | LIFO Stack (or recursion) |
| Best for | Memory-constrained problems, existence questions |
| Weakness | Can get stuck in infinite branches, not optimal |
| Example | Solving a Sudoku puzzle (go deep, backtrack on failure) |
| But memory | only O(bd) = O(2×3) = O(6) nodes stored! |
Uniform Cost Search (UCS)
| Strategy | Expand cheapest-total-cost node first |
| Data structure | Priority Queue (min-heap by path cost) |
| Best for | Shortest path with varying edge costs |
| Weakness | Explores in all directions (no goal guidance) |
| Example | Finding cheapest flight route between cities |
| Start | {A: cost 0} |
| Expand A | {B: 3, C: 5, D: 7} |
| Expand B | {C: 5, E: 6, D: 7} |
| Expand C | {E: 6, D: 7, F: 8} |
Iterative Deepening DFS (IDDFS)
| Strategy | Repeated DFS with increasing depth limits |
| Combines | BFS completeness + DFS memory efficiency |
| DFS(limit=0) | Check only start node |
| DFS(limit=1) | Check depth 0 and 1 |
| DFS(limit=2) | Check depth 0, 1, and 2 |
| Total nodes | (d+1)×1 + d×b + (d-1)×b² + ... + 1×b^d |
| Dominated by last level | only ~11% overhead for b=10! |
Greedy Best-First Search
| Strategy | Expand node closest to goal (by heuristic) |
| Evaluation | f(n) = h(n) only |
| Best for | Quick approximate solutions |
| Weakness | Can be misled by heuristic, not optimal |
| Example | GPS suggesting "as the crow flies" direction |
A* Search
| Strategy | Expand node with lowest f(n) = g(n) + h(n) |
| Best for | Optimal pathfinding with available heuristic |
| Weakness | Memory grows exponentially |
| Requirement | h must be admissible (never overestimates) |
| Expand Arad | Sibiu(393), Timisoara(447), Zerind(449) |
| Expand Sibiu | f = 140 + 253 = 393 |
Worked Example: Comparing All Algorithms
Consider finding a path in this graph:
| Optimal path: A | C→D→E (cost = 2+1+2 = 5) |
| Alternative: A | B→E (cost = 1+4 = 5) — tie! |
| Non-optimal: A | B→D→E (cost = 1+3+2 = 6) |
| BFS: A | B,C → D,E,D → finds B→E first (depth 2) ✓ optimal (by luck) |
| DFS: A | B → E (finds A→B→E, cost 5) — happens to be optimal here |
| UCS: A(0) | B(1),C(2) → C(2),D(4),E(5) → D(3),E(5) → E(5) ✓ optimal guaranteed |
| Greedy (h=straight line to E): May go A | B→E directly if h(B)<h(C) |
| A* | f(B)=1+h(B), f(C)=2+h(C) — guaranteed optimal with admissible h |
When to Choose Which Algorithm
| YES | BFS (simple and optimal) |
| NO | continue |
| YES | A* (optimal + efficient with good heuristic) |
| NO | UCS (optimal without heuristic) |
| YES + need optimality | IDDFS or IDA* |
| YES + approximate OK | DFS or Greedy |
| YES | Hill Climbing, Simulated Annealing, Genetic Algorithm |
| YES | Minimax with Alpha-Beta Pruning |
Heuristic Quality and Its Impact
The quality of a heuristic dramatically affects A* performance:
| Manhattan distance | expands ~100 nodes for 8-puzzle |
| Misplaced tiles | expands ~1000 nodes for 8-puzzle |
| h=0 (UCS) | expands ~100,000 nodes for 8-puzzle |
Space-Time Tradeoffs
Summary
No single search algorithm is best for all problems. BFS and IDDFS offer completeness and optimality for uniform-cost problems with minimal heuristic knowledge. UCS handles variable costs optimally. A* combines cost information with heuristic guidance for the most efficient optimal search, but requires admissible heuristics and substantial memory. Understanding these tradeoffs allows you to select the right algorithm based on your problem's characteristics: graph structure, available heuristics, memory constraints, and optimality requirements.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Search Algorithms Comparison - AI Techniques.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Artificial Intelligence topic.
Search Terms
artificial-intelligence, artificial intelligence, artificial, intelligence, search, algorithms, comparison, search algorithms comparison - ai techniques
Related Artificial Intelligence Topics