66
Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley 2. RN, AIMA

Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first searchUniform cost search

Robert PlattNortheastern University

Some images and slides are used from:1. CS188 UC Berkeley2. RN, AIMA

Page 2: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

What is graph search?

Start state

Goal state

Page 3: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

What is a graph?

Graph:

Edges:

Vertices:

Directed graph

Page 4: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

What is a graph?

Graph:

Edges:

Vertices:

Undirected graph

Page 5: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Graph search

Given: a graph, G

Problem: find a path from A to B

– A: start state

– B: goal state

Page 6: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Graph search

Given: a graph, G

Problem: find a path from A to B

– A: start state

– B: goal state

How?

Page 7: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

A search tree

Start at A

Page 8: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

A search tree

Successors of A

Page 9: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

A search tree

Successors of A

parent children

Page 10: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

A search tree

Let's expand S next

Page 11: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

A search tree

Successors of S

Page 12: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

A search tree

A was already visited!

Page 13: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

A search tree

A was already visited!So, prune it!

Page 14: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

A search tree

In what order should we expand states?

– here, we expanded S, but we could also have expanded Z or T

– different search algorithms expand in different orders

Page 15: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first search (BFS)

Slide: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

Page 16: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first search (BFS)

Page 17: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first search (BFS)

Start node

Page 18: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first search (BFS)

Page 19: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first search (BFS)

Page 20: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first search (BFS)

Page 21: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first search (BFS)

We're going to maintain a queue called the fringe

– initialize the fringe as an empty queue

Fringe

Page 22: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first search (BFS)

– add A to the fringe

fringeFringeA

Page 23: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first search (BFS)

-- remove A from the fringe

-- add successors of A to the fringe

fringe

FringeBC

Page 24: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first search (BFS)

-- remove B from the fringe

-- add successors of B to the fringe

fringe

FringeCDE

Page 25: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first search (BFS)

fringe

FringeDEFG

-- remove C from the fringe

-- add successors of C to the fringe

Page 26: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first search (BFS)

fringe

FringeDEFG

Which state gets removed next from the fringe?

Page 27: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first search (BFS)

fringe

FringeDEFG

Which state gets removed next from the fringe?

What kind of a queue is this?

Page 28: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first search (BFS)

fringe

FringeDEFG

Which state gets removed next from the fringe?

What kind of a queue is this?

FIFO Queue!(first in first out)

Page 29: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first search (BFS)

Page 30: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Breadth first search (BFS)

What is the purpose of the explored set?

Page 31: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

BFS Properties

Is BFS complete?– is it guaranteed to find a solution if one exists?

Page 32: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

BFS Properties

Is BFS complete?– is it guaranteed to find a solution if one exists?

What is the time complexity of BFS?– how many states are expanded before finding a sol'n?

– b: branching factor– d: depth of shallowest solution– complexity = ???

Page 33: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

BFS Properties

Is BFS complete?– is it guaranteed to find a solution if one exists?

What is the time complexity of BFS?– how many states are expanded before finding a sol'n?

– b: branching factor– d: depth of shallowest solution– complexity =

Page 34: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

BFS Properties

Is BFS complete?– is it guaranteed to find a solution if one exists?

What is the time complexity of BFS?– how many states are expanded before finding a sol'n?

– b: branching factor– d: depth of shallowest solution– complexity =

What is the space complexity of BFS?– how much memory is required?

– complexity = ???

Page 35: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

BFS Properties

Is BFS complete?– is it guaranteed to find a solution if one exists?

What is the time complexity of BFS?– how many states are expanded before finding a sol'n?

– b: branching factor– d: depth of shallowest solution– complexity =

What is the space complexity of BFS?– how much memory is required?

– complexity =

Page 36: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

BFS Properties

Is BFS complete?– is it guaranteed to find a solution if one exists?

What is the time complexity of BFS?– how many states are expanded before finding a sol'n?

– b: branching factor– d: depth of shallowest solution– complexity =

What is the space complexity of BFS?– how much memory is required?

– complexity =

Is BFS optimal?– is it guaranteed to find the best solution (shortest path)?

Page 37: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Another BFS example...

Page 38: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Uniform Cost Search (UCS)

Slide: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

Page 39: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Uniform Cost Search (UCS)

Notice the distances between cities

Page 40: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Uniform Cost Search (UCS)

Notice the distances between cities– does BFS take these distances into account?

Page 41: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Uniform Cost Search (UCS)

Notice the distances between cities– does BFS take these distances into account?– does BFS find the path w/ shortest milage?

Page 42: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Uniform Cost Search (UCS)

Notice the distances between cities– does BFS take these distances into account?– does BFS find the path w/ shortest milage?– compare S-F-B with S-R-P-B. Which costs less?

Page 43: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Uniform Cost Search (UCS)

Notice the distances between cities– does BFS take these distances into account?– does BFS find the path w/ shortest milage?– compare S-F-B with S-R-P-B. Which costs less?

How do we fix this?

Page 44: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Uniform Cost Search (UCS)

Notice the distances between cities– does BFS take these distances into account?– does BFS find the path w/ shortest milage?– compare S-F-B with S-R-P-B. Which costs less?

How do we fix this?UCS!

Page 45: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Uniform Cost Search (UCS)

Same as BFS except: expand node w/ smallest path cost

Length of path

Page 46: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Uniform Cost Search (UCS)

Same as BFS except: expand node w/ smallest path cost

Length of path

Cost of going from state A to B:

Minimum cost of path going from start state to B:

Page 47: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Uniform Cost Search (UCS)

Same as BFS except: expand node w/ smallest path cost

Length of path

Cost of going from state A to B:

Minimum cost of path going from start state to B:

BFS: expands states in order of hops from start

UCS: expands states in order of

Page 48: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Uniform Cost Search (UCS)

Same as BFS except: expand node w/ smallest path cost

Length of path

Cost of going from state A to B:

Minimum cost of path going from start state to B:

BFS: expands states in order of hops from start

UCS: expands states in order of How?

Page 49: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Uniform Cost Search (UCS)

Simple answer: change the FIFO to a priority queue– the priority of each element in the queue is its path cost.

Page 50: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

Uniform Cost Search (UCS)

Page 51: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

UCS

FringeA

Path Cost0

Explored set:

Page 52: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

UCS

140 11875

Explored set: A

FringeASTZ

Path Cost014011875

Page 53: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

UCS

140 11875

146

Explored set: A, Z

FringeASTZT

Path Cost014011875146

Page 54: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

UCS

140 11875

146229

Explored set: A, Z, T

FringeASTZTL

Path Cost014011875146229

Page 55: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

UCS

140 11875

239 220 146229

Explored set: A, Z, T, S

FringeASTZTLFR

Path Cost014011875146229239220

Page 56: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

UCS

140 11875

239 220 146229

Explored set: A, Z, T, S

FringeASTZTLFR

Path Cost014011875146229239220

Page 57: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

UCS

140 11875

239 220

336 317

146229

Explored set: A, Z, T, S, R

FringeASTZTLFRCP

Path Cost014011875146229239220336317

Page 58: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

UCS

140 11875

239 220

336 317

146229

299

Explored set: A, Z, T, S, R, L

FringeASTZTLFRCPM

Path Cost014011875146229239220336317299

Page 59: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

UCS

140 11875

239 220

336 317

146229

299

Explored set: A, Z, T, S, R, L

FringeASTZTLFRCPM

Path Cost014011875146229239220336317299

When does this end?

Page 60: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

UCS

140 11875

239 220

336 317

146229

299

Explored set: A, Z, T, S, R, L

FringeASTZTLFRCPM

Path Cost014011875146229239220336317299

When does this end?– when the goal state is removed from the queue

Page 61: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

UCS

140 11875

239 220

336 317

146229

299

Explored set: A, Z, T, S, R, L

FringeASTZTLFRCPM

Path Cost014011875146229239220336317299

When does this end?– when the goal state is removed from the queue– NOT when the goal state is expanded

Page 62: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

UCS

Page 63: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

UCS Properties

Is UCS complete?– is it guaranteed to find a solution if one exists?

What is the time complexity of UCS?– how many states are expanded before finding a sol'n?

– b: branching factor– C*: cost of optimal sol'n– e: min one-step cost– complexity =

What is the space complexity of BFS?– how much memory is required?

– complexity =

Is BFS optimal?– is it guaranteed to find the best solution (shortest path)?

Page 64: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

S

a

b

d p

a

c

e

p

h

f

r

q

q c G

a

qe

p

h

f

r

q

q c G

a

Strategy: expand a cheapest node first:

Fringe is a priority queue (priority: cumulative cost) S

G

d

b

p q

c

e

h

a

f

r

3 9 1

16411

5

713

8

1011

17 11

0

6

39

1

1

2

8

8 2

15

1

2

Cost contours

2

UCS vs BFS

Slide: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

Page 65: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

UCS vs BFS

S

a

b

d p

a

c

e

p

h

f

r

q

q c G

a

qe

p

h

f

r

q

q c G

a

S

G

d

b

p q

c

e

h

a

f

r

Search

Tiers

Strategy: expand a shallowest node first

Implementation: Fringe is a FIFO queue

Slide: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)

Page 66: Breadth first search Uniform cost search · Breadth first search Uniform cost search Robert Platt Northeastern University Some images and slides are used from: 1. CS188 UC Berkeley

UCS vs BFS

Start Goal

c 3

c 2c 1 Remember: UCS explores

increasing cost contours

The good: UCS is complete and optimal!

The bad: Explores options in every

“direction” No information about goal

location

We’ll fix that soon!

Slide: Adapted from Berkeley CS188 course notes (downloaded Summer 2015)