You are on page 1of 35

Solving Problem by Searching

Uniform Cost Search


 It is optimal according to any specified path length
function
 It modifies the breadth first strategy by always
expanding the lowest-cost node on the fringe
 When certain conditions are met, the first solution that
is found is guaranteed to be the cheapest solution
 It searches in branches which are more or less in the
same cost
 At any given point in the execution, the algorithm
never expands a node which has a cost greater than
the cost of the shortest path in the graph

2
Uniform Cost Search

3
Uniform Cost Search

4
Uniform Cost Search

5
Properties of Uniform Cost Search
 Complete
◦ Yes
 Time
◦ O(bd)
 Space
◦ O(bd)
 Optimal
◦ Yes
Bidirectional Search
 The idea of bidirectional search is to reduce the search time by
searching forward from the start and backward from the goal
simultaneously
 When the two search frontiers intersect, the algorithm can
reconstruct a single path that extends from the start state through
the frontier intersection to the goal.
 A new problem arises during a bidirectional search, namely
ensuring that the two search frontiers actually meet.
◦ For example, a depth-first search in both directions is not likely to work
well because its small search frontiers are likely to pass each other by.
Breadth-first search in both directions would be guaranteed to meet.

7
Properties of Bidirectional Search
 Complete
◦ Yes
 Time
◦ O(bd/2)
 Space
◦ Algorithm dependent
 Optimal
◦ Yes
Avoiding Repeated States
 There are three ways to deal with repeated states, in
increasing order of effectiveness and computational
overhead
◦ Do not return to the state you just came from. Have the expand
function (or the operator set) refuse to generate any successor that is
the same state as the node's parent.
◦ Do not create paths with cycles in them. Have the expand function
(or the operator set) refuse to generate any successor of a node that is
the same as any of the node's ancestors.
◦ Do not generate any state that was ever generated before. This
requires every state that is generated to be kept in memory, resulting
in a space complexity of O(bd), potentially. It is better to think of this
as O(s), where s is the number of states in the entire state space.

9
Examples

MISSIONARIES AND
CANNIBALS

10
Exercise
The missionaries and cannibals: Three missionaries and three cannibals are on one side of a river,
along with a boat that can hold one or two people (one for rowing). Find a way to get everyone to
the other side, without ever leaving a group of missionaries in one place outnumbered by the
cannibals in that place (the cannibals eat the missionaries then).

a. Formulate the problem precisely, making only those distinctions necessary to ensure a valid
solution. Draw a diagram of the complete state space.
b. Implement and solve the problem optimally using an appropriate search algorithm. Is it a good idea
to check for repeated states?
c. Why do you think people have a hard time solving this puzzle, given that the state space is so
simple?

Image from http://www.cse.msu.edu/~michmer3/440/Lab1/cannibal.html


Missionaries & Cannibals
State: q = (M,C,B) signifying the number of missionaries, cannibals, and boats on the left
bank. The start state is (3,3,1) and the goal state is (0,0,0).
Actions (successor function): (10 possible but only 5 available each move due to boat)
 One cannibal/missionary crossing L  R: subtract (0,1,1) or (1,0,1)
 Two cannibals/missionaries crossing L  R: subtract (0,2,1) or (2,0,1)
 One cannibal/missionary crossing R  L: add (1,0,1) or (0,1,1)
 Two cannibals/missionaries crossing R  L: add (2,0,1) or (0,2,1)
 One cannibal and one missionary crossing: add/subtract (1,1,1)

Image from http://www.cse.msu.edu/~michmer3/440/Lab1/cannibal.html


Missionaries & Cannibals states

Assumes that passengers have to get out of the boat after the trip.
Red states = missionaries get eaten.
Breadth-first search on
Missionaries & Cannibals

States are generated by applying:


+/- (1,0,1)
+/- (0,1,1)
+/- (2,0,1)
+/- (0,2,1)
+/- (1,1,1)
In that order (left to right)
Red states = missionaries get eaten
Yellow states = repeated states
Breadth-first search on
Missionaries & Cannibals

States are generated by applying:


+/- (1,0,1)
+/- (0,1,1)
+/- (2,0,1)
+/- (0,2,1)
+/- (1,1,1)
In that order (left to right)
Red states = missionaries get eaten
Yellow states = repeated states
Breadth-first search on
Missionaries & Cannibals

States are generated by applying:


+/- (1,0,1)
+/- (0,1,1)
+/- (2,0,1)
+/- (0,2,1)
+/- (1,1,1)
In that order (left to right)
Red states = missionaries get eaten
Yellow states = repeated states
Breadth-first search on
Missionaries & Cannibals

States are generated by applying:


+/- (1,0,1)
+/- (0,1,1)
+/- (2,0,1)
+/- (0,2,1)
+/- (1,1,1)
In that order (left to right)
Red states = missionaries get eaten
Yellow states = repeated states
Breadth-first search on
Missionaries & Cannibals

States are generated by applying:


+/- (1,0,1)
+/- (0,1,1)
+/- (2,0,1)
+/- (0,2,1)
+/- (1,1,1)
In that order (left to right)
Red states = missionaries get eaten
Yellow states = repeated states
Breadth-first search on
Missionaries & Cannibals

States are generated by applying:


+/- (1,0,1)
+/- (0,1,1)
+/- (2,0,1)
+/- (0,2,1)
+/- (1,1,1)
In that order (left to right)
Red states = missionaries get eaten
Yellow states = repeated states
Breadth-first search on
Missionaries & Cannibals

States are generated by applying:


+/- (1,0,1)
+/- (0,1,1)
+/- (2,0,1)
+/- (0,2,1)
+/- (1,1,1)
In that order (left to right)
Red states = missionaries get eaten
Yellow states = repeated states
Breadth-first search on
Missionaries & Cannibals

States are generated by applying:


+/- (1,0,1)
+/- (0,1,1)
+/- (2,0,1)
+/- (0,2,1)
+/- (1,1,1)
In that order (left to right)
Red states = missionaries get eaten
Yellow states = repeated states
Breadth-first search on
Missionaries & Cannibals

States are generated by applying:


+/- (1,0,1)
+/- (0,1,1)
+/- (2,0,1)
+/- (0,2,1)
+/- (1,1,1)
In that order (left to right)
Red states = missionaries get eaten
Yellow states = repeated states
Breadth-first search on
Missionaries & Cannibals

States are generated by applying:


+/- (1,0,1)
+/- (0,1,1)
+/- (2,0,1)
+/- (0,2,1)
+/- (1,1,1)
In that order (left to right)
Red states = missionaries get eaten
Yellow states = repeated states
Breadth-first search on
Missionaries & Cannibals

States are generated by applying:


+/- (1,0,1)
+/- (0,1,1)
+/- (2,0,1)
+/- (0,2,1)
+/- (1,1,1)
In that order (left to right)
Red states = missionaries get eaten
Yellow states = repeated states
Breadth-first search on
Missionaries & Cannibals

States are generated by applying:


+/- (1,0,1)
+/- (0,1,1)
+/- (2,0,1)
+/- (0,2,1)
+/- (1,1,1)
In that order (left to right)
Red states = missionaries get eaten
Yellow states = repeated states
Breadth-first search on
Missionaries & Cannibals
-(0,2,1) [2 cannibals cross L  R] 
+(0,1,1) [1 cannibal crosses R  L]
-(0,2,1) [2 cannibals cross L  R]
+(0,1,1) [1 cannibal crosses R  L]
-(2,0,1) [2 missionaries cross L  R]
+(1,1,1) [1 cannibal & 1 missionary
cross R  L]
-(2,0,1) [2 missionaries cross L  R]
+(0,1,1) [1 cannibal crosses R  L]
-(0,2,1) [2 cannibals cross L  R]
+(1,0,1) [1 missionary crosses R  L]
-(1,1,1) [1 cannibal & 1 missionary
cross L  R]

This is an optimal solution (minimum


number of crossings). [Why?]
Would Depth-first work?
Breadth-first search on
Missionaries & Cannibals
-(0,2,1) [2 cannibals cross L  R] 
+(0,1,1) [1 cannibal crosses R  L]
-(0,2,1) [2 cannibals cross L  R]
+(0,1,1) [1 cannibal crosses R  L]
-(2,0,1) [2 missionaries cross L  R]
+(1,1,1) [1 cannibal & 1 missionary
cross R  L]
-(2,0,1) [2 missionaries cross L  R]
+(0,1,1) [1 cannibal crosses R  L]
-(0,2,1) [2 cannibals cross L  R]
+(1,0,1) [1 missionary crosses R  L]
-(1,1,1) [1 cannibal & 1 missionary
cross L  R]

This is an optimal solution (minimum


number of crossings).
Would Depth-first work?
Breadth-first search on
Missionaries & Cannibals

Expanded 48 nodes

Depth-first search on
Missionaries & Cannibals
Expanded 30 nodes

(if repeated states are checked,


otherwise we end up in an
endless loop)
The Farmer, Wolf, Duck, Corn Problem
Farmer, Wolf, Goat, Cabbage
Farmer, Fox, Chicken, Corn
Farmer Dog, Rabbit, Lettuce

A farmer with his wolf, duck and bag of corn come to the east side of a river
they wish to cross. There is a boat at the rivers edge, but of course only the
farmer can row. The boat can only hold two things (including the rower) at any
one time. If the wolf is ever left alone with the duck, the wolf will eat it.
Similarly if the duck is ever left alone with the corn, the duck will eat it. How
can the farmer get across the river so that all four arrive safely on the other side?
This means that
everybody/everything is on the
same side of the river.
FWDC

This means that we somehow got


the Wolf to the other side. F DC
W
F WD C

WD C D C W C WD
F F W F D F C

Search Tree for “Farmer, Wolf, Duck, Illegal State


Corn”
F WD C

WD C D C W C WD
F F W F D F C

F W C F WD C
D

Search Tree for “Farmer, Wolf, Duck, Illegal State Repeated


Corn”
State
F WD C

WD C D C W C WD
F F W F D F C

F W C F WD C
D

W C C W
F D F WD F D C

F C F W C F D C F W F WD F W C
WD D W D C C D

D C C D WD D W
F W F WD F W C F C F W C F D C

F D F WD F D C
W C C W

D
F W C F WD C

Search Tree for “Farmer, Wolf, Duck, Illegal State Repeated Goal State
Corn”
State
F WD C F WD C
Initial State

W C
F D F
W
D
C Farmer takes duck to left bank

F W C
D
F W
D
C Farmer returns alone

C
F WD F WD
C Farmer takes wolf to left bank

F D C
W
F
W
D C
Farmer returns with duck

D D
F W C F W C
Farmer takes corn to left bank

F D F D
W C W C Farmer returns alone

F WD C F WD C Farmer takes duck to left bank

Success!
References
 Example taken from: http://algorithmicthoughts.wordpress.com
/2012/12/15/artificial-intelligence-uniform-cost-searchucs/

35

You might also like