TBA
4.1 GenericSearch ....................................... 3
4.2 Breadth-FirstSearch .................................... 3
4.3 Depth-FirstSearch ..................................... 4
4.4 IterativeDeepening ..................................... 5
4.5 IterativeBroadening .................................... 6
4.6 BidirectionalSearch .................................... 7
4.7 Summary .......................................... 8
Our first series of lectures is concerned with the following topics in the core area of search:
A basic search problem is a 4-tuple �X,S,G,δ�, where
Example: As an example, consider the slidingtilespuzzle, where the objectiveis to slide numbered tiles into numerical order, given an n × m board with n × m − 1 tiles and one blank space. The states X describe the locations of the numbers and the blank space. The set of start states S = X. The set of goal states G ⊆ X includes only that state in which the tiles are in row-major order: i.e, 1 to n in the first row, n +1 to 2n inthesecond row, ...,andfinally, n(m − 1)+1 to nm − 1 in the last row, with the blank square in the bottom-right-most corner. The transition function describes the change in state that results from moving the blank space left, right, up, or down. (See Figure 1).
135 123 X ↓ X →← 724 456 →← X ↑ X 68 78X ↑ X XXX
(A)Start State (B)Goal State (C)Examples of Legal Moves
Figure1: SlidingTilesPuzzle(n = m = 3). (A) Start State. (B) Goal State. (C) Examples of Legal moves: An arrow indicates the direction in which a tile can be moved. An “X” means that a tile cannot be moved.
Exercise: Formalize the following version of the All the King’s Digits puzzle as a basic search problem: Given the sequence of digits 0123456789, insert the symbols (,),+,−,·,/ between the digits such that the resulting expression evaluates to 100.
One possible solution to this puzzle is:
0+1+2+3+4+5+6+7+(8· 9) =100
Can you find others?
Before discussing a variety of algorithms designed to solve search problems, it is worth mentioning how we will evaluate such algorithms. The following criteria are typically used for this evaluation:
• optimality: doesit find anoptimal solution(e.g.,ashortestpath),if multiplesolutionsexists?
The analyses and algorithms presented in this lecture depend on the assumption that the search space is a tree of(possibly infinite) depth d with finite branching factor b. Each node in this tree represents a state in the search space. The depth of a tree is defined as the maximum number of links connecting the root node to any other node in the tree. The branching factor of a treeis defined asthe maximum number ofimmediate successors(i.e., children) of any nodeinthetree.
In atree,theimmediate successors of a node(otherthantheleaves) are calledits children; the(unique) immediatepredecessor of a node(otherthanthe root) is calledits parent; and the nodes with whom it shares its parent are called its siblings.
In this section, we discuss five blind search algorithms (for trees). They are all instances of the genericsearch algorithm(fortrees) whichisdescribed first.
During a search, the fringe (or frontier) is the collection of nodes waiting to be visited, usually stored as a stack, a queue, or a priority queue. Nodes on the fringe are called open. After nodes are deleted from the fringe, they are labeled closed.
Search spaces canbeformulated astrees orgraphs. Below, weformulategeneric search algorithms forboth trees andgraphs; thedifferencebetweenthemisthatthetreealgorithmdoesnotmaintain a list of closed nodes because nodes in a tree are never reencountered during a search.
The remainder ofthe(blind search) algorithmsin thislecture specialize thegeneric search algorithm fortrees(Table 1). Wedeferadetaileddiscussionofgraph search untilthelecturesonheuristic search(Lectures03 and04).
The mainidea ofbreadth-first search(BFS) isto visit all nodes atdepth i before visiting those at depth i+1: i.e., after visiting a node at the next level, visit its siblings before visiting its children. BFS is implemented by storing the set of open nodes as a queue, and accessing its entries in a first-in-first-out fashion.
BFS is complete: it is guaranteed to find a solution, if one exists. Moreover, BFS is optimal: it always finds a goal node of minimal distance from the start. That’s the good news. The bad news
Search(X,S,G,δ)
| Inputs | search problem |
| Output | (pathto) goal node or failure |
| Initialize | O = S is the set of open nodes |
while (O is not empty) do
fail
Table 1: Generic Search Algorithm for Trees. Search(X,S,G,δ)
| Inputs | search problem |
| Output | (pathto) goal node or failure |
| Initialize | O = S is the set of open nodes |
| C = ∅ is the queue of closed nodes |
while (O is not empty) do
fail
Table 2: Generic Search Algorithm for Graphs.
is, in the worst case, BFS visits every node, which takes time as follows:
d bd+1 − 1
1+b+b2 + ... + bd = bi == O(bd) 1
b− 1
i=0
In terms of space, BFS maintains a list of all nodes at all depths: at depth d the length of this list is bd . Hence, the space complexity of BFS is exponential in d. In summary, both the time and space complexities of BFS are exponential in d.
LikeBFS,depth-first-search(DFS) can alsobe viewed asproceeding levelby level;however, after visiting a node at the next level, DFS visits its children before visiting its siblings. DFS is imple
1+ bd+1 bd+1
Let S =1+ b + b2 + ... + bd . Then bS = b + b2 + ... + bd . So(b − 1)S = bS − S = − 1, from which
bd+1
it follows that S = −1 .
b−1
BFS(X,S,G,δ)
| Inputs | search problem |
| Output | (pathto) goal node or failure |
| Initialize | O = S is the queue of open nodes |
while (O is not empty) do
fail
Table 3: Breadth-First Search.
mented by storing the set of open nodes as a stack, and accessing its entries in a last-in-first-out fashion.
DFS(X,S,G,δ)
| Inputs | search problem |
| Output | (pathto) goal node or failure |
| Initialize | O = S is the stack of open nodes |
while (O is not empty) do
fail
Table 4: Depth-First Search.
Like BFS, the time complexity of DFS is O(bd). It is exponential in d because in the worst-case DFS visits every node. The space complexity of DFS, however, is linear in d, where d is the length of longest path. Since at most b nodes are stored at each of the d depths, the space complexity of DFS is O(bd).
DFS is neither complete nor optimal. Given knowledge base {φ → φ,φ} and formula φ, DFS could fail to prove φ by forever visiting φ → φ. Using DFS, a robot that intends to visit its neighbor to the west, but starts out searching to the east, travels all the way around the world before finding its neighbor!
Iterative deepening (ID) is a search algorithm with the space requirements of DFS—it requires memory linear in d—and theperformanceproperties ofBFS—itis complete and(asymptotically) optimal. The main idea of iterative deepening is to repeatedly search in depth-first fashion, over
ID(X,S,G,δ)
| Inputs | search problem |
| Output | (pathto) goal node |
| Initialize | c = 0 is the cut-off depth |
| O = S is the stack of open nodes |
while (1)do
1. while (O is not empty) do
i. prepend δ(n)to front of O
2. increment c, O = S
Table 5: Iterative Deepening.
ID is optimal and complete: it is guaranteed to find a goal if one exists—specifically, an optimal goalif multiple solutions exist(aslong as c =1). It iteratively performs depth-first searches; thus, its space complexity is that of DFS, namely O(bd).
Like DFS and BFS, the time complexity of ID is O(bd): i.e., in the worst-case it is exponential in
d. ID visits nodes at depth 0 d+1 times, at depth 1 d times, ...,and atdepth d 1 time. Thus, the total time required is given by:
1+ 1+b +1+ b+ b2 +... +1+ b+ b2 + ... + bd = O(1+b+b2 + ... + bd)= O(bd)
��� � ��� � � �� �
depth 1 depth 2 depth d
Theidea ofiterativebroadening(IB) is analogoustothat ofiterativedeepening,butIBiteratively broadensits search, whereasIDiterativelydeepensit. IBperformsdepth-firstsearches on subgraphs of breadth 1, breadth 2, breadth 3, and so on, until a goal node is reached.
IBis notcomplete: itis susceptible tofollowinginfinitepaths withinitsbreadthlimit asit conducts depth-first search. Neither is IB optimal.
In the worst-case, its space complexity equals that of DFS, and the time it requires is O(bd). Specifically, the number of nodes IB visits is given by:
d+1+2+ ... +2d +... +1+ b+ ... + bd = O(1d +2d + ... + bd)= O((b+1)d+1)
| � | �� | � | � | �� | � | |
|---|---|---|---|---|---|---|
| breadth 2 | breadth b | |||||
| since |
bd+1 � bb � b+1
� (b+1)d+1 − 1
= x ddx ≤ c d ≤ x ddx =
d+1 01 d+1
c=1
IB(X,S,G,δ)
| Inputs | search problem |
| Output | (pathto) goal node |
| Initialize | c = 1 is the cut-off breadth |
| O = S is the stack of open nodes |
while (1)do
1. while (O is not empty) do
i. prepend c nodes in δ(n)to front of O
2. increment c, O = S
Table 6: Iterative Broadening.
A bidirectional search problem is a 5-tuple �X,S,G,δ,γ�, where �X,S,G,δ� is a basic search problem andγ : X → 2X isan “inverse” transitionfunction,meaning γ(x)is the setofpredecessors of x. Inbidirectionalsearch, as the name suggests, searchproceeds simultaneouslyin twodirections: bothforwardsfrom aninitial state andbackwardsfrom agoal state. Search terminates where and whenthe two searches meet. However, notallbasic searchproblems are easilyposed asbidirectional searchproblems: e.g.,itisnontrivial togeneratepredecessorsof theset of checkmateconfigurations in the game of chess.
Bidirectional search is typically implemented as two breadth-first searches. Like BFS, bidirectional BFS is optimal and complete. Bidirectional search improves upon BFS in terms of complexity, however. The time complexity of bidirectional BFS is exponential in d/2. In the worst case, each direction ofbidirectionalBFSvisits every nodethroughdepth d/2, whichfor twodirections requires O(2bd/2 )= O(bd/2). Its space complexity is also O(bd/2), since it maintains list ofall nodes all at
bd/2
depths i, and the number of nodes at depth d/2= .
Island-driven search is a generalization of bidirectional search in which a series of intermediate goals, or islands, is identified between the start and goal nodes. The islands reduce the one large search problem to m small search problems. If the islands are spaced evenly between the start and goal nodes, the number of nodes visited is mbd/m ≪ bd . An obvious difficulty with this approach is the identification of islands through which an optimal search path traverses. Consequently, it is difficult to guarantee optimality.
Criteria
DFS BFS ID IB biBFS
Time
O(bd) O(bd) O(bd) O((b+1)(d+1) ) O(bd/2) O(bd/2)Space
O(bd) O(bd) O(bd) O(bd) Completeness
NO YES YES NO YES Optimality
NO YES YES NO YES
1. IBispreferredifthebranchingfactorislarge: thesearch spaceis wide(possiblyinfinite), particularly if goals are known to be deep
Figure 2: (a) BFS: ABCDEFGHIJKLM. (b) DFS: ABEFGCHIJDKLM. (c) ID: AABCDABEFGCHIJDKLM.(d) IB: ABEABEFCHIABEFGCHIJDKLM.