graphalgorithmsii dfs bfs - college of...

34
CS261 Data Structures DFS and BFS – Edge List Representa:on

Upload: others

Post on 28-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

CS261  Data  Structures  

DFS  and  BFS  –  Edge  List  Representa:on    

Page 2: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

2  

Applica:on:  Maze  Path  Finding  

•  Find  a  path  from  start  to  finish  in  a  maze:  – Easily  represent  a  maze  as  a  graph  

– Compute  single  source  (S)  reachability,  stopping  when  get  to  F  

F

S

Page 3: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

3  

Applica:on:  Maze  Path  Finding  

•  Find  a  path  from  start  to  finish  in  a  maze:  – Easily  represent  a  maze  as  a  graph  

F

S

Page 4: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

4  

Applica:on:  Maze  Path  Finding  Example  

•  Single-­‐Source  Reachability    

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

STACK  

5e  

For consistency (order in which neighbors are pushed onto the stack)

Page 5: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

5  

Applica:on:  Maze  Path  Finding  Example  

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

1  

STACK  

4e  

Page 6: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

6  

Applica:on:  Maze  Path  Finding  Example  

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

1  

STACK  

4d  3e  

2  

Page 7: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

7  

Applica:on:  Maze  Path  Finding  Example  

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

1  

STACK  

5d  3e  

2  3  

Page 8: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

8  

Applica:on:  Maze  Path  Finding  Example  

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

1  

STACK  

5c  3e  

2  3  4  

Page 9: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

9  

Applica:on:  Maze  Path  Finding  Example  

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

1  

STACK  

5b  4c  3e  

2  3  

4  5  

Page 10: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

10  

Applica:on:  Maze  Path  Finding  Example  

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

1  

STACK  

5a  4b  4c  3e  

2  3  

4  5  6  

Page 11: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

11  

Applica:on:  Maze  Path  Finding  Example  

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

1  

STACK  

4a  4b  4c  3e  

2  3  

4  5  6  7  

Page 12: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

12  

Applica:on:  Maze  Path  Finding  Example  

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

1  

STACK  

4b  4c  3e  

2  3  

4  5  6  7  

8  

DEAD  END!!  

Page 13: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

13  

Applica:on:  Maze  Path  Finding  Example  

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

1  

STACK  

4c  3e  

2  3  

4  5  6  7  

8   9  

Page 14: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

14  

Applica:on:  Maze  Path  Finding  Example  

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

1  

STACK  

3c  3e  

2  3  

4  5  6  7  

8   9   10  

Page 15: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

15  

Applica:on:  Maze  Path  Finding  Example  

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

1  

STACK  

3b  2c  3e  

2  3  

4  5  6  7  

8   9   10  

11  

Page 16: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

16  

Applica:on:  Maze  Path  Finding  Example  

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

1  

STACK  

 2c  3e  

2  3  

4  5  6  7  

8   9   10  

11  12  13  

14  

15  

Page 17: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

What  happens  if  we  use  a  Queue?  

Page 18: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

18  

Applica:on:  Maze  Path  Finding  Example  

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

QUEUE  

5e  

Page 19: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

19  

Applica:on:  Maze  Path  Finding  Example  

1

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

QUEUE  

4e  

Page 20: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

20  

Applica:on:  Maze  Path  Finding  Example  

2

1

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

QUEUE  

4d  3e  

Page 21: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

21  

Applica:on:  Maze  Path  Finding  Example  

3

2

1

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

QUEUE  

3d  2e  4d    

Page 22: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

22  

Applica:on:  Maze  Path  Finding  Example  

3

4 2

1

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

QUEUE  

5d  3d  2e      

Page 23: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

23  

Applica:on:  Maze  Path  Finding  Example  

5

3

4 2

1

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

QUEUE  

1e  5d  3d        

Page 24: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

24  

Applica:on:  Maze  Path  Finding  Example  

5

6 3

4 2

1

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

QUEUE  

1e  5d          

Page 25: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

25  

Applica:on:  Maze  Path  Finding  Example  

5

6 3

4 2

7 1

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

QUEUE  

5c  1e            

Page 26: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

26  

Applica:on:  Maze  Path  Finding  Example  

8 5

6 3

4 2

7 1

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

QUEUE  

1d  5c              

Page 27: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

27  

Applica:on:  Maze  Path  Finding  Example  

8 5

6 3

4 2

9 7 1

 a  b  c  d  e  

1  

2  

3  

4  

5   1  

3  

2  4  

QUEUE  

5b  4c  1d              

Page 28: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

28  

25 19 14 10 8 24 22 18 13 5

23 20 15 6 3

21 16 11 4 2

17 12 9 7 1

15 14

13 12 11

8 9 10 3 2

7 6 5 4 1

Applica:on:  Maze  Path  Finding  

Depth-­‐First  (Stack)   Breadth-­‐First  (Queue)  

Page 29: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

DFS  vs.  BFS  

•  DFS  like  a  single  person  working  a  maze  •  BFS  like  a  wave  flowing  through  a  maze  •  DFS  can  take  an  unfortunate  route  and  have  to  backtrack  a  

long  way,  and  mul:ple  :mes  •  DFS  can  get  lucky  and  find  the  solu:on  very  quickly  •  BFS  may  not  find  it  as  quickly,  but  will  always  find  it  •  Because  BFS  first  checks  all  paths  of  length  1,  then  of  length  2,  

then  of  length  3,  etc….it’s  guaranteed  to  find  a  path  containing  the  least  steps  from  start  to  goal  (if  it  exists)  

•  What  if  there’s  one  infinite  path….DFS  may  go  down  it…but  BFS  will  not  get  stuck  in  it  

Page 30: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&
Page 31: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

Time  Complexity:  DFS/BFS  

•  O(V+E)  :me    in  both  cases  –  Key  observa:on:    Edge  list  scanned  once  for  each  vertex,  so  scans  E  edges  

Ini:alize  set  of  reachable  ver:ces  and  add  vi  to  a  stack  

While  stack  is  not  empty    Get  and  remove  (pop)  last  vertex  v  from  stack  

     if  vertex  v  is  not  in  reachable,        add  it  to  reachable      For  all  neighbors,  vj,  of  v,  if  vj  is  NOT  in  reachable        add  to  stack  

Page 32: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

Space  Complexithy:  DFS/BFS  

•  What  about  space?  – BFS  must  store  all  ver:ces  on  a  Queue  at  most  once  

– DFS  uses  a  Stack  and  stores  all  ver:ces  on  the  stack  at  most  once  

–  In  both  cases,  O(V)  space  worst  case  –  In  prac:ce,  BFS  may  take  up  more  space  because  it  looks  at  all  paths  of  a  specific  length  at  once.      e.g.  if  search  a  deep  tree  ,  BFS  will  store  lots  of  long  poten:al  paths  

Page 33: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

DFS  vs.  BFS  :In  prac:ce  

•  Depends  on  the  problem  –  If  there  are  some  very  deep  paths,  DFS  could  spend  a  lot  of  :me  going  down  them  

–  If  it’s  a  very  broad/wide  tree,  BFS  could  require  a  lot  of  memory  on  the  queue  

–  If  you  need  to  find  a  shortest  path,  BFS  guarantees  is  – Are  solu:ons  near  top  of  the  tree?  

•  BFS  may  find  it  more  quickly  •  e.g.  Search  a  family  tree  for  distant  ancestor  who  was  alive  a  long  :me  ago  

– Are  solu:ons  at  the  leaves  •  DFS  can  find  it  more  quickly  •  e.g.  Search  a  family  tree  for  someone  who’s  s:ll  alive  

Page 34: GraphAlgorithmsII DFS BFS - College of Engineeringweb.engr.oregonstate.edu/~hessro/static/media/... · DFSvs.BFS • DFS&like&asingle&person&working&amaze& • BFS&like&awave&flowing&through&amaze&

Implementa:on  Varia:ons    

•  Can  easily  do  DFS  recursively  •  Can  avoid  “Reachable”  in  both  DFS/BFS  by  instead,  adding  a  color  field  to  each  node  – white:        unvisited  – gray:      considered  (on  queue,  stack)  – black:      reachable  

•  Store  addi:onal  informa:on  to  use  in  solving  other  important  graph  problems