breadth-first search; search with costskevinlb/teaching/cs322... · recap breadth-first search...

22
Recap Breadth-First Search Breadth-first Search; Search with Costs CPSC 322 – Search 3 Textbook §3.5 Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 1

Upload: others

Post on 04-Jun-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Breadth-first Search; Search with Costs

CPSC 322 – Search 3

Textbook §3.5

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 1

Page 2: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Lecture Overview

1 Recap

2 Breadth-First Search

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 2

Page 3: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Graph Search Algorithm

Input: a graph,a set of start nodes,Boolean procedure goal(n) that tests if n is a goal node.

frontier := {〈s〉 : s is a start node};while frontier is not empty:

select and remove path 〈n0, . . . , nk〉 from frontier;if goal(nk)

return 〈n0, . . . , nk〉;for every neighbor n of nk

add 〈n0, . . . , nk, n〉 to frontier;end while

After the algorithm returns, it can be asked for more answers and theprocedure continues.

Which value is selected from the frontier defines the search strategy.

The neighbor relationship defines the graph.

The goal function defines what is a solution.

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 3

Page 4: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Depth-first Search

Depth-first search treats the frontier as a stack

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

Complete when the graph has no cycles and is finite

Time complexity is O(bm)Space complexity is O(bm)

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 4

Page 5: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

DFS Example

http://aispace.org/search/

“simple tree graph”

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 5

Page 6: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Using Depth-First Search

When is DFS appropriate?

space is restrictedsolutions tend to occur at the same depth in the treeyou know how to order nodes in the list of neighbours so thatsolutions will be found relatively quickly

When is DFS inappropriate?

some paths have infinite lengththe graph contains cyclessome solutions are very deep, while others are very shallow

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 6

Page 7: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Using Depth-First Search

When is DFS appropriate?

space is restrictedsolutions tend to occur at the same depth in the treeyou know how to order nodes in the list of neighbours so thatsolutions will be found relatively quickly

When is DFS inappropriate?

some paths have infinite lengththe graph contains cyclessome solutions are very deep, while others are very shallow

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 6

Page 8: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Using Depth-First Search

When is DFS appropriate?

space is restrictedsolutions tend to occur at the same depth in the treeyou know how to order nodes in the list of neighbours so thatsolutions will be found relatively quickly

When is DFS inappropriate?

some paths have infinite lengththe graph contains cyclessome solutions are very deep, while others are very shallow

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 6

Page 9: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Using Depth-First Search

When is DFS appropriate?

space is restrictedsolutions tend to occur at the same depth in the treeyou know how to order nodes in the list of neighbours so thatsolutions will be found relatively quickly

When is DFS inappropriate?

some paths have infinite lengththe graph contains cyclessome solutions are very deep, while others are very shallow

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 6

Page 10: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Lecture Overview

1 Recap

2 Breadth-First Search

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 7

Page 11: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Breadth-first Search

Breadth-first search treats the frontier as a queue

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

Example:

the frontier is [p1, p2, . . . , pr]neighbours of p1 are {n1, . . . , nk}

What happens?

p1 is selected, and tested for being a goal.Neighbours of p1 follow pr at the end of the frontier.Thus, the frontier is now [p2, . . . , pr, (p1, n1), . . . , (p1, nk)].p2 is selected next.

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 8

Page 12: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

BFS Example

http://aispace.org/search/

“simple tree graph”

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 9

Page 13: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Analysis of Breadth-First Search

Is BFS complete?

Yes (but it wouldn’t be if the branching factor for any nodewas infinite)In fact, BFS is guaranteed to find the path that involves thefewest arcs (why?)

What is the time complexity, if the maximum path length ism and the maximum branching factor is b?

The time complexity is O(bm): must examine every node inthe tree.The order in which we examine nodes (BFS or DFS) makes nodifference to the worst case: search is unconstrained by thegoal.

What is the space complexity?

Space complexity is O(bm): we must store the whole frontierin memory

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 10

Page 14: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Analysis of Breadth-First Search

Is BFS complete?

Yes (but it wouldn’t be if the branching factor for any nodewas infinite)In fact, BFS is guaranteed to find the path that involves thefewest arcs (why?)

What is the time complexity, if the maximum path length ism and the maximum branching factor is b?

The time complexity is O(bm): must examine every node inthe tree.The order in which we examine nodes (BFS or DFS) makes nodifference to the worst case: search is unconstrained by thegoal.

What is the space complexity?

Space complexity is O(bm): we must store the whole frontierin memory

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 10

Page 15: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Analysis of Breadth-First Search

Is BFS complete?

Yes (but it wouldn’t be if the branching factor for any nodewas infinite)In fact, BFS is guaranteed to find the path that involves thefewest arcs (why?)

What is the time complexity, if the maximum path length ism and the maximum branching factor is b?

The time complexity is O(bm): must examine every node inthe tree.The order in which we examine nodes (BFS or DFS) makes nodifference to the worst case: search is unconstrained by thegoal.

What is the space complexity?

Space complexity is O(bm): we must store the whole frontierin memory

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 10

Page 16: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Analysis of Breadth-First Search

Is BFS complete?

Yes (but it wouldn’t be if the branching factor for any nodewas infinite)In fact, BFS is guaranteed to find the path that involves thefewest arcs (why?)

What is the time complexity, if the maximum path length ism and the maximum branching factor is b?

The time complexity is O(bm): must examine every node inthe tree.The order in which we examine nodes (BFS or DFS) makes nodifference to the worst case: search is unconstrained by thegoal.

What is the space complexity?

Space complexity is O(bm): we must store the whole frontierin memory

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 10

Page 17: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Analysis of Breadth-First Search

Is BFS complete?

Yes (but it wouldn’t be if the branching factor for any nodewas infinite)In fact, BFS is guaranteed to find the path that involves thefewest arcs (why?)

What is the time complexity, if the maximum path length ism and the maximum branching factor is b?

The time complexity is O(bm): must examine every node inthe tree.The order in which we examine nodes (BFS or DFS) makes nodifference to the worst case: search is unconstrained by thegoal.

What is the space complexity?

Space complexity is O(bm): we must store the whole frontierin memory

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 10

Page 18: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Analysis of Breadth-First Search

Is BFS complete?

Yes (but it wouldn’t be if the branching factor for any nodewas infinite)In fact, BFS is guaranteed to find the path that involves thefewest arcs (why?)

What is the time complexity, if the maximum path length ism and the maximum branching factor is b?

The time complexity is O(bm): must examine every node inthe tree.The order in which we examine nodes (BFS or DFS) makes nodifference to the worst case: search is unconstrained by thegoal.

What is the space complexity?

Space complexity is O(bm): we must store the whole frontierin memory

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 10

Page 19: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Using Breadth-First Search

When is BFS appropriate?

space is not a problemit’s necessary to find the solution with the fewest arcsalthough all solutions may not be shallow, at least some arethere may be infinite paths

When is BFS inappropriate?

space is limitedall solutions tend to be located deep in the treethe branching factor is very large

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 11

Page 20: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Using Breadth-First Search

When is BFS appropriate?

space is not a problemit’s necessary to find the solution with the fewest arcsalthough all solutions may not be shallow, at least some arethere may be infinite paths

When is BFS inappropriate?

space is limitedall solutions tend to be located deep in the treethe branching factor is very large

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 11

Page 21: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Using Breadth-First Search

When is BFS appropriate?

space is not a problemit’s necessary to find the solution with the fewest arcsalthough all solutions may not be shallow, at least some arethere may be infinite paths

When is BFS inappropriate?

space is limitedall solutions tend to be located deep in the treethe branching factor is very large

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 11

Page 22: Breadth-first Search; Search with Costskevinlb/teaching/cs322... · Recap Breadth-First Search Depth- rst Search Depth- rst searchtreats the frontier as a stack It always selects

Recap Breadth-First Search

Using Breadth-First Search

When is BFS appropriate?

space is not a problemit’s necessary to find the solution with the fewest arcsalthough all solutions may not be shallow, at least some arethere may be infinite paths

When is BFS inappropriate?

space is limitedall solutions tend to be located deep in the treethe branching factor is very large

Breadth-first Search; Search with Costs CPSC 322 – Search 3, Slide 11