artificial intelligence, lecture 3.2, page 1 · illustrative graph | depth- rst search 1 2 3 4 5 6...

23
Learning Objectives At the end of the class you should be able to: demonstrate how depth-first search will work on a graph demonstrate how breadth-first search will work on a graph predict the space and time requirements for depth-first and breadth-first searches c D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 1 1 / 11

Upload: others

Post on 06-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Learning Objectives

At the end of the class you should be able to:

demonstrate how depth-first search will work on a graph

demonstrate how breadth-first search will work on a graph

predict the space and time requirements for depth-first andbreadth-first searches

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 1 1 / 11

Page 2: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Depth-first Search

Depth-first search treats the frontier as a stack

It always selects one of the last elements added to the frontier.

If the list of paths on the frontier is [p1, p2, . . .]I p1 is selected. Paths that extend p1 are added to the front of

the stack (in front of p2).I p2 is only selected when all paths from p1 have been explored.

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 2 2 / 11

Page 3: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Illustrative Graph — Depth-first Search

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15 16

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 3 3 / 11

Page 4: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Which shaded goal will depth-first search find first?

Q W

T

U

Y

R

V

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 4 4 / 11

Page 5: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Complexity of Depth-first Search

Does depth-first search guarantee to find the path with fewestarcs?

What happens on infinite graphs or on graphs with cycles ifthere is a solution?

What is the time complexity as a function of length of thepath selected?

What is the space complexity as a function of length of thepath selected?

How does the goal affect the search?

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 5 5 / 11

Page 6: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Complexity of Depth-first Search

Does depth-first search guarantee to find the path with fewestarcs?

What happens on infinite graphs or on graphs with cycles ifthere is a solution?

What is the time complexity as a function of length of thepath selected?

What is the space complexity as a function of length of thepath selected?

How does the goal affect the search?

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 6 5 / 11

Page 7: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Complexity of Depth-first Search

Does depth-first search guarantee to find the path with fewestarcs?

What happens on infinite graphs or on graphs with cycles ifthere is a solution?

What is the time complexity as a function of length of thepath selected?

What is the space complexity as a function of length of thepath selected?

How does the goal affect the search?

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 7 5 / 11

Page 8: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Complexity of Depth-first Search

Does depth-first search guarantee to find the path with fewestarcs?

What happens on infinite graphs or on graphs with cycles ifthere is a solution?

What is the time complexity as a function of length of thepath selected?

What is the space complexity as a function of length of thepath selected?

How does the goal affect the search?

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 8 5 / 11

Page 9: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Complexity of Depth-first Search

Does depth-first search guarantee to find the path with fewestarcs?

What happens on infinite graphs or on graphs with cycles ifthere is a solution?

What is the time complexity as a function of length of thepath selected?

What is the space complexity as a function of length of thepath selected?

How does the goal affect the search?

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 9 5 / 11

Page 10: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Breadth-first Search

Breadth-first search treats the frontier as a queue.

It always selects one of the earliest elements added to thefrontier.

If the list of paths on the frontier is [p1, p2, . . . , pr ]:I p1 is selected. Its neighbors are added to the end of the queue,

after pr .I p2 is selected next.

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 10 6 / 11

Page 11: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Illustrative Graph — Breadth-first Search

1

2 3

4 5 6 7

8 9 10 11 12 13 14

15 16

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 11 7 / 11

Page 12: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Which shaded goal will breadth-first search find first?

C A

D B

E

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 12 8 / 11

Page 13: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Complexity of Breadth-first Search

Does breadth-first search guarantee to find the path withfewest arcs?

What happens on infinite graphs or on graphs with cycles ifthere is a solution?

What is the time complexity as a function of the length of thepath selected?

What is the space complexity as a function of the length ofthe path selected?

How does the goal affect the search?

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 13 9 / 11

Page 14: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Complexity of Breadth-first Search

Does breadth-first search guarantee to find the path withfewest arcs?

What happens on infinite graphs or on graphs with cycles ifthere is a solution?

What is the time complexity as a function of the length of thepath selected?

What is the space complexity as a function of the length ofthe path selected?

How does the goal affect the search?

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 14 9 / 11

Page 15: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Complexity of Breadth-first Search

Does breadth-first search guarantee to find the path withfewest arcs?

What happens on infinite graphs or on graphs with cycles ifthere is a solution?

What is the time complexity as a function of the length of thepath selected?

What is the space complexity as a function of the length ofthe path selected?

How does the goal affect the search?

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 15 9 / 11

Page 16: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Complexity of Breadth-first Search

Does breadth-first search guarantee to find the path withfewest arcs?

What happens on infinite graphs or on graphs with cycles ifthere is a solution?

What is the time complexity as a function of the length of thepath selected?

What is the space complexity as a function of the length ofthe path selected?

How does the goal affect the search?

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 16 9 / 11

Page 17: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Complexity of Breadth-first Search

Does breadth-first search guarantee to find the path withfewest arcs?

What happens on infinite graphs or on graphs with cycles ifthere is a solution?

What is the time complexity as a function of the length of thepath selected?

What is the space complexity as a function of the length ofthe path selected?

How does the goal affect the search?

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 17 9 / 11

Page 18: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Lowest-cost-first Search

Sometimes there are costs associated with arcs. The cost of apath is the sum of the costs of its arcs.

cost(〈n0, . . . , nk〉) =k∑

i=1

cost(〈ni−1, ni 〉)

An optimal solution is one with minimum cost.

At each stage, lowest-cost-first search selects a path on thefrontier with lowest cost.

The frontier is a priority queue ordered by path cost.

The first path to a goal is a least-cost path to a goal node.

When arc costs are equal =⇒breadth-first search.

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 18 10 / 11

Page 19: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Lowest-cost-first Search

Sometimes there are costs associated with arcs. The cost of apath is the sum of the costs of its arcs.

cost(〈n0, . . . , nk〉) =k∑

i=1

cost(〈ni−1, ni 〉)

An optimal solution is one with minimum cost.

At each stage, lowest-cost-first search selects a path on thefrontier with lowest cost.

The frontier is a priority queue ordered by path cost.

The first path to a goal is

a least-cost path to a goal node.

When arc costs are equal =⇒breadth-first search.

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 19 10 / 11

Page 20: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Lowest-cost-first Search

Sometimes there are costs associated with arcs. The cost of apath is the sum of the costs of its arcs.

cost(〈n0, . . . , nk〉) =k∑

i=1

cost(〈ni−1, ni 〉)

An optimal solution is one with minimum cost.

At each stage, lowest-cost-first search selects a path on thefrontier with lowest cost.

The frontier is a priority queue ordered by path cost.

The first path to a goal is a least-cost path to a goal node.

When arc costs are equal =⇒

breadth-first search.

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 20 10 / 11

Page 21: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Lowest-cost-first Search

Sometimes there are costs associated with arcs. The cost of apath is the sum of the costs of its arcs.

cost(〈n0, . . . , nk〉) =k∑

i=1

cost(〈ni−1, ni 〉)

An optimal solution is one with minimum cost.

At each stage, lowest-cost-first search selects a path on thefrontier with lowest cost.

The frontier is a priority queue ordered by path cost.

The first path to a goal is a least-cost path to a goal node.

When arc costs are equal =⇒breadth-first search.

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 21 10 / 11

Page 22: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Summary of Search Strategies

Strategy Frontier Selection Complete Halts Space

Depth-first Last node added

No No Linear

Breadth-first First node added

Yes No Exp

Lowest-cost-first Minimal cost(p)

Yes No Exp

Complete — guaranteed to find a solution if there is one (forgraphs with finite number of neighbours, even on infinite graphs)Halts — on finite graph (perhaps with cycles).Space — as a function of the length of current path

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 22 11 / 11

Page 23: Artificial Intelligence, Lecture 3.2, Page 1 · Illustrative Graph | Depth- rst Search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 c D. Poole and A. Mackworth 2017 Arti cial Intelligence,

Summary of Search Strategies

Strategy Frontier Selection Complete Halts Space

Depth-first Last node added No No LinearBreadth-first First node added Yes No ExpLowest-cost-first Minimal cost(p) Yes No Exp

Complete — guaranteed to find a solution if there is one (forgraphs with finite number of neighbours, even on infinite graphs)Halts — on finite graph (perhaps with cycles).Space — as a function of the length of current path

c©D. Poole and A. Mackworth 2017 Artificial Intelligence, Lecture 3.2, Page 23 11 / 11