BWG7JK53 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

34 Search Techniques

B. Uniform-Cost Search rent node. The time complexity of a depth-first search


to depth d is O(bd), since it generates the same set of
If all edges do not have the same cost, then breadth- nodes as breadth-first search, but simply in a different
first search generalizes to uniform-cost search. Instead order. Thus, as a practical matter, depth-first search is
of expanding nodes in order of their depth from the time limited rather than space limited.
root, uniform-cost search expands nodes in order of The disadvantage of depth-first search is that it may
their cost from the root. At each step, the next node not terminate on an infinite tree, but simply go down
n to be expanded is one whose cost g(n) is lowest, the leftmost path forever. For example, even though
where g(n) is the sum of the edge costs from the root there are a finite number of states of the Eight Puz-
to node n. The nodes are stored in a priority queue. zle, the tree fragment shown in Fig. 1 can be infinitely
This algorithm is also known as Dijkstra’s single-source extended down any path, generating an infinite num-
shortest-path algorithm. ber of duplicate nodes representing the same states.
Whenever a node is chosen for expansion by The usual solution to this problem is to impose a cut-
uniform-cost search, a lowest cost path to that node off depth on the search. Although the ideal cutoff is
has been found. The worst-case time complexity of the solution depth d, this value is rarely known in ad-
uniform-cost search is O(bc/m), where c is the cost of vance of actually solving the problem. If the chosen
an optimal solution, and m is the minimum edge cost. cutoff depth is less than d, the algorithm will fail to
Unfortunately, it also suffers the same memory limi- find a solution, whereas if the cutoff depth is greater
tation as breadth-first search. than d, a large price is paid in execution time, and
the first solution found may not be an optimal one.

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

You might also like