1 / 31

Elementary Graph Algorithms

Understand Breadth-First Search (BFS) and Depth-First Search (DFS) with illustrative examples from Comp.122, Fall 2004 course materials.

grantv
Download Presentation

Elementary Graph Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Elementary Graph Algorithms Comp 122, Fall 2004

  2. white: undiscovered gray: discovered black: finished BFS(G,s) 1. for each vertex u in V[G] – {s} 2 docolor[u]  white 3 d[u]  4 [u]  null 5 color[s]  gray 6 d[s]  0 7 [s]  null 8 Q   9 enqueue(Q,s) 10 while Q   11 do u  dequeue(Q) 12 for each v in Adj[u] 13 doif color[v] = white 14 then color[v]  gray 15 d[v]  d[u] + 1 16 [v]  u 17 enqueue(Q,v) 18 color[u]  black Q: a queue of discovered vertices color[v]: color of v d[v]: distance from s to v [u]: predecessor of v Example: animation. Comp 122, Fall 2004

  3. Example (BFS) r s t u    0     v w y x Q: s 0 Comp 122, Fall 2004

  4. Example (BFS) r s t u   0 1    1 v w y x Q: w r 1 1 Comp 122, Fall 2004

  5. Example (BFS) r s t u 2  0 1  2  1 v w y x Q: r t x 1 2 2 Comp 122, Fall 2004

  6. Example (BFS) r s t u 2  0 1 2  2 1 v w y x Q: t x v 2 2 2 Comp 122, Fall 2004

  7. Example (BFS) r s t u 2 0 3 1 2  2 1 v w y x Q: x v u 2 2 3 Comp 122, Fall 2004

  8. Example (BFS) r s t u 2 0 3 1 2 3 2 1 v w y x Q: v u y 2 3 3 Comp 122, Fall 2004

  9. Example (BFS) r s t u 2 0 3 1 2 3 2 1 v w y x Q: u y 3 3 Comp 122, Fall 2004

  10. Example (BFS) r s t u 2 0 3 1 2 3 2 1 v w y x Q: y 3 Comp 122, Fall 2004

  11. Example (BFS) r s t u 2 0 3 1 2 3 2 1 v w y x Q: Comp 122, Fall 2004

  12. Example (BFS) r s t u 2 0 3 1 2 3 2 1 v w y x BFS Tree Comp 122, Fall 2004

  13. DFS DFS-Visit(u) color[u]  GRAY time  time + 1 d[u] time for each v  Adj[u] doifcolor[v] = WHITE then[v] u DFS-Visit(v) color[u]  BLACK time  time + 1 f[u]  time DFS(G) 1. for each vertex u  V[G] 2. docolor[u]  white 3. [u]  NULL 4. time 0 5. for each vertex u  V[G] 6. doifcolor[u] = white 7. then DFS-Visit(u) Uses a global timestamp time. Comp 122, Fall 2004

  14. Example (DFS) u v w 1/ x z y Comp 122, Fall 2004

  15. Example (DFS) u v w 2/ 1/ x z y Comp 122, Fall 2004

  16. Example (DFS) u v w 2/ 1/ 3/ x z y Comp 122, Fall 2004

  17. Example (DFS) u v w 2/ 1/ 3/ 4/ x z y Comp 122, Fall 2004

  18. Example (DFS) u v w 2/ 1/ B 3/ 4/ x z y Comp 122, Fall 2004

  19. Example (DFS) u v w 2/ 1/ B 3/ 4/5 x z y Comp 122, Fall 2004

  20. Example (DFS) u v w 2/ 1/ B 3/6 4/5 x z y Comp 122, Fall 2004

  21. Example (DFS) u v w 2/7 1/ B 3/6 4/5 x z y Comp 122, Fall 2004

  22. Example (DFS) u v w 2/7 1/ B F 3/6 4/5 x z y Comp 122, Fall 2004

  23. Example (DFS) u v w 2/7 1/8 B F 3/6 4/5 x z y Comp 122, Fall 2004

  24. Example (DFS) u v w 2/7 9/ 1/8 B F 3/6 4/5 x z y Comp 122, Fall 2004

  25. Example (DFS) u v w 2/7 9/ 1/8 B C F 3/6 4/5 x z y Comp 122, Fall 2004

  26. Example (DFS) u v w 2/7 9/ 1/8 B C F 3/6 4/5 10/ x z y Comp 122, Fall 2004

  27. Example (DFS) u v w 2/7 9/ 1/8 B C F 3/6 4/5 10/ B x z y Comp 122, Fall 2004

  28. Example (DFS) u v w 2/7 9/ 1/8 B C F 3/6 4/5 10/11 B x z y Comp 122, Fall 2004

  29. Example (DFS) u v w 2/7 9/12 1/8 B C F 3/6 4/5 10/11 B x z y Comp 122, Fall 2004

More Related