Professional Documents
Culture Documents
(CS-401)
Lecture 3:
Problem Solving Search
3
Searching and AI
• Searching falls under Artificial Intelligence (AI).
7
Searching in AI
8
Problem Formulation
1. State
2. Initial state
3. Actions or Successor Function
4. Goal Test
5. Path Cost
6. Solution
9
Problem Formulation (The Romania Example)
Solution: a sequence of actions leading from the initial state to a goal state
{Arad Sibiu Rimnicu Vilcea Pitesti Bucharest} 8
Problem Formulation (The 8- Puzzle Example)
State: The location of the eight tiles, and the blank
Robot Navigation
• States
– locations
– position of actuators
• Initial state
– start position (dependent on the task)
• Successor function (operators)
– movement, actions of actuators
• Goal test
– task-dependent
• Path cost
– maybe very complex
• distance, energy consumption
15
Problem Formulation (Real-life Applications)
• States
– location of components
• Initial state
– no components assembled
• Successor function (operators)
– place component
• Goal test
– system fully assembled
• Path cost
– number of moves
16
Searching for Solutions
General procedure
– Check for goal state
– Expand the current state
• Determine the set of reachable states
• Return “failure” if the set is empty
– Select one from the set of reachable states
– Move to the selected state
Search Tree
– Generated as the search space is traversed
• The search space itself is not necessarily a tree, frequently it is a
graph
• The tree specifies possible paths through the search space
– Expansion of nodes
• As states are explored, the corresponding nodes are expanded by
applying the successor function
– this generates a new set of (child) nodes
• The fringe (frontier/queue) is the set of nodes not yet visited
– newly generated nodes are added to the fringe
– Search strategy
• Determines the selection of the next node to be expanded
• Can be achieved by ordering the nodes in the fringe
– e.g. queue (FIFO), stack (LIFO), “best” node w.r.t. some measure (cost)
18
Search Tree Vs Graph Tree
BASIS FOR
TREE GRAPH
COMPARISON
Path Only one between two More than one path is
vertices. allowed.
Root node It has exactly one root Graph doesn't have a
node. root node.
Loops No loops are permitted. Graph can have loops.
1
1
A4 D3 3
1 1
5 2
S3 C2 G0
3 3
1 4
B2 E1
Uninformed Search
– breadth-first Informed Search
– uniform-cost search – best-first search
– depth-first – search with heuristics
– depth-limited search – memory-bounded search
– iterative deepening – iterative improvement search
– bi-directional search
30
Breadth-First Search
Algorithm (Informal)
1. Enqueue the root/initial node (Queue Structure).
2. Dequeue a node and examine it.
1. If the element sought is found in this node, quit the search and
return a result.
2. Otherwise enqueue any successors (the direct child nodes) that
have not yet been discovered.
3. If the queue is empty, every node on the graph has been
examined – quit the search and return "not found".
4. Repeat from Step 2.
32
Breadth-First Search
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
Fringe: [] + [2,3]
24
Breadth-First Snapshot 2
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
Fringe: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21] + [22,23]
34
Breadth-First Snapshot 12
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
Note:
The goal node is
8 9 10 11 12 13 14 15 “visible” here, but
we can not
perform the goal
test yet.
16 17 18 19 20 21 22 23 24 25
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
39
Breadth-First Snapshot 17
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [18,19,20,21,22,23,24,25,26,27,28,29,30,31]
40
Breadth-First Snapshot 18
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [19,20,21,22,23,24,25,26,27,28,29,30,31]
41
Breadth-First Snapshot 19
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [20,21,22,23,24,25,26,27,28,29,30,31]
42
Breadth-First Snapshot 20
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [21,22,23,24,25,26,27,28,29,30,31]
43
Breadth-First Snapshot 21
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [22,23,24,25,26,27,28,29,30,31]
44
Breadth-First Snapshot 22
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [23,24,25,26,27,28,29,30,31]
45
Breadth-First Snapshot 23
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [24,25,26,27,28,29,30,31]
46
Breadth-First Snapshot 24
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
Note:
The goal test
8 9 10 11 12 13 14 15
is positive for
this node, and
a solution is
found in 24
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
steps.
Fringe: [25,26,27,28,29,30,31]
47
Example BFS
B S
G C
F D
H E
Solve using BFS
ABDGEFCH
B F
C
A
E H
D
G
Properties of Breadth-First Search (BFS)
Optimality: Yes
b Branching Factor
d The depth of the goal
61
Uniform-Cost -First
63
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 0, expanded: 0 start
Closed Open Node
Node 5 2 4
{S}
A B C
9 4 6 2
6 G 1
D E goal F
H
64
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 1, expanded: 1 start
Closed Open Node
Node 5 2 4
{S:0}
A B C
S not goal {B:2,C:4,A:5}
9 4 6 2
6 G 1
D E goal F
H
65
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 2, expanded: 2 start
Closed Open Node
Node 5 2 4
{S}
A B C
S {B:2,C:4,A:5}
B not goal {C:4,A:5,G:2+6}
9 4 6 2
6 G 1
D E goal F
H
66
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 3, expanded: 3 start
Closed Open Node
Node 5 2 4
{S}
A B C
S {B:2,C:4,A:5}
B {C:4,A:5,G:8}
9 4 6 2
C not goal {A:5,F:4+2,G:8}
6 G 1
D E goal F
H
67
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 4, expanded: 4 start
Closed Open Node
Node 5 2 4
{S}
A B C
S {B:2,C:4,A:5}
B {C:4,A:5,G:8}
9 4 6 2
C {A:5,F:6,G:8}
A not goal {F:6,G:8,E:5+4, 6 G 1
D E goal F
D:5+9}
7
H
68
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 5, expanded: 5 start
H
69
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 6, expanded: 5 start
Closed Open Node
Node 5 2 4
{S}
A B C
S {B:2,C:4,A:5}
B {C:4,A:5,G:8}
9 4 6 2
C {A:5,F:6,G:8}
A {F:6,G:8,E:9,D:14} 6 G 1
D E goal F
F {G:7,G:8,E:9,D:14}
G goal {G:8,E:9,D:14} 7
no expand
H
70
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 6, expanded: 5 start
Closed Open Node
Node 5 2 4
{S}
A B C
S {B:2,C:4,A:5}
B {C:4,A:5,G:8}
9 4 6 2
C {A:5,F:6,G:8}
A {F:6,G:8,E:9,D:14} 6 G 1
D E goal F
F {G:7,G:8,E:9,D:14}
G {G:8,E:9,D:14} 7
path: S,C,F,G
H cost: 7
71
Uniform-Cost Search (UCS)
5 1
S B C
6 2 3
5 4
D E F
3 7 1
5
G 1 H
72
Properties of Uniform-cost Search (UCS)
75
Depth-First Search
Based on [4]
76
Depth-First Search
1. Start
2. Push Root Node to Stack
• Mark Root Node as
Visited
• Print Root Node as Output
3. Check Top of the Stack If
Stack is Empty, Go to Step 6
4. Else, Check Adjacent Top of
the Stack
• If Adjacent is not Visited
• Push Node to Stack
• Mark Node as Visited
• Print Node as Output
• Else Adjacent Visited
5. Go to Step 3
6. Stop
77
Example DFS
B S
G C
F D
H E
Example DFS ABEGFCHD
B F
C
A
E H
D
G
Example DFS
AGEDFCHB
B F
C
A
E H
D
G
Properties of Depth-First Search
Space: O(bm)
Optimal: No
Depth-first goes off into one branch until it reaches a leaf node
– Not good if the goal is on another branch
– Neither complete nor optimal
– Uses much less space than breadth-first
• Much fewer visited nodes to keep track, smaller fringe
Breadth-first is more careful by checking all alternatives
– Complete and optimal (Under most circumstances)
– Very memory-intensive
For a large tree, breadth-first search memory requirements maybe
excessive
For a large tree, a depth-first search may take an excessively long time to
find even a very nearby goal node.
How can we combine the advantages (and avoid the disadvantages) of
these two search techniques?
82
4- Depth-Limited Search
B C D
E F
H G
84
5- Iterative Deepening Depth-First Search
85
Iterative Deepening Depth-First Search
86
Iterative deepening search l =0
87
Iterative deepening search l =1
88
Iterative deepening search l =2
89
Iterative deepening search l =3
90
Example IDS
B C D
E F G
91
Iterative Deepening Depth-First Search
92
Properties of Iterative Deepening Search
Space: O(bd)
b branching factor
d Tree/goal depth
93
Iterative Deepening Search
• Suppose b = 10, d = 5,
NIDS = 50 + 400 + 3,000 + 20,000 + 100,000 = 123,456
NBFS = 1 + 10 + 100 + 1,000 + 10,000 + 100,000 = 111,111
IDS behaves better in case the search space is large and the depth of
goal is unknown.
94
Iterative Deepening Search
Based on [4]
The higher the branching factor, the lower the relative cost of
iterative deepening depth first search
95
6- Bi-directional Search
S
Forward
A D Backwards
B D A E
C E E B B F
11
D F B F C E A C G
14 17 15 15 13
G C G F
19 19 17
G 25
6- Bi-directional Search Example
B E Y
A C H J I G
D F K
L
6- Bi-directional Search
Breadth-First Search:
– Some solutions are known to be shallow
Uniform-Cost Search:
– Actions have varying costs
– Least cost solution is the required
This is the only uninformed search that worries about costs.
Depth-First Search:
– Many solutions exist
– Know (or have a good estimate of) the depth of solution
Iterative-Deepening Search:
– Space is limited and the shortest solution path is required
Summary