Professional Documents
Culture Documents
Search Problem
Search Problem
solving process.
Search Space
River2
Island1 Island2
River1
Graph structure
• Nodes = {a, b, c, d, e}
• Arcs = {(a,b), (a,d), (b,c),….}
b d
c
e
a
Tree
• A tree is a graph in which two nodes have
at most one path between them.
• The tree has a root.
b c d
e f g h i j
Space representation
1 2 3
8 4
7 6 5
1 2 3
8 4
7 6 5
1 2 3 1 4 3 1 4 3 1 4 3
7 4 6 8 7 6 7 8 6 7 6 4
5 8 2 5 8 2 5 6 2 5 8 2
1 1 3 1 3 3 1 4 3 1 4 3
7 4 6 7 4 6 1 7 6 5 7 6
5 8 2 5 8 2 5 8 2 7 8 2
A simple example:
traveling on a graph
C 2
B 9
2
3 F goal state
start state A D E
4
3 4
Search tree
,state = A
cost = 0
,state = B ,state = D
cost = 3 cost = 3
,state = C ,state = F
cost = 5 cost = 12
!goal state
,state = A
cost = 7
!search tree nodes and states are not the same thing
Full search tree
,state = A
cost = 0
,state = B ,state = D
cost = 3 cost = 3
!goal state
,state = A
,state = F
cost = 7
cost = 11
!goal state
,state = B ,state = D
.. cost = 10
.. cost = 10
. .
Problem types
• Deterministic, fully observable single-state
problem
– Solution is a sequence of states
• Non-observable sensorless problem
– Problem-solving may have no idea where it is;
solution is a sequence
• Nondeterministic and/or partially observable
Unknown state space
Algorithm types
• There are two kinds of search algorithm
– Complete
• guaranteed to find solution or prove there is
none
– Incomplete
• may not find a solution even when it exists
• often more efficient (or there would be no
point)
Comparing Searching Algorithms:
?Will it find a solution? the best one
Def. : A search algorithm is complete if
whenever there is at least one solution, the
algorithm is guaranteed to find it within a finite
amount of time.
destination city?
London
State space representation (salesman)
• State:
– the list of cities that are already visited
• Ex.: ( NewYork, Boston )
• Initial state:
• Ex.: ( NewYork )
• Rules:
– add 1 city to the list that is not yet a member
– add the first city if you already have 5 members
• Goal criterion:
– first and last city are equal
Example: The 8-puzzle
1 3 1 4 3 1 4 3 1 4 3
7 4 6 7 6 7 8 6 7 6
5 8 2 5 8 2 5 2 5 8 2
1 3 1 3 4 3 1 4 3
7 4 6 7 4 6 1 7 6 5 7 6
5 8 2 5 8 2 5 8 2 8 2
Example: Chess
• Problem: develop a program that plays chess
1. A way to represent board situations
Ex.:
8 List:
7 (( king_black, 8, C),
6 ( knight_black, 7, B),
5 ( pawn_black, 7, G),
4 ( pawn_black, 5, F),
3 ( pawn_white, 2, H),
2 ( king_white, 1, E))
1
A B C D E F G H
Chess
search tree
~15
Move 1
Move 2
~ (15)2
~ (15)3 Move 3
AND-OR-tree?
C
A B
Goal: A on B and B on C
AND
C C
A B A B
Goal: A on B Goal: B on C
Search in State Spaces
Goal states
Goal states
A D • Move
downward
B D A E s, level by
level, until
C E E B B F goal is
reached.
D F B F C E A C G
G C G F
G
O(bm)
O(bm)
50
Using Breadth-first Search
• When is BFS appropriate?
• space is not a problem
• it's necessary to find the solution with the fewest arcs
• When there are some shallow solutions
• there may be infinite paths
D F • Return to left-over
alternatives (higher-up)
G
only when needed.
Depth-first search
• Expand deepest unexpanded node
– fringe = LIFO queue, i.e., put successors at front
–
:Implementation •
Depth-first search
• Expand deepest unexpanded node
– fringe = LIFO queue, i.e., put successors at front
–
:Implementation •
Depth-first search
• Expand deepest unexpanded node
– fringe = LIFO queue, i.e., put successors at front
–
:Implementation •
Depth-first search
• Expand deepest unexpanded node
– fringe = LIFO queue, i.e., put successors at front
–
:Implementation •
Depth-first search
• Expand deepest unexpanded node
– fringe = LIFO queue, i.e., put successors at front
–
:Implementation •
Depth-first search
• Expand deepest unexpanded node
– fringe = LIFO queue, i.e., put successors at front
–
:Implementation •
Depth-first search
• Expand deepest unexpanded node
– fringe = LIFO queue, i.e., put successors at front
–
:Implementation •
Depth-first search
• Expand deepest unexpanded node
– fringe = LIFO queue, i.e., put successors at front
–
:Implementation •
Depth-first search
• Expand deepest unexpanded node
– fringe = LIFO queue, i.e., put successors at front
–
:Implementation •
Depth-first search
• Expand deepest unexpanded node
– fringe = LIFO queue, i.e., put successors at front
–
:Implementation •
Depth-first search
• Expand deepest unexpanded node
– fringe = LIFO queue, i.e., put successors at front
–
:Implementation •
Depth-first search
• Expand deepest unexpanded node
– fringe = LIFO queue, i.e., put successors at front
–
:Implementation •
• Is DFS complete?
Analysis of DFS
.
• Is DFS optimal?
We will look at the answers in AISpace (but see next few slides for a
summary of what we do)
Analysis of DFS
Def. : A search algorithm is complete if whenever there is at least
one
solution, the algorithm is guaranteed to find it within a finite
amount of time.
Is DFS complete? No
• If there are cycles in the graph, DFS may get “stuck” in one of them
• see this in AISpace by loading “Cyclic Graph Examples” or by adding a
cycle to “Simple Tree”
• e.g., click on “Create” tab, create a new edge from N7 to N1, go back to
“Solve” and see what happens
Analysis of DFS
Def.: A search algorithm is optimal if
when it finds a solution, it is the best one (e.g., the shortest)
Is DFS optimal? No
• see this in AISpace by loading “Extended Tree Graph” and set N6 as a goal
• e.g., click on “Create” tab, right-click on N6 and select “set as a goal node”
68
Analysis of DFS
Def.: The time complexity of a search algorithm is
the worst-case amount of time it will take to run,
expressed in terms of
- maximum path length m
- maximum forward branching factor b.
O(bm)
- for every node in the path currently explored, DFS maintains a path to its unexplored siblings in the search tree
- Alternative paths that DFS needs to explore
- The longest possible path is m, with a maximum of b-1 alterative paths per node
O(bm)
B C D E F
Goal state
G H I J K L M N O P
Q R S T U V W X Y Z
G H I J K L M N O P
Node L is located and the
Q R S T U search returns a solution.
.Press space to end
Press
Press space
space to
to continue
begin thethe
search
search
Size
SizeofofQueue:
Queue:0
1
5
6
7
8
9 Queue:
Queue:
Queue:
Queue:
Queue:
K,
L,
J,
G,
H,
I,Queue:
K,
J,
M,
L,
F,
H,
I,
Queue:
K,
J,
M,
L,
G,
N,
I,Queue:
E,K,
L,
M,
J,
O,
N,
H,
F,
D,
M,
K,
L,
C,
N,
O,
P,
I,
G,
Queue:
E,M,
L,
J,
N,
D,
O,
Q,
B,
P,
H,
F,K,
M,
N,
O,
E,
Q,
C,
P,
R,
Queue:
G,
I,L,
O,
F,
N,
P,
Q,
D,
J,
R,
S,
Empty
H,M,
G,
Q,
K,
O,
P,
T,
R,
E,
S,I,Q
H
R
N
A
U
L
T
P
F
SJ
10 Current
Nodes FINISHED
Action:
:Current
Current
SEARCH
Expanding
Action:
Action Current Currentlevel:
level:2
0
1
expanded:
expanded:11
109
0
1
2
3
4
5
6
7
8 Backtracking
BREADTH-FIRST SEARCH PATTERN n/a
Aside: Internet Search
• Typically human search will be “incomplete”,
• E.g. finding information on the internet before
google, etc
– look at a few web pages,
– if no success then give up
Example
• Determine whether data-driven or goal-
driven and depth-first or breadth-first would
be preferable for solving each of the
following
– Diagnosing mechanical problems in an
automobile (goal-depth)
– You have met a person who claims to be your
distant cousin, with a common ancestor named
John. You like to verify her claim (goal-breadth)
– A theorem prover for plane geometry(data-depth)
Example
• A program for examining sonar readings and
interpreting them (data"(" ت!!!مشيج!ولب!!!سدا!تا ا!حسن-
تنفعdepth , breadth برضو
Start Goal
78
Example Search Problem
• A genetics professor
– Wants to name her new baby boy
– Using only the letters D,N & A
• Search through possible strings (states)
– D,DN,DNNA,NA,AND,DNAN, etc.
– 3 operators: add D, N or A onto end of string
– Initial state is an empty string
• Goal test
– Look up state in a book of boys’ names, e.g. DAN
G(n) = The cost of each move as the distance between each town
H(n) = The Straight Line Distance between any town and town M.
A 40 B
12
C 10 D 23 10
5
20 E F 10
G H
10 5
I J
10 10
15
5
K 20 20 M
L
A 45 E 32 I 12 M 0
B 20 F 23 J 5
C 34 G 15 K 40
D 25 H 10 L 20
• Consider the following search problem. Assume a state is represented as
an integer, that the initial state is the number 1, and that the two
successors of a state n are the states 2n and 2n+1. For example, the
successors of 1 are 2 and 3, the successors of 2 are 4 and 5, the successors
of 3 are 6 and 7, etc. Assumes the goal state is the number 12. Consider
the following heuristics for evaluating the state n where the goal state is g
• h1(n) = |n-g| & h2(n) = (g – n) if (n g) and h2 (n) = if (n >g)
• Show the search trees generated for each of the following strategies for
the initial state 1 and the goal state 12, numbering the nodes in the order
expanded.
• Depth-first search b) Breadth-first search
• c) beast-first with heuristic h1 d) A* with heuristic (h1+h2)
• If any of these strategies get lost and never find the goal, then show the
few steps and say "FAILS"