Professional Documents
Culture Documents
BWG7JK53 4
BWG7JK53 4
BWG7JK53 4
C. Depth-First Search
D. Depth-First Iterative-Deepening
Depth-first search remedies the space limitation of
breadth-first and uniform-cost search by always gen- Depth-first iterative-deepening (DFID) combines the best
erating next a child of the deepest unexpanded node features of breadth-first and depth-first search. DFID
(see Fig. 3). Both algorithms can be implemented us- first performs a depth-first search to depth one, then
ing a list of unexpanded nodes, with the difference starts over, executing a complete depth-first search to
that breadth-first search manages the list as a first-in depth two, and continues to run depth-first searches
first-out queue, whereas depth-first search treats the to successively greater depths, until a solution is found
list as a last-in first-out stack. More commonly, depth- (see Fig. 4).
first search is implemented recursively, with the re- Since it never generates a node until all shallower
cursion stack taking the place of an explicit node nodes have been generated, the first solution found
stack. by DFID is guaranteed to be on a shortest path. Fur-
The advantage of depth-first search is that its space thermore, since at any given point it is executing a
requirement is only linear in the search depth, O(d), depth-first search, saving only a stack of nodes, and
as opposed to exponential for breadth-first search. the algorithm terminates when it finds a solution at
The reason is that the algorithm only needs to store depth d, the space complexity of DFID is only O(d).
a stack of nodes on the path from the root to the cur- Although it appears that DFID wastes a great deal of
time in the iterations prior to the one that finds a so-
lution, this extra work is usually insignificant. To see
this, note that the number of nodes at depth d is bd, and
each of these nodes is generated once, during the final
iteration. The number of nodes at depth
d 1 is bd1, and each of these is generated twice, once
during the final iteration, and once during the penul-
timate iteration. In general, the number of nodes gen-
erated by DFID is bd 2bd1 3bd2 db. This
is asymptotically O(bd) if b is greater than one, since for
large values of d the lower order terms become in-
significant. In other words, most of the work goes into
Figure 3 Order of node generation for depth-first search the final iteration, and the cost of the previous itera-
iterative-deepening search. tions is relatively small. The ratio of the number of