You are on page 1of 19

Lecture 10

Solving Problems by
Searching
Artificial Intelligence
COSC-3112

Ms. Humaira Anwer


humairaanwer13@ce.ceme.edu.pk

Lecture 10 Solving Problems by Searching 1


Today’s Agenda
• Depth First Search

Lecture 10 Solving Problems by Searching 2


Depth-First Search
• Recall from Data Structures the basic algorithm for
a depth-first search on a graph or tree

• Expand the deepest unexpanded node

• Unexplored successors are placed on a stack until


fully explored

Lecture 10 Solving Problems by Searching 3


Depth-First Search

Stack

Output

Lecture 10 Solving Problems by Searching 4


Depth-First Search
Root-> Left Child -> Right child

Stack

Output A

Lecture 10 Solving Problems by Searching 5


Depth-First Search

B Active
A Paused
Stack

Output A B

Lecture 10 Solving Problems by Searching 6


Depth-First Search

D Active
B Paused
A Paused
Stack

Output A B D

Lecture 10 Solving Problems by Searching 7


Depth-First Search

H Active
D Paused
B Paused
A Paused
Stack

Output A B D H

Lecture 10 Solving Problems by Searching 8


Depth-First Search

D Active
B Paused
A Paused
Stack

Output A B D H

Lecture 10 Solving Problems by Searching 9


Depth-First Search

I Active
D Paused
B Paused
A Paused
Stack

Output A B D H I

Lecture 10 Solving Problems by Searching 10


Depth-First Search

D Active
B Paused
A Paused
Stack

Output A B D H I

Lecture 10 Solving Problems by Searching 11


Depth-First Search

B Active
A Paused
Stack

Output A B D H I

Lecture 10 Solving Problems by Searching 12


Depth-First Search

B Active
A Paused
Stack

Output A B D H I

Lecture 10 Solving Problems by Searching 13


Depth-First Search

E Active
B Paused
A Paused
Stack

Output A B D H I E

Lecture 10 Solving Problems by Searching 14


Depth-First Search

J Active
E Paused
B Paused
A Paused
Stack

Output A B D H I E J

Lecture 10 Solving Problems by Searching 15


Depth-First Search

E Active
B Paused
A Paused
Stack

Output A B D H I E J

Lecture 10 Solving Problems by Searching 16


Depth-First Search
•Complete?

•Time Complexity?
• if the maximum path length is m and
the maximum branching factor is b
• O(bm+1)

Lecture 10 Solving Problems by Searching 17


Depth First Search
• Space
• O(bm)

• Optimal
• No

Lecture 10 Solving Problems by Searching 18


Using Depth-First Search
• When is DFS appropriate?
• space is restricted
• solutions tend to occur at the same depth in the tree
• When is DFS inappropriate?
• some paths have infinite length
• the graph contains cycles
• some solutions are very deep, while others are very
shallow

Lecture 10 Solving Problems by Searching 19

You might also like