Chapter 2
Chapter 2
Searching – Part#1
Chapter 3
♦ Problem-solving agents
♦ Example Problems
♦ Problem formulation
♦ Search Algorithms
♦ Uninformed Search
Strategies
♦ Informed (Heuristic)
Search Strategies
♦ Heuristic Functions
Oradea
71
Neamt
Zerind 87
75 151
Iasi
Arad
140
92
Sibiu Fagaras
99
118 Vaslui
80
Timisoara Rimnicu Vilcea
142
111 Pitesti 211
Lugoj 97
70 98
146 85 Hirsova
Mehadia 101 Urziceni
75 138 86
Bucharest
Dobreta 120
90
Craiova Eforie
Giurgiu
Example: Romania
Find solution:
sequence of cities,
e.g., Arad, Sibiu, Fagaras,
Bucharest
Problem types
An optimal solution has the lowest path cost among all solutions
Abstraction when formulating problems
R
L R
S S
R R
L R L R
L L
S S
S S
R
L R
S S
states??
actions??
goal
test??
path
cost??
Example: vacuum world state space
R
L R
S S
R R
L R L R
L L
S S
S S
R
L R
S S
states??: Integer dirt and robot locations (ignore dirt amounts etc.)
actions??
goal test??
path cost??
Example: vacuum world state space
R
L R
S S
R R
L R L R
L L
S S
S S
R
L R
S S
R
L R
S S
R R
L R L R
L L
S S
S S
R
L R
S S
R
L R
S S
R R
L R L R
L L
S S
S S
R
L R
S S
7 2 4 1 2
5 6 3 4 5
8 3 1 6 7 8
states??
actions??
goal
test??
path
cost??
Example: The 8-puzzle
7 2 4 1 2
5 6 3 4 5
8 3 1 6 7 8
7 2 4 1 2
5 6 3 4 5
8 3 1 6 7 8
7 2 4 1 2
5 6 3 4 5
8 3 1 6 7 8
7 2 4 1 2
5 6 3 4 5
8 3 1 6 7 8
Basic idea:
offline, simulated exploration of state space
by generating successors of already-explored
states (a.k.a. expanding states)
Arad
Arad
Arad
7 3 2 2
The Expand function creates new nodes, filling in the various fields
and using the SuccessorFn of the problem to create the
corresponding states.
Implementation: General Tree Search
▪ We can rarely build this full graph in Tiny search graph for a tiny
memory (it’s too big), but it’s a useful idea search problem
Search strategies
B C
D E F G
Breadth-first Search
D E F G
Breadth-first Search
D E F G
Breadth-first Search
D E F G
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 0, expanded: 0 start
expnd. node Frontier list
{S} 5 2 4
A B C
9 4 6 2
6 G 1
D E goal F
38
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 1, expanded: 1 start
expnd. node Frontier list
{S} 5 2 4
S not goal {A,B,C}
A B C
9 4 6 2
6 G 1
D E goal F
39
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 2, expanded: 2 start
expnd. node Frontier list
{S} 5 2 4
S {A,B,C}
A B C
A not goal {B,C,D,E}
9 4 6 2
6 G 1
D E goal F
40
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 3, expanded: 3 start
expnd. node Frontier list
{S} 5 2 4
S {A,B,C}
A B C
A {B,C,D,E}
B not goal {C,D,E,G}
9 4 6 2
6 G 1
D E goal F
41
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 4, expanded: 4 start
expnd. node Frontier list
{S} 5 2 4
S {A,B,C}
A B C
A {B,C,D,E}
B {C,D,E,G}
9 4 6 2
C not goal {D,E,G,F}
6 G 1
D E goal F
42
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 5, expanded: 5 start
expnd. node Frontier list
{S} 5 2 4
S {A,B,C}
A B C
A {B,C,D,E}
B {C,D,E,G}
9 4 6 2
C {D,E,G,F}
6 G 1
D not goal {E,G,F,H} D E goal F
43
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 6, expanded: 6 start
expnd. node Frontier list
{S} 5 2 4
S {A,B,C}
A B C
A {B,C,D,E}
B {C,D,E,G}
9 4 6 2
C {D,E,G,F}
6 G 1
D {E,G,F,H} D E goal F
E not goal {G,F,H,G}
7
44
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 7, expanded: 6 start
expnd. node Frontier list
{S} 5 2 4
S {A,B,C}
A B C
A {B,C,D,E}
B {C,D,E,G}
9 4 6 2
C {D,E,G,F}
6 G 1
D {E,G,F,H} D E goal F
E {G,F,H,G}
G goal {F,H,G} no expand 7
45
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 7, expanded: 6 start
expnd. node Frontier list
{S} 5 2 4
S {A,B,C}
A B C
A {B,C,D,E}
B {C,D,E,G}
9 4 6 2
C {D,E,G,F}
6 G 1
D {E,G,F,H} D E goal F
E {G,F,H,G}
G {F,H,G} 7
path: S,B,G
H cost: 8
46
Properties of breadth-first search
B C
D E F G
H I J K L M N O
Depth-first Search
B C
D E F G
H I J K L M N O
Depth-first Search
B C
D E F G
H I J K L M N O
Depth-first Search
B C
D E F G
H I J K L M N O
Depth-first Search
B C
D E F G
I J K L M N O
Depth-first Search
B C
E F G
J K L M N O
Depth-first Search
B C
E F G
J K L M N O
Depth-first Search
B C
E F G
K L M N O
Depth-first Search
F G
L M N O
Depth-first Search
F G
L M N O
Depth-first Search
F G
L M N O
Depth-first Search
F G
M N O
Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 0, expanded: 0 start
expnd. node Frontier
{S}
5 2 4
A B C
9 4 6 2
6 G 1
D E goal F
61
Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 1, expanded: 1 start
expnd. node Frontier
{S}
5 2 4
S not goal {A,B,C}
A B C
9 4 6 2
6 G 1
D E goal F
62
Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 2, expanded: 2 start
expnd. node Frontier
{S}
5 2 4
S {A,B,C}
A B C
A not goal {D,E,B,C}
9 4 6 2
6 G 1
D E goal F
63
Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 3, expanded: 3 start
expnd. node Frontier
{S}
5 2 4
S {A,B,C}
A B C
A {D,E,B,C}
D not goal {H,E,B,C}
9 4 6 2
6 G 1
D E goal F
64
Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 4, expanded: 4 start
expnd. node Frontier
{S}
5 2 4
S {A,B,C}
A B C
A {D,E,B,C}
D {H,E,B,C}
9 4 6 2
H not goal {E,B,C}
6 G 1
D E goal F
65
Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 5, expanded: 5 start
expnd. node Frontier
{S}
5 2 4
S {A,B,C}
A B C
A {D,E,B,C}
D {H,E,B,C}
9 4 6 2
H {E,B,C}
6 G 1
E not goal {G,B,C} D E goal F
66
Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 6, expanded: 5 start
expnd. node Frontier
{S}
5 2 4
S {A,B,C}
A B C
A {D,E,B,C}
D {H,E,B,C}
9 4 6 2
H {E,B,C}
6 G 1
E {G,B,C} D E goal F
G goal {B,C} no expand
7
67
Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 6, expanded: 5 start
expnd. node Frontier
{S}
5 2 4
S {A,B,C}
A B C
A {D,E,B,C}
D {H,E,B,C}
9 4 6 2
H {E,B,C}
6 G 1
E {G,B,C} D E goal F
G {B,C}
7
path: S,A,E,G
H cost: 15
68
Properties of Depth-first Search
Idea: Iteratively increase the search limit until the depth of the
shallowest solution d is reached.
Because most of the nodes are on the bottom of the search tree, it
not a big waste to iteratively re-generate the top
Limit = 0 A
Iterative Deepening Search
Limit = 1 A A A
B C B C C
Iterative Deepening Search
Limit = 2 A A A A
B C B C B C B C
D E F G D E F G D E F G E F G
A A A
C C C
F G F G G
Iterative Deepening Search
Limit = 3 A A A A
B C B C B C B C
D E F G D E F G D E F G D E F G
H I J K L M N O H I J K L M N O H I J K L M N O H I J K L M N O
A A A A
B C B C B C B C
D E F G E F G E F G E F G
I J K L M N O J K L M N O J K L M N O K L M N O
A A A A
C C C C
F G F G F G F G
L M N O L M N O L M N O M N O
Iterative Deepening Search
Complete?? Yes
Time?? (d + 1)b0 + db1 + (d − 1)b2 + . . . + bd = O(bd)
Space?? O(bd)
IDS does better because other nodes at depth d are not expanded BFS
can be modified to apply goal test when a node is generated
Summary of Uninformed Search algorithms
Uniform-cost search
generalSearch(problem, priorityQueue)
S
# of nodes tested: 0, expanded: 0 start
expnd. node Frontier list
{S}
5 2 4
A B C
9 4 6 2
6 G 1
D E goal F
80
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 1, expanded: 1 start
expnd. node Frontier list
{S:0}
5 2 4
S not goal {B:2,C:4,A:5}
A B C
9 4 6 2
6 G 1
D E goal F
81
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 2, expanded: 2 start
expnd. node Frontier list
{S}
5 2 4
S {B:2,C:4,A:5}
A B C
B not goal {C:4,A:5,G:2+6}
9 4 6 2
6 G 1
D E goal F
82
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 3, expanded: 3 start
expnd. node Frontier list
{S}
5 2 4
S {B:2,C:4,A:5}
A B C
B {C:4,A:5,G:8}
C not goal {A:5,F:4+2,G:8}
9 4 6 2
6 G 1
D E goal F
83
Unifor m-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 4, expanded: 4 start
expnd. node Frontier list
{S}
5 2 4
S {B:2,C:4,A:5}
A B C
B {C:4,A:5,G:8}
C {A:5,F:6,G:8}
9 4 6 2
A not goal {F:6,G:8,E:5+4,
D:5+9} 6 G 1
D E goal F
84
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 5, expanded: 5 start
expnd. node Frontier list
{S}
5 2 4
S {B:2,C:4,A:5}
A B C
B {C:4,A:5,G:8}
C {A:5,F:6,G:8}
9 4 6 2
A {F:6,G:8,E:9,D:14}
6 G 1
F not goal {G:4+2+1,G:8,E:9, D E goal F
D:14}
7
85
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 6, expanded: 5 start
expnd. node Frontier list
{S}
5 2 4
S {B:2,C:4,A:5}
A B C
B {C:4,A:5,G:8}
C {A:5,F:6,G:8}
9 4 6 2
A {F:6,G:8,E:9,D:14}
6 G 1
F {G:7,G:8,E:9,D:14} D E goal F
G goal {G:8,E:9,D:14}
no expand 7
86
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 6, expanded: 5 start
expnd. node Frontier list
{S}
5 2 4
S {B:2,C:4,A:5}
A B C
B {C:4,A:5,G:8}
C {A:5,F:6,G:8}
9 4 6 2
A {F:6,G:8,E:9,D:14}
6 G 1
F {G:7,G:8,E:9,D:14} D E goal F
G {G:8,E:9,D:14}
7
path: S,C,F,G
H cost: 7
87
Summary