Graph Search — The Beginnings
Graph search: the first thing AI could actually do — and still useful today.
Your phone finds the fastest route through millions of streets in milliseconds. How? It searches a graph. This technique — modeling problems as graphs and searching them systematically — is where artificial intelligence began.
In this article, you will learn the two fundamental search strategies on which the earliest AI programs were built: Breadth-First Search (BFS) and Depth-First Search (DFS). You already know graphs as data structures — now they become tools for AI.
State Space — Every Problem Is a Graph
Any problem with a defined start, a goal, and allowed actions can be represented as a graph. This abstraction is the central idea of classical AI: whether chess, route planning, or puzzles — everything becomes the same mathematical structure.
Definition
State Space
Analogy
Definition
Where the maze analogy breaks: a maze is two-dimensional and manageable. Chess has approximately 10⁴⁴ legal positions — a maze the size of a galaxy.
Take the 8-Puzzle (3×3 sliding puzzle): the start state is a random solvable arrangement of tiles 1-8, the goal is the ordered sequence. Each move slides one tile into the empty space. The state space has exactly 181,440 reachable states (9!/2).
8-Puzzle
181,440
Reachable states (9!/2)
Chess
~10 ⁴⁴
Legal positions
Go
~10 ¹⁷⁰
Legal positions
Caution
Misconception: The algorithm builds the entire graph in memory
No. States are generated on-the-fly — the algorithm computes successor states only when it needs them. Storing the entire graph for chess (10⁴⁴ positions) would require more storage than exists on Earth.
Breadth-First Search (BFS) — Level by Level
Breadth-First Search explores the state space layer by layer: first all neighbors of the start node, then their neighbors, and so on. It uses a queue (FIFO — First In, First Out). To see how this search works in practice, let's switch from the maze to a different picture: imagine searching for a friend in a building. You check all rooms on your floor first (level 0), then go up one floor (level 1) and check all rooms there. You are guaranteed to find your friend on the nearest floor. Where the analogy breaks: a building has few floors with a constant number of rooms. In a state space, each level has exponentially more nodes than the previous one. The following example uses this small graph: A is the start node. From A, edges lead to B and C. From B, edges lead to D and E. From C, an edge leads to F. From E, an edge also leads to F. The goal is F.
Note
BFS Step by Step
- 1. We start at A and mark it as visited.
- 2. We look at A. It has two neighbors: B and C. Both join the queue.
- 3. Next up is B. B has neighbors D and E — both join the queue.
- 4. Now C is next. C has one neighbor: F. F joins the queue.
- 5. D is checked (dead end). E is checked (F already known).
- 6. F comes up — that's our goal! Path: A → C → F, just 2 edges. That's the shortest path.
BFS is complete (always finds a solution if one exists) and optimal (finds the shortest path by edge count). The cost: time and space complexity O(b^d) — exponential.
Caution
Misconception: BFS is always the best choice
BFS requires O(b^d) memory — exponential in solution depth. For chess with branching factor b=35 and depth d=80, the complete search tree would have roughly 35⁸⁰ nodes. That is more nodes than atoms in the observable universe (10⁸⁰). BFS is optimal but often physically impossible.
Depth-First Search (DFS) — As Deep as Possible
Depth-First Search takes the opposite approach: it follows one path as deep as possible before backtracking and trying alternatives. It uses a stack (LIFO — Last In, First Out) or recursion. Imagine navigating a maze by always turning left. When you hit a dead end, you go back to the last intersection and try right. You will find the exit — but probably not via the shortest route. Where the analogy breaks: "always turn left" works in physical mazes (walls prevent cycles). In graphs, DFS can loop endlessly without a visited set.
Note
DFS Step by Step
- 1. We start at A. A is not the goal. We pick the first neighbor: B.
- 2. B is not the goal. On to B's first neighbor: D.
- 3. D is not the goal and has no neighbors — dead end! Back to B.
- 4. B has another neighbor: E. Let's try that.
- 5. E is not the goal. E has one neighbor: F.
- 6. F is the goal — found it! Path: A → B → E → F, that's 3 edges.
DFS is not complete (can get stuck in cycles) and not optimal (finds a path, not the shortest). But DFS needs only O(b·m) memory (m = maximum search depth) — dramatically less than BFS.
Caution
Misconception: DFS finds the shortest path
No. DFS finds the first path it stumbles upon — and which path that is depends on the order of neighbors. In the example, DFS found [A, B, E, F] with 3 edges, while BFS found the shorter [A, C, F] with 2 edges.
BFS vs. DFS — The Trade-Off
There is no "better" algorithm — the choice depends on the problem.
Data structure: Queue (FIFO). Finds shortest path: Yes. Complete: Yes. Memory: O(b^d) — exponential. Best for: Small state spaces, shortest path required.
Data structure: Stack (LIFO). Finds shortest path: No. Complete: No (without cycle detection). Memory: O(b·m) — linear. Best for: Deep state spaces, limited memory, existence check.
BFS wins for small state spaces when the shortest path is needed. DFS wins for deep state spaces, limited memory, or when any path will do.
Interactive: Compare BFS and DFS Live
You have learned about BFS and DFS in theory. Now you can try both algorithms on the same graph. Switch between BFS and DFS, step through the execution — and observe how differently they traverse the graph.
Interactive: PageRank — When Authority Flows Through Links
BFS and DFS search a graph to find a path. Google's PageRank also uses a graph but asks a different question: which node is the most important? The idea is a random surfer who follows links and occasionally jumps to any page (the damping). Build a small graph below, start the iteration and watch for the aha moment: a single link from an important node can be worth more than many links from unimportant ones.
In his seminal 1950 paper "Programming a Computer for Playing Chess," Claude Shannon distinguished two strategies: Type A (brute force — examine all moves to a fixed depth) and Type B (selective — favor promising moves). Type A resembles blind search such as BFS and DFS (it skips no move and only evaluates the positions at the fixed search depth); Type B points toward heuristic search.
The exponential explosion makes blind search impossible for real problems: the 8-Puzzle has 181,440 states (solvable), the 15-Puzzle has ~1.05 × 10¹³ (first searched completely with breadth-first search in 2005: 28 days, 1.4 terabytes), a Rubik's Cube ~4.3 × 10¹⁹, chess ~4.8 × 10⁴⁴, and Go ~2.08 × 10¹⁷⁰.
A clever compromise is iterative deepening: run depth-first search repeatedly, going one level deeper each time. This combines the guarantees of breadth-first search with the memory efficiency of depth-first search.
Blind search (BFS/DFS) cannot solve real-world AI problems alone. The next article introduces heuristics — intelligent shortcuts that make search practical.
Note
Key Takeaways
- Any problem with defined states, transitions, and goals can be modeled as a state space — making it searchable by algorithms.
- BFS guarantees the shortest path but consumes exponential memory. DFS uses minimal memory but may find long detours or loop forever.
- Blind search works for small state spaces but is physically impossible for complex problems like chess — this is why AI needs heuristics (next article).
Check
Quiz: Graph Search
What does a "state" represent in a state space?
Select one answer
1. What does a "state" represent in a state space?
2. Which data structure does BFS use, and what property does this guarantee?
3. A navigation app must find the route with the fewest road segments between two cities. The road network has 500 intersections. Which algorithm is appropriate?
4. A robot must check if ANY path exists through a maze with 1,000 levels. Memory is limited to 1 MB. Which algorithm is more appropriate?
5. You trace DFS and BFS on the same graph from A to F. BFS returns [A, C, F] (2 edges), DFS returns [A, B, E, F] (3 edges). Why did DFS find a longer path?
6. Chess has approximately 10⁴⁴ legal positions with a branching factor of about 35. A student claims: "We just need a faster computer to run BFS on chess." Evaluate this claim.
Check
Comprehension Check
Can you explain this in your own words? Tick what you've got.
0 of 3 done