cs10:# problem#solving#viaobjectoriented# programming#mcasey/cs10/slides/day16.pdfcs10:#...

51
CS 10: Problem solving via Object Oriented Programming Winter 2017 Tim Pierson 260 (255) Sudikoff Day 16 – Graph Traversals

Upload: others

Post on 15-Jul-2021

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

CS  10:  Problem  solving  via  Object  Oriented  

Programming  Winter  2017  

 

Tim  Pierson  260  (255)  Sudikoff  

Day  16  –  Graph  Traversals  

Page 2: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

2  

Agenda  

1.  Depth  first  search  

2.  Breadth  first  search  

Page 3: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

3  

Graph  traversals  are  useful  to  answer  quesRons  about  vertex  relaRonships  Some  Graph  traversals  uses  

•  Uses  are  typically  around  reachability    •  CompuRng  path  from  vertex  u  to  vertex  v  •  Given  start  vertex  s  of  Graph  G,  compute  a  path  with  the  

minimum  number  of  edges  between  s  and  all  other  verRces  (or  report  no  such  path  exists)  

 •  TesRng  whether  G  is  fully  connected  (e.g.,  all  verRces  reachable)    •  IdenRfying  cycles  in  G  (or  reporRng  no  cycle  exists)  

•  Today’s  examples  have  no  cycles  (next  class  will  consider  them)  

Page 4: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

4  

Depth  First  Search  (DFS)  uses  a  stack  to  explore  as  if  in  a  maze  

A  

B  

C  

D  

F  

Start  

DFS  basic  idea  •  Keep  going  unRl  you  

can’t  go  any  further,  then  back  track  

•  Relies  on  a  stack  (implicit  or  explicit)  to  keep  track  of  where  you’ve  been  

Goal  

E  

H  

G   I  

Graph  structure  from  h_p://stackoverflow.com/quesRons/687731/breadth-­‐first-­‐vs-­‐depth-­‐first  

Page 5: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

5  

Some  of  you  did  Depth  First  Search  on  Problem  Set  1  RegionFinder  Loop over all the pixels

If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so

 

Page 6: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

Loop over all the pixels If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so

 

6  

Some  of  you  did  Depth  First  Search  on  Problem  Set  1  RegionFinder  

If  you  added  to  end  of  list…  

Page 7: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

Loop over all the pixels If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so

 

7  

Some  of  you  did  Depth  First  Search  on  Problem  Set  1  RegionFinder  

And  if  you  get  pixel  from  end  of  list,  you  implemented  a  stack    

If  you  added  to  end  of  list…  

Page 8: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

8  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Page 9: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

9  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

A  

Push(A)  

Page 10: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

10  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

Pop(A),  mark  visited  

1  

Page 11: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

11  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

E   D   C   B  

Push  unvisited  adjacent  

1  

Page 12: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

12  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

E   D   C  

Pop(B),  mark  visited  

2  

1  

Page 13: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

13  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

E   D   C  

Push  unvisited  adjacent  (F,  but  not  A)  

2  

F  

1  

Page 14: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

14  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

E   D   C  

Pop(F),  mark  visited  

2  

3  

1  

Page 15: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

15  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

E   D   C  

Push  unvisited  adjacent  (H,  but  not  B)  

2  

3  

1  

H  

Page 16: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

16  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

E   D   C  

Pop(H),  mark  visited  

2  

3  

1  

4  

Page 17: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

17  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

E   D   C  

Nothing  to  push,  back  up  by  popping  C  

2  

3  

1  

4  

Page 18: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

18  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

E   D  

Pop(C),  mark  visited  

2  

3  

1  

4  

5  

Page 19: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

19  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

E   D  

Nothing  to  push,  back  up  by  popping  D  

2  

3  

1  

4  

5  

Page 20: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

20  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

E  

Pop(D),  mark  visited  

2  

3  

1  

4  

5  

6  

Page 21: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

21  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

E  

Push  unvisited  adjacent  (G,  but  not  A)  

2  

3  

1  

4  

5  

6  

G  

Page 22: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

22  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

E  

Pop(G),  mark  visited  

2  

3  

1  

4  

5  

6   7  

Page 23: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

23  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

E  

Push  unvisited  adjacent  (I,  but  not  D)  

2  

3  

1  

4  

5  

6   7  

I  

Page 24: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

24  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

E  

Pop(I),  mark  visited  

2  

3  

1  

4  

5  

6   7   8  

Page 25: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

25  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

E  

Nothing  to  push,  back  up  by  popping  E  

2  

3  

1  

4  

5  

6   7   8  

Page 26: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

26  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

Pop(E),  mark  visited  

2  

3  

1  

4  

5  

6   7   8  

9  

Page 27: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

27  

Depth  First  Search  (DFS)  is  like  exploring  a  maze  

DFS  algorithm  stack.push(s) //start node repeat until find goal vertex or stack empty:

u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent

if !v.visited stack.push(v)

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

Stack  

Found  goal  

2  

3  

1  

4  

5  

6   7   8  

9  

Page 28: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

28  

Node  discovery  tells  us  something  about  the  graph  Discovery  edges  •  Edges  that  lead  to  unvisited  nodes  •  Discovery  edges  form  a  tree  on  the  graph  •  Can  traverse  from  start  to  goal  on  tree  (if  goal  reachable)  •  Can  tell  us  which  nodes  are  not  reachable  (not  on  path  formed  by  discovery  nodes)  

•  Not  guaranteed  to  be  shortest  path!    Back  edges  •  Edges  that  lead  to  previously  discovered  nodes  •  Lead  to  ancestor  nodes  in  tree  •  Indicate  presence  of  a  cycle  in  the  graph  

Page 29: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

29  

Run  Rme  is  O(n+m)  Run  >me  •  Assume  graph  with  n  nodes  and  m  edges  •  Visit  each  node  at  most  one  Rme  (due  to  visited  indicator)  

•  Visit  each  edge  at  most  one  Rme  

•  Run  Rme  is  O(n+m)  

Page 30: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

30  

Agenda  

1.  Depth  first  search  

2.  Breadth  first  search  

Page 31: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

31  

Breadth  First  Search  (BFS)  can  find  the  shortest  path  between  nodes  

A  

B  

C  

D  

F  

Start  

BFS  basic  idea  •  Explore  outward  in  

“ripples”  

•  Look  at  all  nodes  1  step  away,  then  all  nodes  2  steps  away…  

•  Relies  on  a  queue  (implicit  or  explicit)  implementaRon  

•  Path  found  from  s  to  any  other  vertex  is  shortest  Goal  

E  

H  

G   I  

Page 32: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

32  

Some  of  you  did  Breadth  First  Search  on  Problem  Set  1  RegionFinder  Loop over all the pixels

If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so

 

Page 33: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

Loop over all the pixels If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so

 

33  

Some  of  you  did  Breadth  First  Search  on  Problem  Set  1  RegionFinder  

If  you  added  to  end  of  list…  

Page 34: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

Loop over all the pixels If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so

 

34  

Some  of  you  did  Breadth  First  Search  on  Problem  Set  1  RegionFinder  

And  if  you  get  pixel  from  front  of  list,  you  implemented  a  queue  

If  you  added  to  end  of  list…  

Page 35: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

35  

Breadth  First  Search  (BFS)  can  find  the  shortest  path  between  nodes  

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

BFS  algorithm  enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:

u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Page 36: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

36  

Breadth  First  Search  (BFS)  can  find  the  shortest  path  between  nodes  

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

BFS  algorithm  enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:

u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue  

enqueue(A)  

Page 37: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

37  

Breadth  First  Search  (BFS)  can  find  the  shortest  path  between  nodes  

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

BFS  algorithm  enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:

u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue  

enqueue(A)  

A  

1  

Page 38: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

38  

Breadth  First  Search  (BFS)  can  find  the  shortest  path  between  nodes  

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

BFS  algorithm  enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:

u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue  

dequeue(A),  unvisited  enqueue  adjacent  

B   C   D   E  

1  

Page 39: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

39  

Breadth  First  Search  (BFS)  can  find  the  shortest  path  between  nodes  

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

BFS  algorithm  enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:

u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue  

dequeue(B),  enqueue  unvisited  adjacent  F  

C   D   E   F  

2  

1  

Page 40: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

40  

Breadth  First  Search  (BFS)  can  find  the  shortest  path  between  nodes  

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

BFS  algorithm  enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:

u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue  

dequeue(C),  enqueue  unvisited  adjacent  (none)    

D   E   F  

2  

1  

3  

Page 41: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

41  

Breadth  First  Search  (BFS)  can  find  the  shortest  path  between  nodes  

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

BFS  algorithm  enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:

u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue  

dequeue(D),  enqueue  unvisited  adjacent  G  

E   F   G  

2  

1  

3  

4

Page 42: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

42  

Breadth  First  Search  (BFS)  can  find  the  shortest  path  between  nodes  

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

BFS  algorithm  enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:

u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue  

dequeue(E),  enqueue  unvisited  adjacent  

F   G  

2  

1  

3  

4

5

Page 43: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

43  

Breadth  First  Search  (BFS)  can  find  the  shortest  path  between  nodes  

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

BFS  algorithm  enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:

u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue  

Found  goal!  

F   G  

2  

1  

3  

4

5

Page 44: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

44  

Breadth  First  Search  (BFS)  can  find  the  shortest  path  between  nodes  

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

BFS  algorithm  enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:

u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue  

Keep  going  for  fun  

F   G  

2  

1  

3  

4

5

Page 45: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

45  

Breadth  First  Search  (BFS)  can  find  the  shortest  path  between  nodes  

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

BFS  algorithm  enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:

u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue  

dequeue(F),  enqueue  unvisited  adjacent  H    

G   H  

2  

1  

3  

4

5

6

Page 46: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

46  

Breadth  First  Search  (BFS)  can  find  the  shortest  path  between  nodes  

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

BFS  algorithm  enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:

u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue  

dequeue(G),  enqueue  unvisited  adjacent  I    

H   I  

2  

1  

3  

4

5

6

7

Page 47: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

47  

Breadth  First  Search  (BFS)  can  find  the  shortest  path  between  nodes  

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

BFS  algorithm  enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:

u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue  

dequeue(H),  enqueue  unvisited  adjacent  (none)    

I  

2  

1  

3  

4

5

6

7

8

Page 48: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

48  

Breadth  First  Search  (BFS)  can  find  the  shortest  path  between  nodes  

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

BFS  algorithm  enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:

u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue  

dequeue(I),  enqueue  unvisited  adjacent  (none)    

2  

1  

3  

4

5

6

7

8

9

Page 49: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

49  

Breadth  First  Search  (BFS)  can  find  the  shortest  path  between  nodes  

A  

B  

C  

D  

F  

Start  

Goal  

E  

H  

G   I  

BFS  algorithm  enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:

u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue  

All  nodes  explored  

2  

1  

3  

4

5

6

7

8

9

Page 50: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

50  

Node  discovery  tells  us  something  about  the  graph  Discovery  edges  •  Lead  to  unvisited  nodes  •  Form  a  tree  on  the  graph  •  Can  traverse  from  start  to  goal  (or  any  node)  •  Can  tell  us  which  nodes  are  not  reachable  (not  on  path  formed  by  discovery  nodes)  

•  Path  guaranteed  to  have  smallest  number  of  edges    Can  track  how  we  got  to  node  to  find  shortest  path  •  Build  vertex  tree  •  Parent  of  each  vertex  is  vertex  that  discovered  it  •  Parent  is  unique  because  we  don’t  visit  verRces  twice  

Page 51: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#

51  

Run  Rme  is  O(n+m)  Run  >me  •  Assume  graph  with  n  nodes  and  m  edges    •  Visit  each  node  at  most  one  Rme  (due  to  visited  indicator  

 •  Visit  each  edge  at  most  one  Rme  

•  Run  Rme  O(n+m)  

•  Useful  for  the  Kevin  Bacon  game!