Search Algorithms and Intuition
Search as Problem Solving
Search is a process for exploring choices in order to find a path, solution, or useful pattern. Maps make search easy to illustrate, but search is not limited to maps: it can also describe planning, decision-making, and reasoning about stories.
A search problem can be represented as:
A starting state, such as a location s.
A set of possible choices from each state.
A goal state, such as a destination g.
A strategy for deciding which choices to explore and in what order.
Finding some path is different from finding the best path. Search strategies vary in how quickly they find a solution, how much they explore, and whether they use information about which choices appear promising.
People often find a reasonably good route by looking at a map. The visual system seems to contribute substantially to this everyday problem-solving ability, but its contribution is not fully explained by the search procedures described here. Computer programs that lack human-like visual understanding must use other methods.
Search Trees and Paths
A search tree represents the alternatives that can be explored from the starting state. Each branch represents a choice, and each path through the tree represents a sequence of choices.
For example, if s can lead to a or b, the first level contains those two alternatives. Further levels show the alternatives available from each of them.
Consistent ordering
When several choices are available, they can be explored in a consistent order, such as lexical order. This makes the search procedure predictable and makes search trees easier to construct and compare.
Avoiding cycles within a path
A path should not revisit a state that already appears on that same path. Otherwise, it could loop indefinitely—for example, by leaving a state and later returning to it.
This restriction is different from avoiding repeated exploration across different paths. A search may initially generate separate paths that arrive at the same state; a further optimization can prevent expanding that state more than once.
Exhaustive Search: The British Museum Approach
The British Museum approach finds every possible path rather than stopping when it finds a route to the goal. It systematically enumerates the available paths, respecting the rule that a path cannot loop back to a state already on itself.
This approach has a simple purpose: it guarantees that all possible paths are considered. Its disadvantage is that it can require enormous effort. Since it must find everything, clever ordering cannot eliminate the fundamental work of enumerating all possibilities.
The method is therefore a useful contrast with practical searches, which try to find a solution without examining every path.
Depth-First Search
Depth-first search (DFS) pursues one branch as far as possible before considering alternatives. If choices are explored in lexical order, DFS follows the first choice, then the first available choice from that state, and continues downward.
If it reaches a dead end, it can use backtracking:
Return to the most recent state where a choice was made.
Take a different available branch.
Continue searching from there.
Backtracking makes DFS useful when a promising-looking branch may fail and another branch must be tried. It is optional in some descriptions of DFS, but it is generally valuable when the goal is to find a path and the first branch may lead to a dead end.
DFS can find a solution without exploring every branch, but its first solution may be long or otherwise poor. Its behavior depends strongly on the order in which branches are explored.
Breadth-First Search
Breadth-first search (BFS) explores the search tree level by level. It first considers paths with one step from the start, then paths with two steps, then paths with three steps, and so on. It stops when it reaches a goal.
This systematic approach can be wasteful when many paths at the same level lead away from the goal. In particular, BFS does not by itself know which direction is closer to the destination.
When all steps have equal cost, BFS finds a path with the fewest steps, because it explores shorter paths before longer ones. That does not necessarily make it the best choice when steps have different costs or when “best” means something other than the smallest number of steps.
Avoiding repeated expansions
A basic search may expand paths that arrive at the same state multiple times. For example, it may reach a state by one route, then later reach and expand it again by another route. This repeats work.
A common improvement is to keep an expanded-state list:
When a path is considered for expansion, check its final state.
If that state has already been expanded, do not expand it again.
Otherwise, expand it and record the state.
This avoids redundant work in searches such as DFS and BFS. It is distinct from the rule against revisiting a state within a single path: the path rule prevents cycles, while the expanded-state list prevents repeated exploration across different paths.
A Queue-Based View of Search
DFS and BFS can be implemented with the same general procedure. The key difference is how newly generated paths are added to a queue—a list of paths awaiting consideration.
A general search procedure works as follows:
Initialize the queue with a path containing only the starting state.
Take a path from the queue.
Check whether it reaches the goal.
If it does not, extend it by generating paths through its available next choices.
Add those new paths to the queue according to the search strategy.
Repeat until a goal is found or there are no paths left to consider.
The queue’s ordering determines which path is explored next:
DFS: Put new paths at the front of the queue. The most recently generated paths are explored first, so search continues down a branch.
BFS: Put new paths at the back of the queue. Older paths are explored first, so search proceeds level by level.
Thus, two searches can share almost all their machinery while differing in a single important decision: where to place newly generated paths.
Heuristic Search
A heuristic is information that helps estimate which choices are promising. In a route-finding problem, an estimate of distance to the goal can guide the search toward states that appear closer to the destination.
Heuristics can reduce unnecessary exploration, but they do not necessarily guarantee the best solution. Their usefulness also depends on whether such information is available and relevant to the problem.
Hill Climbing
Hill climbing is a heuristic version of depth-oriented search. Rather than choosing among alternatives only by a fixed ordering, it prefers the state that appears closest to the goal. If two alternatives seem equally promising, a rule such as lexical order can break the tie.
For example, from a starting point with two choices, hill climbing selects whichever choice has the smaller estimated distance to the destination. It then applies the same principle at the next state.
Hill climbing can be efficient because it directs effort toward promising choices. It may find a good path with far fewer expansions than an uninformed search. However, choosing the locally most promising option does not ensure that the overall route is best. A route can appear to approach the goal and still lead into a dead end.
Beam Search
Beam search combines level-by-level exploration with a heuristic limit on how many paths are retained. It proceeds in the style of BFS, but keeps only a fixed number of the most promising paths at each level.
The number of paths retained is the beam width, often denoted w. At each level, the search evaluates the candidate paths and discards all but the best w according to the heuristic.
A narrow beam can greatly reduce the amount of work. However, it may discard a path that would later have led to the solution. Beam search therefore trades completeness—the guarantee of retaining every relevant possibility—for efficiency.
Best-First Search
Best-first search selects the most promising available leaf in the search tree, even if that leaf lies on a different branch from the one most recently explored. It can therefore switch among branches, rather than continuing strictly down one path or across one level.
This strategy differs from hill climbing, which makes choices locally as it extends a path. Best-first search considers the available leaves across the broader search tree and selects the one that currently looks best. A similar strategy can be used in problem-solving structures such as AND/OR trees: the system may work on whichever subproblem appears easiest or most promising.
Comparing the Main Search Strategies
Uninformed strategies
British Museum search: Enumerates every possible path.
Depth-first search: Pursues a branch deeply before trying alternatives; can use backtracking.
Breadth-first search: Explores paths level by level.
These strategies do not use an estimate of distance or progress toward the goal to prioritize choices.
Heuristic strategies
Hill climbing: Follows the locally most promising next choice.
Beam search: Explores level by level while retaining only the best fixed number of candidates at each level.
Best-first search: Selects the most promising available leaf in the search tree.
Heuristic strategies can focus search more effectively when useful guidance is available, but their success depends on the quality of that guidance and the structure of the problem.
Limitations of Hill Climbing in Continuous Spaces
Hill climbing can also be applied to a continuous space. For example, someone trying to reach higher ground in fog could test several directions, estimate which one leads uphill most strongly, and then move in that direction.
This local strategy can fail in several ways.
Local maxima
A local maximum is a point that is higher than nearby points but lower than the true highest point in the overall landscape. A climber who only checks nearby directions may conclude that the destination has been reached, even though a higher peak lies elsewhere.
Plateaus
A plateau is a broad, relatively flat region. On a flat shoulder, nearby directions may seem equally good, so local uphill information provides no clear guidance. The search may fail to make progress toward the actual goal. Such flat terrain can be dangerous in real navigation: someone relying only on immediate slope may not find the route to safety.
Ridges
A ridge can also mislead hill climbing. In a contour map, a narrow ridge may rise diagonally while the four directions tested locally—north, south, east, and west—each appear to go downhill. The climber may wrongly conclude that the current point is a peak, even though movement along the ridge would lead upward.
This difficulty can become especially significant in high-dimensional spaces, where the structure of the landscape is more complicated and a limited set of local directions may provide a misleading picture.
Search as a Model of Reasoning
Search is relevant to models of intelligence because planning involves evaluating choices and considering how they may work out. A search procedure can represent part of this process: it explores possibilities, uses information to prioritize them, and identifies a path or pattern that meets some goal.
Maps are only one illustration. Search can also support reasoning about events, explanations, and higher-level ideas in a story.
Building and searching a story representation
A story-understanding system can represent events and relationships in an elaboration graph. The graph may contain:
Information stated directly in the story.
Additional events or consequences inferred using common-sense rules.
Higher-level patterns that connect events to concepts such as revenge or victory.
For example, common-sense knowledge can support inferences about what follows when one character kills another. Reflective knowledge—knowledge about broader concepts and patterns—can describe the structure of revenge or of a victory that ultimately leads to disaster.
Recognizing higher-level patterns
To identify a concept such as revenge, the system searches the elaboration graph for a pattern matching its description of revenge. The pattern may depend on both explicitly stated events and events inferred from common-sense rules.
A Pyrrhic victory is a success that brings consequences so damaging that the victory is ultimately costly or self-defeating. A story graph may show a character achieving an initial goal, then later suffering a disastrous consequence because of that success. Searching for this higher-level pattern lets the system recognize the Pyrrhic victory rather than merely listing the events.
Answering “why” questions
A system can use graph relationships to answer questions at different levels:
Common-sense explanations can be produced by examining local links among events and goals. For example, a character’s action may be explained by anger or by a desire to become king.
Reflective explanations can be produced by identifying higher-level patterns in which the event participates, such as revenge or a Pyrrhic victory.
The system can then report the relevant explanation in language. The important idea is that answering a question involves more than retrieving a sentence: it can require searching for connections among events, inferring consequences, and recognizing higher-level patterns.
Search is fundamentally about exploring choices and relationships. Maps provide a clear visual example, but the same underlying idea can be used for navigation, planning, and interpreting stories.

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