17graphtraversals bfs dfs - computer science | bryn · pdf file ·...

101
Graph Traversals: BreadthFirst and DepthFirst Search Eric Eaton Bryn Mawr College Computer Science Department

Upload: dangnhu

Post on 17-Mar-2018

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Graph  Traversals:  Breadth-­‐First  and  Depth-­‐First  Search  

Eric Eaton

Bryn Mawr College Computer Science Department

Page 2: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

What  is  a  Graph?  

Graphs are collections of vertices joined by edges “Graph”  ≡  “Network”  

Vertices Edges vertices edges, arcs math nodes links, relations computer science sites bonds physics actors ties, relations sociology

vertex

edge

2

Page 3: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

3

Example  Networks  

Terrorist Network (from http://www.orgnet.com/tnet.html)

Co-Authorship Network of UIST Conference (by Lothar Krempel, http://www.mpi-fg-koeln.mpg.de/~lk/netvis/)

Airline Network (Source: Northwest Airlines)

Terrorist Network (by Valdis Krebs, Orgnet.com)

School Friendship Network (from Moody 2001)

Protein-Protein Interactions (by Peter Uetz)

Page 4: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Other  Applica8ons  

•  Intersections and streets within a city •  Computer networks •  Electronic circuits •  Food webs •  Gene regulatory networks •  Steps to solve a puzzle •  many more...

4

Page 5: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Outline  

•  Introduc8on  •  Graph  Basics  •  Graph  Search  Problem  

– Breadth-­‐First  Search  – Depth-­‐First  Search  

•  Complexity  Analysis  

5

Page 6: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Outline  

•  IntroducEon  •  Graph  Basics  •  Graph  Search  Problem  

– Breadth-­‐First  Search  – Depth-­‐First  Search  

•  Complexity  Analysis  

6

Page 7: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Basic  Graph  Defini8ons  

§  A graph G = (V,E) consists of a finite set of vertices V and a finite set of edges E

§  Each edge is a pair (u,v) where u,v ∈ V §  V and E are sets, so each vertex u ∈ V is unique,

and each edge e ∈ E is unique §  v is adjacent to u

§  We will focus on two types: §  Undirected graphs §  Directed graphs

7

u

v (u,v)

Page 8: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Undirected  Graph  

8

2

1

3 4

5

All  edges  are  two-­‐way  

n  V  =  {  1,  2,  3,  4,  5  }  

n   Edges  are  unordered  pairs:   E  =  {  {1,2},  {2,3},  {3,4},  {2,4},  {4,5},  {5,1}  }  

Page 9: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Directed  Graph  

9

2

1

3 4

5

All  edges  are  “one-­‐way”  as  indicated  by  the  arrows  

n  V  =  {  1,  2,  3,  4,  5  }  

n   Edges  are  ordered  pairs:   E  =  {  (1,2),  (2,4),  (3,2),  (4,3),  (4,5),  (5,1),  (5,4)  }  

Page 10: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Degree  

Undirected  Graphs  degree(u):    the  number  of  edges  {u,v}  for  all  v ∈ V

10

2

1

3 4

5

Page 11: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Degree  

Undirected  Graphs  degree(u):    the  number  of  edges  {u,v}  for  all  v ∈ V

Directed  Graphs  in-­‐degree(u):    the  number  of  edges  (v,u)  for  all  v ∈ V

out-­‐degree(u):    the  number  of  edges  (u,v)  for  all  v ∈ V

 

11

2

1

3 4

5 2

1

3 4

5

Page 12: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

12

Paths  in  Graphs  §  A  path  in  a  graph  is  a  sequence  of  verEces    

s.t.                                                              for  1  ≤  i  <  n  

§  The  path’s  length  is  the  number  of  edges  on  the  path  –  The  length  of  the  path  from  a  vertex  to  itself  is  0  

§  In  a  simple  path,  all  verEces  are  disEnct  –  The  first  and  last  verEces  may  be  the  same  

w1, w2, . . . , wn (wi, wi+1) 2 E

2

1

3 4

5 path length = 4

Page 13: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Paths  in  Graphs  

§  How  many  simple  paths  are  there  from  1  to  4  and  what  are  their  lengths?  

13

2

1

3 4

5 2

1

3 4

5

Page 14: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Outline  

•  IntroducEon  •  Graph  Basics  •  Graph  Search  Problem  

– Breadth-­‐First  Search  – Depth-­‐First  Search  

•  Complexity  Analysis  

14

Page 15: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Outline  

•  IntroducEon  •  Graph  Basics  •  Graph  Search  Problem  

– Breadth-­‐First  Search  – Depth-­‐First  Search  

•  Complexity  Analysis  

15

Page 16: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Graph  Search  Problem  

§  Goal:    Find  a  simple  path  from  a  starEng  vertex  to  a  goal  vertex  

§ What  applicaEons  can  be  framed  as  instances  of  this  problem?  

17

v1 v7 v2

v8 v4 v6 v3

v9 v10 v5

start

goal

Page 17: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Intui8on  §  From  starEng  vertex,  keep  expanding  verEces    unEl  we  find  the  goal          

   

18

Page 18: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Intui8on  §  From  starEng  vertex,  keep  expanding  verEces    unEl  we  find  the  goal          

   

19

v1 v7 v2

v8 v4 v6 v3

v9 v10 v5

start

goal

Page 19: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Intui8on  §  From  starEng  vertex,  keep  expanding  verEces    unEl  we  find  the  goal          

   

20

v1 v7 v2

v8 v4 v6 v3

v9 v10 v5

start

goal

Page 20: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Intui8on  §  From  starEng  vertex,  keep  expanding  verEces    unEl  we  find  the  goal          

   

21

v1 v7 v2

v8 v4 v6 v3

v9 v10 v5

start

goal

Page 21: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Intui8on  §  From  starEng  vertex,  keep  expanding  verEces    unEl  we  find  the  goal          

   

§  Breadth-­‐First:    expand  shallowest  unexpanded  vertex  §  Depth-­‐First:    expand  deepest  unexpanded  vertex  

22

v1 v7 v2

v8 v4 v6 v3

v9 v10 v5

start

goal

Page 22: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Queuing  Func8on  §  Used  to  maintain  a  ranked  list  of  nodes  that  are  candidates  for  expansion  §  Called  the  “fringe”  

§  SubsEtuEng  different  queuing  funcEons  yields  different  searches  

23

v1 v7 v2

v8 v4 v6 v3

v9 v10 v5

start

goal

Page 23: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Protec8on  Against  Cycles  §  We  need  to  guard  against  cycles  

§  Mark  each  vertex  as  “closed”  when  we  encounter  it  §  Do  not  consider  closed  verEces  again  

25

v1 v7 v2

v8 v4 v6 v3

v9 v10 v5

start

goal

Page 24: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Bookkeeping  Structures  

§  Node:  –  vertex  ID  –  predecessor  node  –  path  length    

§  Problem:  –  graph  –  starEng  vertex  –  goalTest(Vertex  v)  –  tests  if  vertex  is  a  goal  state  

26

Page 25: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

General  Graph  Search  // problem describes the graph, start vertex, and goal test // queueingfn is a comparator function that ranks two states // graphSearch returns either a goal node or failure

graphSearch(problem, queuingFn) { open = {}, closed = {} //empty lists

queuingFn(open, new Node(problem.startvertex)) //init

loop { if empty(open) then return FAILURE //no nodes remain

c = removeFront(open) //get current node

if problem.goalTest(c.vertex) //goal test return c

if c.vertex is not in closed { //avoid duplicates add c.vertex to closed for each Vertex w adjacent to c.vertex //expand node

if w is not in closed queuingFn(open, new Node(w,c)); }

} } 27

Page 26: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  

28

A G

H D F C

I J E

B start goal

Page 27: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Breadth-­‐First  Search  

Expands  the  “shallowest”  vertex  

Page 28: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

30

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

A G

H D F C

I J E

B start goal open  list   closed  list  

Page 29: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

31

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

A G

H D F C

I J E

B start goal open  list  (A,0,null)  

closed  list  

Page 30: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

32

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

A G

H D F C

I J E

B start goal open  list  (A,0,null)  

closed  list  

Page 31: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

33

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (A,0,null)  

closed  list  A G

H D F C

I J E

B start goal

Page 32: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

34

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list   closed  list  

node  c  (A,0,null)  

   

A G

H D F C

I J E

B start goal

Page 33: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

35

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list   closed  list  

node  c  (A,0,null)  

   

A G

H D F C

I J E

B start goal

Page 34: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

36

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list   closed  list  

node  c  (A,0,null)  

   

A G

H D F C

I J E

B start goal

Page 35: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

37

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list   closed  list  A  

node  c  (A,0,null)  

   

A G

H D F C

I J E

B start goal

Page 36: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

38

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list   closed  list  A  

node  c  (A,0,null)  

   

A G

H D F C

I J E

B start goal

Page 37: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

39

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (B,1,A)  (C,1,A)  (D,1,A)  

 

closed  list  A  

node  c  (A,0,null)  

   

A G

H D F C

I J E

B start goal

Page 38: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

40

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (B,1,A)  (C,1,A)  (D,1,A)  

 

closed  list  A   A G

H D F C

I J E

B start goal

Page 39: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

41

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (C,1,A)  (D,1,A)  

 

closed  list  A  

node  c  (B,1,A)  

   

A G

H D F C

I J E

B start goal

Page 40: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

42

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (C,1,A)  (D,1,A)  

 

closed  list  A  

node  c  (B,1,A)  

   

A G

H D F C

I J E

B start goal

Page 41: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

43

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (C,1,A)  (D,1,A)  

 

closed  list  A  

node  c  (B,1,A)  

   

A G

H D F C

I J E

B start goal

Page 42: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

44

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (C,1,A)  (D,1,A)  

 

closed  list  A  B  

node  c  (B,1,A)  

   

A G

H D F C

I J E

B start goal

Page 43: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

45

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (C,1,A)  (D,1,A)  

 

closed  list  A  B  

node  c  (B,1,A)  

   

A G

H D F C

I J E

B start goal

Page 44: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

46

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (C,1,A)  (D,1,A)  

 

closed  list  A  B  

node  c  (B,1,A)  

   

A G

H D F C

I J E

B start goal

Need  to  add  (C,2,B)  and  (F,2,B)  to  open    

Since  BFS  expands  the  shallowest  node,  what  must  we  insure  about  the  open  list?  

 What  queuing  funcEon  should  we  use?  

Page 45: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

47

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (C,1,A)  (D,1,A)  (C,2,B)  (F,2,B)  

 

closed  list  A  B  

node  c  (B,1,A)  

   

A G

H D F C

I J E

B start goal

BFS  uses  a    FIFO  Queue!  

Page 46: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

48

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (D,1,A)  (C,2,B)  (F,2,B)  

 

closed  list  A  B  

node  c  (C,1,A)  

   

A G

H D F C

I J E

B start goal

Page 47: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

49

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (D,1,A)  (C,2,B)  (F,2,B)  

 

closed  list  A  B  C  

node  c  (C,1,A)  

   

A G

H D F C

I J E

B start goal

Page 48: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

50

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (D,1,A)  (C,2,B)  (F,2,B)  (F,2,C)  (E,2,C)  

 

closed  list  A  B  C  

node  c  (C,1,A)  

   

A G

H D F C

I J E

B start goal

Page 49: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

51

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (C,2,B)  (F,2,B)  (F,2,C)  (E,2,C)  

 

closed  list  A  B  C  

node  c  (D,1,A)  

   

A G

H D F C

I J E

B start goal

Page 50: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

52

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (C,2,B)  (F,2,B)  (F,2,C)  (E,2,C)  

 

closed  list  A  B  C  D  

node  c  (D,1,A)  

   

A G

H D F C

I J E

B start goal

Page 51: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

53

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (C,2,B)  (F,2,B)  (F,2,C)  (E,2,C)  (E,2,D)  

 

closed  list  A  B  C  D  

node  c  (D,1,A)  

   

A G

H D F C

I J E

B start goal

Page 52: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

54

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (F,2,B)  (F,2,C)  (E,2,C)  (E,2,D)  

 

closed  list  A  B  C  D  

node  c  (C,2,B)  

   

A G

H D F C

I J E

B start goal

Page 53: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

55

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (F,2,B)  (F,2,C)  (E,2,C)  (E,2,D)  

 

closed  list  A  B  C  D  

node  c  (C,2,B)  

   

A G

H D F C

I J E

B start goal

Page 54: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

56

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (F,2,C)  (E,2,C)  (E,2,D)  

 

closed  list  A  B  C  D  

node  c  (F,2,B)  

   

A G

H D F C

I J E

B start goal

Page 55: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

57

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (F,2,C)  (E,2,C)  (E,2,D)  

 

closed  list  A  B  C  D  F  

node  c  (F,2,B)  

   

A G

H D F C

I J E

B start goal

Page 56: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

58

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (F,2,C)  (E,2,C)  (E,2,D)  (G,3,F)  (H,3,F)  

 

closed  list  A  B  C  D  F  

node  c  (F,2,B)  

   

A G

H D F C

I J E

B start goal

Page 57: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

59

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (E,2,C)  (E,2,D)  (G,3,F)  (H,3,F)  

 

closed  list  A  B  C  D  F  

node  c  (F,2,C)  

   

A G

H D F C

I J E

B start goal

Page 58: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

60

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (E,2,C)  (E,2,D)  (G,3,F)  (H,3,F)  

 

closed  list  A  B  C  D  F  

node  c  (F,2,C)  

   

A G

H D F C

I J E

B start goal

Page 59: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

61

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (E,2,D)  (G,3,F)  (H,3,F)  

 

closed  list  A  B  C  D  F  

node  c  (E,2,C)  

   

A G

H D F C

I J E

B start goal

Page 60: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

62

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (E,2,D)  (G,3,F)  (H,3,F)  

 

closed  list  A  B  C  D  E  F  

node  c  (E,2,C)  

   

A G

H D F C

I J E

B start goal

Page 61: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

63

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (G,3,F)  (H,3,F)  

 

closed  list  A  B  C  D  E  F  

node  c  (E,2,D)  

   

A G

H D F C

I J E

B start goal

Page 62: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

64

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (H,3,F)  

 

closed  list  A  B  C  D  E  F  

node  c  (G,3,F)  

   

A G

H D F C

I J E

B start goal

Page 63: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (BFS)  

65

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (H,3,F)  

 

closed  list  A  B  C  D  E  F  

node  c  (G,3,F)  

   

A G

H D F C

I J E

B start goal

Page 64: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Breadth-­‐First  Search  

66

A G

H D F C

I J E

B start 0

Page 65: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Breadth-­‐First  Search  

67

A G

H D F C

I J E

B start 0 1

Page 66: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Breadth-­‐First  Search  

68

A G

H D F C

I J E

B start 0 1 2

Page 67: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Breadth-­‐First  Search  

69

A G

H D F C

I J E

B start 0 1 2 3

Page 68: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Breadth-­‐First  Search  

70

A G

H D F C

I J E

B start 0 1 2 3 4

Page 69: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Depth-­‐First  Search  

Expands  the  “deepest”  vertex  

Page 70: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (DFS)  

72

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list   closed  list  

node  c  (A,0,null)  

   

A G

H D F C

I J E

B start goal

Page 71: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (DFS)  

73

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (B,1,A)  (C,1,A)  (D,1,A)  

 

closed  list  A  

node  c  (A,0,null)  

   

A G

H D F C

I J E

B start goal

Page 72: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (DFS)  

74

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (C,1,A)  (D,1,A)  

 

closed  list  A  

node  c  (B,1,A)  

   

A G

H D F C

I J E

B start goal

Page 73: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (DFS)  

75

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (C,1,A)  (D,1,A)  

 

closed  list  A  B  

node  c  (B,1,A)  

   

A G

H D F C

I J E

B start goal

Page 74: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (DFS)  

76

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (C,1,A)  (D,1,A)  

 

closed  list  A  B  

node  c  (B,1,A)  

   

A G

H D F C

I J E

B start goal

Need  to  add  (C,2,B)  and  (F,2,B)  to  open    

Since  DFS  expands  the  deepest  node,  what  must  we  insure  about  the  open  list?  

 What  queuing  funcEon  should  we  use?  

Page 75: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (DFS)  

77

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (F,2,B)  (C,2,B)  (C,1,A)  (D,1,A)  

 

closed  list  A  B  

node  c  (B,1,A)  

   

A G

H D F C

I J E

B start goal

DFS  uses  a    LIFO  Stack!  

Page 76: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (DFS)  

78

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (C,2,B)  (C,1,A)  (D,1,A)  

 

closed  list  A  B  

node  c  (F,2,B)  

   

A G

H D F C

I J E

B start goal

Page 77: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (DFS)  

79

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (C,2,B)  (C,1,A)  (D,1,A)  

 

closed  list  A  B  F  

node  c  (F,2,B)  

   

A G

H D F C

I J E

B start goal

Page 78: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (DFS)  

80

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (H,3,F)  (G,3,F)  (C,2,B)  (C,1,A)  (D,1,A)  

 

closed  list  A  B  F  

node  c  (F,2,B)  

   

A G

H D F C

I J E

B start goal

Page 79: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    Route  Finding  (DFS)  

81

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) then return c if c.vertex is not in closed {

add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

open  list  (H,3,F)  (G,3,F)  (C,2,B)  (C,1,A)  (D,1,A)  

 

closed  list  A  B  F  

A G

H D F C

I J E

B start goal

Page 80: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

What  we’ve  found  so  far...  

Breadth-­‐First  Search    (FIFO  Queue)  §  Solves  unweighted  shortest  path  problem:      Finds  the  shortest  path  between  verEces  if  edges  are  unweighted  (or  equal  cost)    

Depth-­‐First  Search  (LIFO  Stack)  §  Finds  nearby  goals  quickly  if  lucky  §  If  unlucky,  finds  nearby  goals  very  slowly  

82

Page 81: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Applica8on:    8-­‐Puzzle  

Given  an  iniEal  configuraEon  of  8  numbered  Eles  on  a  3  x  3  board,  move  the  Eles  as  to  produce  a  desired  goal  configuraEon      

83 Slide adapted from materials by Stuart Russell

Page 82: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

•  What are the vertices? 3 x 3 array configuration of the tiles on the board.

•  What are the edges?

•  Starting vertex? •  of the board. •  Goal vertex / vertices? A particular

configuration of the board.

Applica8on:    8-­‐Puzzle  

84 Slide adapted from materials by Stuart Russell

Page 83: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

•  What  are  the  ver8ces?    Each  vertex  corresponds  to  a  parEcular  Ele  configuraEon  

•  What  are  the  edges?    Consider  four  operators:    Move  Blank  Square  Lei,  Right,  Up  or  Down    – This  is  a  more  efficient  encoding  than  considering  each  of  4  moves  for  each  Ele  

 The  edges  signify  applying  an  operator  to  a  board  configuraEon  

•  Ini8al  state?    A  parEcular  board  configuraEon  •  Goal  vertex?    A  parEcular  board  configuraEon  

Applica8on:    8-­‐Puzzle  

85 Slide adapted from materials by Stuart Russell

Page 84: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Outline  

•  IntroducEon  •  Graph  Basics  •  Graph  Search  Problem  

– Breadth-­‐First  Search  – Depth-­‐First  Search  

•  Complexity  Analysis  

86

Page 85: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Outline  

•  IntroducEon  •  Graph  Basics  •  Graph  Search  Problem  

– Breadth-­‐First  Search  – Depth-­‐First  Search  

•  Complexity  Analysis  

87

Page 86: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

88

n  In  the  worst  case,  the  goal  vertex  won’t  be  found  

What  is  the  Time  Complexity  of  BFS  &  DFS?  

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) return c

if c.vertex is not in closed { add c.vertex to closed for each Vertex w adjacent to c.vertex

if w is not in closed queuingFn(open, new Node(w,c)); }

} }

Page 87: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) return c

if c.vertex is not in closed { add c.vertex to closed for each Vertex w adjacent to c.vertex

if w is not in closed queuingFn(open, new Node(w,c)); }

} }

89

n  In  the  worst  case,  the  goal  vertex  won’t  be  found  

Each  vertex  is  in  the  queue  at  most  once,  so  the  outer  loop  runs  at  most  |V |  iteraEons    

What  is  the  Time  Complexity  of  BFS  &  DFS?  

Page 88: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) return c

if c.vertex is not in closed { add c.vertex to closed for each Vertex w adjacent to c.vertex

if w is not in closed queuingFn(open, new Node(w,c)); }

} }

90

n  In  the  worst  case,  the  goal  vertex  won’t  be  found  

Each  vertex  is  in  the  queue  at  most  once,  so  the  outer  loop  runs  at  most  |V |  iteraEons    

Performance  will  depend  on  the  Eme  for  getAdjacent()    

What  is  the  Time  Complexity  of  BFS  &  DFS?  

Page 89: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

91

Graph  Representa8on:    Adjacency  Matrix  

2

1

3 4

5

1 2 3 4 51 0 1 0 0 02 0 0 0 1 03 0 1 0 0 04 0 0 1 0 15 1 0 0 1 0

2

1

3 4

5

1 2 3 4 51 0 1 0 0 12 1 0 1 1 03 0 1 0 1 04 0 1 1 0 15 1 0 0 1 0

Page 90: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

92

Graph  Representa8on:    Adjacency  Matrix  

2

1

3 4

5

1 2 3 4 51 0 1 0 0 02 0 0 0 1 03 0 1 0 0 04 0 0 1 0 15 1 0 0 1 0

2

1

3 4

5

1 2 3 4 51 0 1 0 0 12 1 0 1 1 03 0 1 0 1 04 0 1 1 0 15 1 0 0 1 0

What  is  the  performance  of  getAdjacent(u)?  

Page 91: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

93

Graph  Representa8on:    Adjacency  List  

2

1

3 4

5

What  is  the  performance  of  getAdjacent(u)?  

2 4

3 5

1 2 3 4 5 1 4

2

Page 92: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) return c

if c.vertex is not in closed { add c.vertex to closed for each Vertex w adjacent to c.vertex

if w is not in closed queuingFn(open, new Node(w,c)); }

} }

94

n  Using  an  adjacency  matrix:  

|V |  iteraEons    

O(|V |)    

What  is  the  Time  Complexity  of  BFS  &  DFS?  

Page 93: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) return c

if c.vertex is not in closed { add c.vertex to closed for each Vertex w adjacent to c.vertex

if w is not in closed queuingFn(open, new Node(w,c)); }

} }

95

n  Using  an  adjacency  matrix:    O(|V |2)

|V |  iteraEons    

O(|V |)    

What  is  the  Time  Complexity  of  BFS  &  DFS?  

Page 94: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

graphSearch(problem, queuingFn) { open = {}, closed = {}

queuingFn(open, new Node(problem.startvertex))

loop { if empty(open) then return FAILURE

c = removeFront(open)

if problem.goalTest(c.vertex) return c

if c.vertex is not in closed { add c.vertex to closed for each Vertex w adjacent to c.vertex

if w is not in closed queuingFn(open, new Node(w,c)); }

} }

96

n  Using  an  adjacency  list:  

|V |  iteraEons    

O(out-­‐degree(c.vertex))    

What  is  the  Time  Complexity  of  BFS  &  DFS?  

Page 95: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

97

n  For  an  adjacency  list,  looping  over  all  adjacent  verEces  of  u  will  be  O(out-­‐degree(u))  

n  Therefore,  the  traversal  performance  is      

since  the  inner  loop  is  repeated  O(  |V |  )  Emes  

n  However,  in  a  disconnected  graph,  we  must  sEll  look  at  every  vertex,  so  the  performance  is    O(  |V |  +  |E|  )  

O

0

@|V |X

i=1

out-degree(vi)

1

A= O(|E|)

What  is  the  Time  Complexity  of  BFS  &  DFS?  

How do these terms compare?

Page 96: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Sparse  vs  Dense  Graphs  

§  A  sparse  graph  is  one  with  “few”  edges.  That  is    

§  A  dense  graph  is  one  with  “many”  edges.  That  is  

98

|E| = O�|V |2

|E| = O (|V |)

2

1

3 4

5 2

1

3 4

5

Page 97: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

99

n  For  an  adjacency  list,  getAdjacent(u)  will  be                          O(out-­‐degree(u))  

n  Therefore,  the  traversal  performance  is      

since  getAdjacent  is  done  O(  |V |  )  Emes  

n  However,  in  a  disconnected  graph,  we  must  sEll  look  at  every  vertex,  so  the  performance  is    O(  |V |  +  |E|  )  

O

0

@|V |X

i=1

out-degree(vi)

1

A= O(|E|)

What  is  the  Time  Complexity  of  BFS  &  DFS?  

Ranges  from  O(  |V |  )  to  O(  |V |2),  depending  on  density  

Page 98: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

§  Really  depends  on  the  graph  representaEon  

100

What  is  the  Space  Complexity  of  BFS  &  DFS?  

2 4

3 5

1 2 3 4 5 1 4

2

Adjacency List 1 2 3 4 5

1 0 1 0 0 02 0 0 0 1 03 0 1 0 0 04 0 0 1 0 15 1 0 0 1 0

Adjacency Matrix

Page 99: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

§  Really  depends  on  the  graph  representaEon  

101

What  is  the  Space  Complexity  of  BFS  &  DFS?  

2 4

3 5

1 2 3 4 5 1 4

2

Adjacency List 1 2 3 4 5

1 0 1 0 0 02 0 0 0 1 03 0 1 0 0 04 0 0 1 0 15 1 0 0 1 0

Adjacency Matrix

Space  Complexity  O( |V | + |E| )

Space  Complexity  O( |V |2)

Page 100: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Does  BFS  find  Shortest  Paths  in  Weighted  Graphs?  

102

v1 v7 v2

v8 v4 v6 v3

v9 v10 v5

1

3

4

3 1

1

2 7

3

4

1

2

5

6

Page 101: 17GraphTraversals BFS DFS - Computer Science | Bryn · PDF file · 2013-04-24Graph&Traversals:& Breadth0Firstand&Depth0FirstSearch& Eric Eaton Bryn Mawr College Computer Science Department

Summary  

•  Breadth-­‐First  Search  – Solves  unweighted  shortest  path  problem  – Uses  FIFO  queue  – Traverses  the  graph  in  level-­‐order  

•  Depth-­‐First  Search  – Uses  LIFO  stack  – Takes  a  “deep-­‐dive”  into  the  graph  

•  Time/Space  Complexity:    O(  |V |  +  |E|  )  

103