Lesson 04 - Problem Solving by Searching
Lesson 04 - Problem Solving by Searching
• Formulate goal:
– be in Bucharest
• Formulate problem:
– states: various cities
– actions: drive between cities
• Find solution:
– sequence of cities, e.g., Arad, Sibiu, Fagaras, Bucharest
Another graph example
• However, graphs can also be much more abstract.
• To use this approach, we must specify what are the states, the
actions and the goal test.
• states?
• actions?
• goal test?
• path cost?
Example: The 8-puzzle
Loop do
if nodes is empty then return failure
node REMOVE_FRONT (nodes )
if GOAL_TEST [problem ] applied to STATE ( node ) succeeds
then return node
End
Function SIMPLE-PROBLEM-SOLVING-AGENT(percept) returns an action
• Breadth-first search
• Uniform-cost search
• Depth-first search
• Depth-limited search
• Iterative deepening search
• Bidirectional search
Breadth-first search
A D
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
Performance of Breadth-First Search
• Good news
– It is complete
– It is optimal, as long as the path cost function is non-
decreasing (e.g., if all actions have the same cost)
• Bad news
– Time and (especially) space complexity can be
prohibitively high
• Has to keep all nodes in memory: queue gets large
• If d is large, takes a long time to reach a goal
The Properties of breadth-first
search
• Complete?
– Yes (if b is finite)
• Time?
– 1+b+b2+b3+… +bd + b(bd-1) = O(bd+1)
• Space?
– O(bd+1) (keeps every node in memory)
• Optimal?
– Yes (if cost is a non-decreasing function of depth, e.g. when we
have 1 cost per step)
Uniform-cost search
• Expand least-cost unexpanded node.
• The algorithm expands nodes in order of increasing path cost.
• Therefore, the first goal node selected for expansion is the optimal solution.
• Implementation:
– fringe = queue ordered by path cost (priority queue)
• Equivalent to breadth-first if step costs all equal
• Time? number of nodes with g ≤ cost of optimal solution, O(bC*/ ε) where C* is the
cost of the optimal solution
A D
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
Depth-first search (Analysis)
• Not complete
– because a path may be infinite or looping
– then the path will never fail and go back try another option
• Not optimal
– it doesn't guarantee the best solution
• It overcomes (Good NEWS)
– the time and space complexities
Properties of depth-first search
• Complete? No: fails in infinite-depth spaces, spaces with loops
– Modify to avoid repeated states along path
complete in finite spaces
• Time? O(bm): terrible if m is much larger than d
– but if solutions are dense, may be much faster than breadth-first
• Space? O(bm), i.e., linear space!
• Optimal? No
Depth-Limited Search (DLS)
nodePT = Pop(fringe);
if (GoalTest(nodePT->state))
return nodePT;
• For b = 10, d = 5,
– NBFS = 1 + 10 + 100 + 1,000 + 10,000 + 100,000 = 111,111
–
– NIDS = 6 + 50 + 400 + 3,000 + 20,000 + 100,000 = 123,456
–
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
Avoiding Repeated States
Yes,
Complete? Yes Yes No If l d Yes Yes
Time bd bd bm bl bd bd/2
Space bd bd bm bl bd bd/2