0% found this document useful (0 votes)
16 views4 pages

AI Note

Uploaded by

Chaitra Chaitra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

AI Note

Uploaded by

Chaitra Chaitra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

three-dimensaonal protein with the right properties to cure sorne discase.

3.3 SEARCHING FOR SOLUTIONS

Having formulated sone problems, we now need to solve them. A solution is an action
sequence, so search algorithms work by considering various possible action sequences. The
SEARCH TREE
possible action sequences starting at the initial stale form a search tree with the initial state
the root, the branches are actions and the nodes correspond to states in the state space of
the problem. Figure 3.6 shows the first few steps in growing the search tree for finding a route
from Arad to Bucharest. The root node of the tree corresponds to the initial state, IntArad).
The first step is to test whether this is a goal state. (Clearty it is not, but it is important to
check so that we can solve trick problems like "starting in Arad. get to Arad.") Then we
EXPAWOING need to consider taking various actions. We do this by expanding the current state; that is,
GENEFANG
appiying each legal action to the current state, theretby generating a new set of stales. In
PAFENT NODE
this case. we add three branches from the parent node h Arad) leading to three new child
CHLD IOOE nodes: In Sibiu), In Timisoara)A and In Zerind). Now we must choose which of these three
possibilities to consider further.
This is the essence of searchfollowing up one option now and putting the others aside
for later, in case the first choice does not lead lo a solution. Suppose we choose Sibiu fins.
We check to see whether it is a goal stale (it is not) and then expand it to get n(Arad),
Ing Fagaras), InOradea), and InRinunicu Viikea). We can then choose any of these four or go
LENF NCDE back and choose Timisoara or Zeind. Each of these six nodes isa leaf node, that is, a node
with no children in the tree. The set of all leaf nodes available for expansion at any given
HCNTER point is called the frontier. (Many authons call it the open list, which is both geographically
less evocative and les accurate. because other data stuctures are better suitcd than a list.) In
Figure 3.6, the frontier of cach tree consists of thoe nodes with bold outlines.
The process of expanding nodes on the frontier continues until either a solutian is found
or there are no more states to expand The general TREE-SEARCH algorithm is shown infor
mally in Figure 3.7. Search algorithms all share this bsic structure; they vary primarily
SLARCH STIOY according to how they choose which stale to expand nexhe so-called search strategy.
The cagleeyed reader will notice one peculiar thing about the search tree shown in Fig
ure 3.6: it includes the path from Arad to Sibiu and back to Arad again! We say that In(Arad)
is a repealed state in the search tree. generated in this case by a loopy path. Considering
LOOPY NT such loopy paths means that the complete search tree for Romania is infinite because there
is no limit to how often one can traverse a loop. On the other hand, the state space-the
map shown in Figure 32-has only 20 states. As we discuss in Section 3.4, loops can cause

76 Chapler 3. Solving Problems by Searching

certain algorithms to fail, making otherwise solvable problems unsolvabie. Fortunately, there
s no need to consider loopy paths. We can rely on more than intuition for this: because path
costs are additive and step costs are nonnegative loopy path to any given stale is never
better than the same path with the loop removed.
Loopy paths are a special case of the more general concept of redundant paths, which
exist whenever there is more than one way to get from one stale to another. Consider the puths
Arad-Sibiu (140 km ong) and Arad-Zerind-Oradea-Sibiu (297 km long). Obviously, the
second path is redundant-it's just a worse way to get to the same state. If you are concerned
about reaching the goal, there's never ary reason to keep nore than one path to any given
state, because any goal state that is reachable by extending one path is also reschable by
extending the other.
In some cases, it is possible to define the problem itself so as to eliminate redundant
paths, For example. if we formulate the 8queens problem (page 71) so that a qucen can be
placed in any column, then each state with n queens can be reached by n! different paths: but
if we reformulate the problem so that each n queen is placed in the leftmost emply column.
then cach state can be reached only through one path

(a) The lnitial state

(b) After expanding Arad

ennd

(c) Affer expanding Sibiu

CZerind

Figure 3.6 Partial search trees fo fnding a route from Arad to Bucharest. Nodes that
ouilincd in boldt oodes that have not vet been eenerted se hoe i falet d e
function CHILD-NODElPrublem. parrnt. action) returas a node
STaTE s mlem RESULTE Parrnt.STATE
actson).
PARENT= parrnt. ACTION = octon.
PATH-CoST = parent PATH-CoST problem.STEP-CosT(parrnt.STATE, actiorn)

The node data structure is depicted in Figure 3.10. Notice how the PARENT pointers
string the nodes logether into a tree structure. These pointers also allow the solution path to be
extracted when a goal node is found we use the SoLUTION function to returm the sequence
of actions obtained by following parent pointers back lo the root
Up lo n¡w, we have not been very careful to distinguish between nodes and states, but in
writing detailed algorithms it's important lo make that distinction. A node is a bookkeeping
data structure used to represent the search tree. A state corresponds to a configuration of the
wordd. Thus, nodes are on particular paths, as defined by PARENT pointers, whereas states
are not. Furthenare, two different nodes can contain the same world state if that state is
generated via two different search paths
Now that we have nodes, we need somewhere to put them. The frontier needs to be
slored in such a way that the search algorithm can easily choose the next node to expand
according to its preferred strategy. The appropriate data structure for this is a queue. The
operations on a queue are as follows:
EMPTY?(queue) returns true only if there are no more clements in the queue.
PoMqueue) removes the first clernent of the queue and returms it.
INSERT(element, qurue) inserts an clement and returns the resuting queuc.

Chapler 3. Solving Problems by Searching

Queues are characterized by the onder in which they store the inserted nodes, Three common
FFOQUEL variants are the first-in. first-out or FIFO queue, which pops the oldest element of the queue;
UFO QUEUE the last-in, first-out or LIFO queue (also known as a stack), which pops the newvest element
PRORtY LUELE of the queue: and the priority queue, which pops the element of the queue with the highest
priority according to some ordering function.
The explored set can be implemented with a hash table to allow efficient checking for
repeated states. With a good implementation, insertion and lookup can be done in roughly
constant time no matter how many states are stored. One must take care to implement thc
hash table with the right notion of equality betwcen states. For example, in the traveling
salesperson problem (page 74). the hash table needs to know that the set of visited cities
( Bucharest, Urziceni, Vaslui) is the same as {Urziceni, Vaslui,Bucharest). Sometimes this can
be achieved most easily by insisting that the data structures for stales be in some canonical
CANONUCA #ORL form; that is, logically equivalent states should map to the same data structure. In the case
of stales described by sets, for example, a bit-vector representation or a sorted list without
repetition woukd be canonical., whereas an unsorted list would not.

3.3.2 Mleasuring problem-solving performance


Before we get into the design of specific search algorithms, we need lo consider the crilteria
that might be used to choose among them. We can evaluale an algorithm's performance in
four ways:
cOMPLETENSM Completeness: Is the algorithm guaranteed to find a solution when there is one?
oPTINALUTY
Optimality: Does the stralegy find the optimal solution, as defined on page 68?
TNE CON PEXTY
Time complexity: How long does it take to fnd a solution?
sACE COBtY
Space complexity: How much memory is needed to perfom the search?
Time and space complexity are always considered with respect to soame measure of the prob
lem dificulty. In theoretical compuler science. the typical measure is the size of the state
space graph. (V| + |E]. where V'is the set of vertices (nodes) of the graph and E is the set
of edges (links). Thi is appropriale when the graph is an explicit data structure that is input
to the scarch program. (The map of Romania is an example of this.) ln Al, the graph is often
represented inplicitly by the initial stale, actions, and transition model and is frequently infi
nite. For these reasons, complexity is expressed in terms of three quantities: b, the branching
BwCING PACtoR factor or maximum number of successors of any node: d, the depth of the shallowest goal
DEPTH node (i.e.. the number of steps along the path from the root): and m, the maximum length of
any path in the stale space. Time is ofen mcasured in terms of the number of nodes generated
during the search. and space in terms of the maximum number of nodes stored in memory.
For the most part. we describe time and space conplexity for search on a tree: for a graph,
the answer depends on how redundant" the paths in the state space are.
SEARCH COoSt To assess the effectiveness ofa search algorithm, we can consider just the search cost
which typically depends on the time complexity but can also include a tem for memory
TtAL COSt usage or we can use the total cost. which combines the search cost and the path cost of the
solution found For the probiern of finding a route from Arad to Bucharest. the search cost
is the amount of time taken by the search and the soiution cost is the total length of the path

Seetion 3,4. Uninformed Search Strategics 81

in kilometers. Thus, to compute the total cost, we have to add milliseconds and kilometers.
There is no "official exchange rate" between the two, but it might be reasonable in this case to
convert kilometers into milliseconds by using an estimate of the car's average speed (because
time is what the agent cares about), This enabies the agent to fnd an optimal tradeoff point
at which further computation to find a shorter path becames counterproductive. The more
general problem of tradeoffs between different goods is taken up in Chapter l6.

34 UNINFORMED SEARCH STRATEGIES


scems completely obVIOus, try ExeCIse 5.1S Dow.) Thus property is illustrated n Figure 5.9.
As every step moves a state from the frontier into the explored region while moving some
states from the unexplored region into the frontier. we see that the algorithm is rystematically
examining the states in the state space. one by one. until it fnds a solution.
3.3.1 Infrastructure for search algorithms

Search algorithms require a data structure to keep track of the search tree that is being con
structed. For cach node n of the tree, we have a structure that contains four components:
"n.STATE: the state in the state space to which the node corresponds;
" n.PARENT: the node in the search tree that generated this node:
n.ACTION: the action that was applicd to the parent to generate the node:
n.PATH-CosT: the cost, traditionally denoted by g(n), of the path from the initial stale
to the node, as indicated by the parent pointers.

Section 3.3. Searching for Solutions 98/1151

PARENT
Node ACION = Right
PATH CosT =6
STATE

Flgure 3.10 Nodes are the data structures from which the search tree is constructed. Each
has a parent, a stale, and various bookkeeping ficld, Arrows point from child to parent,

Given the components for a parent node, it is casy to see how to compute the necessary
components for a child node. The function CHILD-NoDE takes a parent node and an action
and returns the resulting chikd node:

unction CHILD-NoDE problem, parrnt, artson) returns a node


return a node with
RESULT(pa t.STATE. acthon).
PARENT1
PATH-CosT = perrnt.PATH-CosT problrm.STEP.CosT(parrnt,STATE, artion)

The node data struc ture is depicted in Figure 3.10. Notice how the PARENT POinters
string the nodes together into a tree structure. These pointers also allow the solution path to be
extracted when a goal node is found: we use the SOLUTION function to relum the sequence
of actions obtained by following parent pointers back to the root.
Up to now, we have not been very careful to distinguish between nodes and states, but in
writing detailed algorithms it's important to make that distinction. A node is a bookkeeping
data structure used to represent the search tree. A state corresponds to a configuration of the
world. Thus, nodes are on particular paths, s defined by PARENT pointers, whereas states
are not. Furthemore, two different nodes can contain the same worid state if that state is
generated via two different search paths.
Now that we have nodes, we need somewhere to put them. The frontier noeds to be
stored in such a way that the search algorithm can casily hoose the next node to expand
OUEUE according to its preferred strategy. The appropriate data structure for this is a queue. The
operations on a queue are as follows:
EMFrY?lqueue) returns true only if there are no more elements in the queue.
PoMqueue) removes the first element of the queue and retums it.
INSERT(element, queue) inserts an element and returns the resulting queue.

80 Chapter 3. Solving Probiems by Searching

Queues are characterized by the order in which they store the inserted nodes. Three common
FFO GUELE
variants are the first-in first-out or FIFO quene, which pops the oldest element of the queue;
UFO QLEUE the last-in, first-out or LIFO queue (also known as a stack). which pops the newvest element
PRORTYGUELE of the queue, and the priority queue. which pops the element of the queue with the highest
priority according to some ordering function.
The explored set can be implemented with a hash tabie to allow efficient checking for
repeated stales. Witha good imnplementation insertion and lookup can be done in roughly
constant time no mater how many states are stored. One must take care to implement the
hash table with the ight notion of equality between states. For example. in the traveling
salesperson problem (page 74), the hash table needs to know that the set of visited cities
{Bucharest. Urziceni. Vaskui} is the same as {Urziceni, Vaslui.Bucharest). Sometimes this can
be achieved most easily by insisting that the data structures for stales be in some canonical
CANCUCAL FORL form; that is, logically equivaent states should mnap to the same data structure. In the case
of stales described by sets, for example, a bit-vector representation or a sorted list without
repetition would be canonical, whereas an unsorted list woukd not.

3.3.2 Measuring problem-solving performance


Before we get into he design of specific search algorithms, we need to consider the criteria
that might be used to choose among them. We can evaluale an algorithm's performance in
Section 3.3. Searching for Solutions 77

function TREE-SEARCH(
initialize the fonti problern) returas a solution, cr failure
initi
rusing the initial state af problenm
loop do
iI the frontier is empty then return fadure
choose a leaf node and remeve it from the fronticr
iI the node contains a goal stale then retura the corresponding solutio
expand the chosen node, adding the resuhing nodes lo the fronticr

function GRAPH-SEARCH( proslem) returns a solution, or failure


nitialize the fronticr using the initial state of problem
the explored set lo be empty
lonn de
i the frontier is empty then return failure
97/1151
choase a leaf node and remove it from the fronticr
iI the node containsa goal statle then retura the corresponding solution
add the node to dhe erplored set
expand the chosen node, adding the resulting nodes to the frontier
only if not in the frontier or explored set

Figure 3.7 An informal descriptson of the general tree-scarch and graph-scarch algo
rithns. The parts of GRAPH-SEARCH marked in bold italic are the additions nceded to
handle repeated stales.

In other cases, redundant paths are unavoidable. This includes all problems where
the actions are revensible, such as route-fnding probiems and sliding-block puzzles, Route
EC ANGLAA ONO finding on a rectangular grid (like the one used later for Figure 3.9) is a particularty impor
tant example in conputer games. In such a grid, cach state has four successors, so a scarch
tree of depth d that includes repe ated states has 4 leaves; bul there are only about 2f distinct
states within d steps of any given state. For d = 20, this means aboul a trillion nodes but only
about 800 distinct states. Thus, following redundant paths can causc a tractable problem to
become intractable. This is true even for algorithms that know how to avoid infinite loops.
As the saying goes, algorithms that forget their history ar dooned to repeal it. The
way to avoid exploring redundant paths is to rernember where one has been. To do this, we
IXPLOPEOWT augment the TREE-SEARCH algorithm with a data structurc called the explored set (also
CLOSEO Ut known as the closed list). which remembers every expanded node. Newly generated nodes
that match previously generatcd nodes ones in the explared set or the frontier- can be dis
carded instead of being added to the fronticr. The new algorithm, called GRAPH-SEARCH, 0s
shown informally in Figure 3.7. The specific algorithms in this chapler draw on this general
design
Clearty. the search tree constructed by the GRAPH-SEARCH algorithm contains at most
one copy of cach state, so we can think of it as growing a tree directly on the stale-space graph,
SEPARATOA
as shown in Figure 3.8. The algorithm has another nice property: the frontier separates the
state-space graph into the explored region and the unexplored region, so that every path from

78 Chapter 3. Solving Probiems by Searching

Figure 3.8 Asequence of search trees generated by a graph scarch on the Romania prob
lem of Figure 3.2. At each stage. we have etended each path by one siep. Notice that at the
third stage. the northernmost city (Oradea) has becomea dead end: both of its successors are
already explored via other paths.

al b c)

Figure 3.9 The separation property of GRAPH-SEARCH, illustrated on a rectangular-grid


problem. The frontier (white nodes) always separates the explored region of the stale space
(black nodes) from he unexpiored region (gray nodes). In (a). jus the root has been ex
panded. In (b). one leaf node has been expanded. In (c), the remaining successors of the root
have been expanded in cockwise order.

the initial state to an unexplored state has to pass through a state in the frontier. (If this
seems completely obvious, try Exercise 3.13 now.) This property is illustrated in Figure 3.9.
As every step moves a state from the frontier into the expored region while moving somne
states from the unexplored regicn into the frontier, we see that the algorithm is systematically
examining the states in the state space. one by one. until it fnds a salution.

You might also like