cs 401 computer algorithm i
play

CS 401: Computer Algorithm I DFS / Topological Ordering Xiaorui - PowerPoint PPT Presentation

CS 401: Computer Algorithm I DFS / Topological Ordering Xiaorui Sun 1 BFS Application: Testing Bipartiteness Problem: Given a graph ! , is it bipartite? Many graph problems become: Easier/Tractable if the underlying graph is bipartite


  1. CS 401: Computer Algorithm I DFS / Topological Ordering Xiaorui Sun 1

  2. BFS Application: Testing Bipartiteness Problem: Given a graph ! , is it bipartite? Many graph problems become: • Easier/Tractable if the underlying graph is bipartite (matching) Before attempting to design an algorithm, we need to understand structure of bipartite graphs. v 2 v 2 v 3 v 1 v 4 v 6 v 5 v 4 v 3 v 5 v 6 v 7 v 1 v 7 a bipartite graph G another drawing of G 2

  3. A Characterization of Bipartite Graphs Lemma: Let ! be a connected graph, and let " 0 , … , " & be the layers produced by BFS( ' ). Exactly one of the following holds. (i) No edge of ! joins two nodes of the same layer, and ! is bipartite. (ii) An edge of ! joins two nodes of the same layer, and ! contains an odd-length cycle (and hence is not bipartite). L 1 L 2 L 3 L 2 L 3 L 1 3 Case (ii) Case (i)

  4. Obstruction to Bipartiteness Corollary: A graph ! is bipartite if and only if it contains no odd length cycles. Furthermore, one can test bipartiteness using BFS. Bipartiteness algorithm: • Run BFS on an arbitrary vertex • If there is a non-tree edge connecting two vertices at the same level of the BFS tree, then the graph is not a bipartite graph, otherwise, the graph is a bipartite graph. 4

  5. BFS Summary Breadth First Search (BFS): Explore vertices according to the order of the discovery of vertices Property: • BFS tree • Level = distance (length of shortest path) from the initial vertex • Every edge connect two vertices at the same or adjacent levels Applications of BFS: • Finding connected components of a graph 5 • Testing bipartiteness

  6. Depth First Search Follow the first path you find as far as you can go; back up to last unexplored edge when you reach a dead end, then go as far you can Naturally implemented using recursive calls or a stack 6

  7. DFS(s) – Recursive version Initialization: mark all vertices undiscovered DFS( ! ) Mark ! discovered for each edge {!, $} if ( $ is undiscovered) DFS( $ ) Mark ! fully-explored 7

  8. Color code: DFS(A) undiscovered discovered fully-explored A,1 Suppose edge lists Call Stack at each vertex (Edge list): are sorted B J alphabetically A (B,J) C G H K L D F M I st[] = {1} E 8

  9. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J A (B,J) B (A,C,J) C G H K L D F M I st[] = {1,2} E 9

  10. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J A (B,J) B (A,C,J) C,3 C (B,D,G,H) G H K L D F M I st[] = {1,2,3} E 10

  11. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J A (B,J) B (A,C,J) C,3 C (B,D,G,H) D (C,E,F) G H K L D,4 F M I st[] = {1,2,3,4} E 11

  12. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J A (B,J) B (A,C,J) C,3 C (B,D,G,H) D (C,E,F) E (D,F) G H K L D,4 F M I st[] = {1,2,3,4,5} E,5 12

  13. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J A (B,J) B (A,C,J) C,3 C (B,D,G,H) D (C,E,F) E (D,F) G H K L F (D,E,G) D,4 F,6 M I st[] = {1,2,3,4,5, 6} E,5 13

  14. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J A (B,J) B (A,C,J) C,3 C (B,D,G,H) D (C,E,F) E (D,F) G,7 H K L F (D,E,G) G(C,F) D,4 F,6 M I st[] = {1,2,3,4,5, 6,7} E,5 14

  15. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J A (B,J) B (A,C,J) C,3 C (B,D,G,H) D (C,E,F) E (D,F) G,7 H K L F (D,E,G) G(C,F) D,4 F,6 M I st[] = {1,2,3,4,5, 6,7} E,5 15

  16. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J A (B,J) B (A,C,J) C,3 C (B,D,G,H) D (C,E,F) E (D,F) H K L G,7 F (D,E,G) D,4 F,6 M I st[] = {1,2,3,4,5, 6} E,5 16

  17. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J A (B,J) B (A,C,J) C,3 C (B,D,G,H) D (C,E,F) E (D,F) H K L G,7 D,4 F,6 M I st[] = {1,2,3,4,5} E,5 17

  18. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J A (B,J) B (A,C,J) C,3 C (B,D,G,H) D (C,E,F) H K L G,7 D,4 F,6 M I st[] = {1,2,3,4} E,5 18

  19. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J A (B,J) B (A,C,J) C,3 C (B,D,G,H) H K L G,7 D,4 F,6 M I st[] = {1,2,3} E,5 19

  20. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J A (B,J) B (A,C,J) C,3 C (B,D,G,H) H (C,I,J) H,8 K L G,7 D,4 F,6 M I st[] = {1,2,3,8} E,5 20

  21. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J A (B,J) B (A,C,J) C,3 C (B,D,G,H) H (C,I,J) I (H) H,8 K L G,7 D,4 F,6 I,9 M st[] = {1,2,3,8,9} E,5 21

  22. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J A (B,J) B (A,C,J) C,3 C (B,D,G,H) H (C,I,J) H,8 K L G,7 D,4 F,6 I,9 M st[] = {1,2,3,8} E,5 22

  23. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J,10 A (B,J) B (A,C,J) C,3 C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) H,8 K L G,7 D,4 F,6 I,9 M st[] = {1,2,3,8, 10} E,5 23

  24. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J,10 A (B,J) B (A,C,J) C,3 C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) H,8 K,11 L G,7 K (J,L) D,4 F,6 I,9 M st[] = {1,2,3,8,10 ,11} E,5 24

  25. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J,10 A (B,J) B (A,C,J) C,3 C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) H,8 K,11 L,12 G,7 K (J,L) L (J,K,M) D,4 F,6 I,9 M st[] = {1,2,3,8,10 ,11,12} E,5 25

  26. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J,10 A (B,J) B (A,C,J) C,3 C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) H,8 K,11 L,12 G,7 K (J,L) L (J,K,M) M(L) D,4 F,6 I,9 M,13 st[] = {1,2,3,8,10 ,11,12,13} E,5 26

  27. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J,10 A (B,J) B (A,C,J) C,3 C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) H,8 K,11 L,12 G,7 K (J,L) L (J,K,M) D,4 F,6 I,9 M,13 st[] = {1,2,3,8,10 ,11,12} E,5 27

  28. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J,10 A (B,J) B (A,C,J) C,3 C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) H,8 K,11 L,12 G,7 K (J,L) D,4 F,6 I,9 M,13 st[] = {1,2,3,8,10 ,11} E,5 28

  29. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J,10 A (B,J) B (A,C,J) C,3 C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1,2,3,8, 10} E,5 29

  30. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J,10 A (B,J) B (A,C,J) C,3 C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1,2,3,8, 10} E,5 30

  31. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J,10 A (B,J) B (A,C,J) C,3 C (B,D,G,H) H (C,I,J) H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1,2,3,8} E,5 31

  32. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J,10 A (B,J) B (A,C,J) C,3 C (B,D,G,H) H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1,2,3} E,5 32

  33. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J,10 A (B,J) B (A,C,J) C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1,2} E,5 33

  34. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J,10 A (B,J) B (A,C,J) C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1,2} E,5 34

  35. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J,10 A (B,J) C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1} E,5 35

  36. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J,10 A (B,J) C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {1} E,5 36

  37. Color code: DFS(A) undiscovered discovered fully-explored A,1 Call Stack: (Edge list) B,2 J,10 TA-DA!! C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 st[] = {} E,5 37

  38. DFS(A) Edge code: Tree edge Back edge A,1 B,2 J,10 C,3 H,8 K,11 L,12 G,7 D,4 F,6 I,9 M,13 E,5 38

  39. Edge code: DFS(A) A,1 Tree edge Back edge B,2 No Cross Edges! C,3 H,8 D,4 J,10 E,5 I,9 K,11 F,6 L,12 G,7 M,13 39

  40. Properties of (undirected) DFS Like BFS( ! ): • DFS( ! ) visits " iff there is a path in G from ! to " So, we can use DFS to find connected components • Edges into then-undiscovered vertices define a tree – the "depth first spanning tree" of G Unlike the BFS tree: • The DF spanning tree isn't minimum depth • Its levels don't reflect min distance from the root • Non-tree edges never join vertices on the same or adjacent levels 40

Recommend


More recommend