You are on page 1of 39

Solving Problems by

Searching
Dr. Azhar Mahmood
Associate Professor
Email: azhar.mahmood@cust.edu.pk
Uninformed Search Strategies
• Uninformed search
– no information about the number of steps
– or the path cost from the current state to the goal
– search the state space blindly

• Informed search, or heuristic search


– a cleverer strategy that searches toward the goal,
– based on the information from the current state so far
Uninformed Search Strategies
• Breadth-first search
– Uniform cost search

• Depth-first search
– Depth-limited search
– Iterative deepening search
Breadth-first search
• The root node is expanded first (FIFO)
• All the nodes generated by the root node are then
expanded
• And then their successors and so on
Breadth-first search
S

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
Breadth-First Strategy
New nodes are inserted at the end of FRINGE

2 3 FRINGE = (1)

4 5 6 7
Breadth-First Strategy
New nodes are inserted at the end of FRINGE

2 3 FRINGE = (2, 3)

4 5 6 7
Breadth-First Strategy
New nodes are inserted at the end of FRINGE

2 3 FRINGE = (3, 4, 5)

4 5 6 7
Breadth-First Strategy
New nodes are inserted at the end of FRINGE

2 3 FRINGE = (4, 5, 6, 7)

4 5 6 7
Breadth-first Search (Analysis)
• Breadth-first search
– Complete – find the solution eventually
– Optimal, if step cost is 1

The disadvantage
– if the branching factor of a node is large,
– for even small instances (e.g., chess)
• the space complexity and the time complexity are
enormous
Properties of Breadth-First Search
• Complete? Yes (if b is finite)
• Time?
– Imagine searching a uniform tree where every state has b successors.
– The root of the search tree generates b nodes at the first level, each of which generates b
more nodes, for a total of b2 at the second level.
– Each of these generates b more nodes, yielding b 3 nodes at the third level, and so on.
Now suppose that the solution is at depth d. In the worst case, it is the last node generated
at that level.
– Then the total number of nodes generated is b + b2 + b3 + ···+ bd = O(bd)
– If the algorithm were to apply the goal test to nodes when selected for expansion, rather
than when generated, the whole layer of nodes at depth d would be expanded before the
goal was detected and the time complexity would be O(b d+1).)
– b+b2+b3+… +bd = O(bd+1)
• Space? :O(bd+1) (keeps every node in memory)
• Optimal? Yes (if cost = 1 per step)
Uniform cost search
• Breadth-first finds the shallowest goal state
– but not necessarily be the least-cost solution
– work only if all step costs are equal

• Uniform cost search


– modifies breadth-first strategy
• by always expanding the lowest-cost node
– The lowest-cost node is measured by the path cost g(n)
– This is done by storing the frontier as a priority queue ordered
by g.
Uniform cost search
• The first found solution is guaranteed to be the cheapest
– least in depth
– But restrict to non-decreasing path cost
– Unsuitable for operators with negative cost
Uniform-cost search
• Expand least-cost unexpanded node
• Equivalent to breadth-first if step costs all equal

• Complete? Yes, if step cost ≥ε

• Time? # of nodes with g ≤ cost of optimal solution, O(bceiling(C*/ ε)) where C*


is the cost of the optimal solution

• Space? # of nodes with g ≤ cost of optimal solution, O(bceiling(C*/ ε))

C* be the cost of optimal solution. ε is positive constant (every action cost)

• Optimal? Yes – nodes expanded in increasing


order of g(n)
• Implementation: fringe = queue ordered by path
cost
Depth-first search
• Always expands one of the nodes at the deepest level
of the tree
• Only when the search hits a dead end
– goes back and expands nodes at shallower levels
– Dead end  leaf nodes but not the goal
• Backtracking search
– Only one successor is generated on expansion
– rather than all successors
– fewer memory
Depth-first Search Algorithm
Let S be the start state
1. Initialize Q with the start node Q=(S) as only entry; set
Visited = (S)
2. If Q is empty, fail. Else pick node X from Q
3. If X is a goal, return X, we’ve reached the goal
4. (Otherwise) Remove X from Q
5. Find all the children of node X not in Visited
6. Add these to Q; Add Children of X to Visited
7. Go to Step 2
Depth-first search
• Expand deepest unexpanded node
– fringe = LIFO queue, i.e., put successors at front
• Implementation:
Is A a goal state? No, Expand A and remove from the queue

Q Visited

1 A A

2
3
4
5
Depth-first search
• Expand deepest unexpanded node
• Implementation:
– fringe = LIFO queue, i.e., put successors at front

Is B a goal state?

Q Visited

1 A A

2 BC ABC

3
4
5
Depth-first search
• Expand deepest unexpanded node
• Implementation:
– fringe = LIFO queue, i.e., put successors at front

Is D = goal state?

Q Visited

1 A A

2 BC ABC

3 DEC ABCD

4
5
Depth-first search
• Expand deepest unexpanded node
• Implementation:
– fringe = LIFO queue, i.e., put successors at front
Queue=[H,I,E,C]

Is H = goal state?
Q Visited

1 A A

2 BC ABC

3 DEC ABCDE

4 HIEC ABCDEHI

5
Depth-first search
• Expand deepest unexpanded node
• Implementation:
– fringe = LIFO queue, i.e., put successors at fron

Is I = goal state?
Q Visited

1 A A

2 BC ABC

3 DEC ABCDE

4 HIEC ABCDEHI

5 IEC ABCDEHI

ABCDEHI
Depth-first search
• Expand deepest unexpanded node
– fringe = LIFO queue, i.e., put successors at front
• Implementation:
Is E = goal state?

Q Visited

1 A A

2 BC ABC

3 DEC ABCDE

4 HIEC ABCDEHI
5 IEC ABCDEHI

6 EC ABCDEHI

7
Depth-first search
Queue Visited

1 A A

2 BC ABC Is J = goal state?


3 DEC ABCDE

4 HIEC ABCDEHI

5 IEC ABCDEHI

6 EC ABCDEHI

7 JKC ABCDEHIJK

8 KC ABCDEHIJK
Is K = goal state?
9 C ABCDEHIJK

10 FG ABCDEFHIJK

11 LMG ABCDEFGHIJKLM

12 MG ABCDEFGHIJKLM

13 G ABCDEFGHIJKLM

14 NO ABCDEFGHIJKLM

15 O ABCDEFGHIJKLMN

16 Empty ABCDEFGHIJKLMNO
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
– 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

• Time? O(bm): terrible if m is much larger than d


– m= maximum depth of any node, b= branching factor, d
= depth of the shallowest solution
– but if solutions are dense, may be much faster than
breadth-first
• Space? O(bm), i.e., linear space!
• Optimal? No

–  complete in finite spaces


Depth-Limited Strategy
• Depth-first with depth cutoff k (maximal depth
below which nodes are not expanded)

• Three possible outcomes:


– Solution
– Failure (no solution)
– Cutoff (no solution within cutoff)
Depth-limited search
• It is depth-first search
– with a predefined maximum depth
– However, it is usually not easy to define the
suitable maximum depth
– too small  no solution can be found
– too large  the same problems are suffered
from
• Anyway the search is
– complete
– but still not optimal
Depth -Limited Search
S depth = 3
A D 3
5
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-limited search
= depth-first search with depth limit l,
i.e., nodes at depth l have no successors
Recursive implementation:

29
Iterative deepening search
• No choosing of the best depth limit
• It tries all possible depth limits:
– first 0, then 1, 2, and so on
– combines the benefits of depth-first and breadth-first
search
Iterative deepening search L=0

31
Iterative deepening search L=1

14 Jan 2004 CS 3243 - Blind Search 32


Iterative deepening search L=2

14 Jan 2004 CS 3243 - Blind Search 33


Iterative deepening search L=3

14 Jan 2004 CS 3243 - Blind Search 34


Iterative deepening search
(Analysis)
• Optimal
• Complete
• Time and space complexities
– reasonable
• Suitable for the problem
– having a large search space
– and the depth of the solution is not known
Properties of iterative deepening
search
• Complete? Yes
• Time? (d+1)b0 + d b1 + (d-1)b2 + … + bd = O(bd)
• Space? O(bd)
• Optimal? Yes, if step cost = 1




Summary of algorithms

37
Summary
• Problem formulation usually requires abstracting away
real-world details to define a state space that can
feasibly be explored

• Variety of uninformed search strategies

• Iterative deepening search uses only linear space and


not much more time than other uninformed algorithms

38
Reading
• Artificial Intelligence: A Modern Approach, 3rd
edition
– Chapter 3: Solving problem by Searching
– Section 3.1- 3.4

• Informed/Heuristic Search
– Greedy best-first search
– A* search
– Heuristic functions

You might also like