graph basics
play

Graph Basics Lecturer: Shi Li Department of Computer Science and - PowerPoint PPT Presentation

CSE 431/531: Analysis of Algorithms Graph Basics Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo Outline Graphs 1 Connectivity and Graph Traversal 2 Testing Bipartiteness Topological Ordering 3 2/28


  1. Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 14/28

  2. Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28

  3. Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28

  4. Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28

  5. Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28

  6. Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28

  7. Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28

  8. Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28

  9. Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28

  10. Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28

  11. Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28

  12. Depth-First Search (DFS) Starting from s Travel through the first edge leading out of the current vertex When reach an already-visited vertex (“dead-end”), go back Travel through the next edge If tried all edges leading out of the current vertex, go back 1 7 2 3 4 5 8 6 14/28

  13. Implementing DFS using a Stack DFS ( s ) head ← 1 , stack [1] ← s 1 mark all vertices as “unexplored” 2 while head ≥ 1 3 v ← stack [ head ] , head ← head − 1 4 if v is unexplored then 5 mark v as “explored” 6 for all neighbours u of v 7 if u is not explored then 8 head ← head + 1 , stack [ head ] = u 9 Running time: O ( n + m ) . 15/28

  14. Example of DFS using Stack head 1 7 2 3 1 4 5 8 6 explored vertices: 16/28

  15. Example of DFS using Stack v head 1 7 2 3 4 5 8 6 explored vertices: 16/28

  16. Example of DFS using Stack v head 1 7 2 3 4 5 8 6 1 explored vertices: 16/28

  17. Example of DFS using Stack v head 1 7 2 3 3 2 4 5 8 6 1 explored vertices: 16/28

  18. Example of DFS using Stack head 1 7 v 2 3 3 4 5 8 6 1 explored vertices: 16/28

  19. Example of DFS using Stack head 1 7 v 2 3 3 4 5 8 6 1 2 explored vertices: 16/28

  20. Example of DFS using Stack head 1 7 v 2 3 3 5 4 3 4 5 8 6 1 2 explored vertices: 16/28

  21. Example of DFS using Stack head 1 7 v 2 3 3 5 4 4 5 8 6 1 2 explored vertices: 16/28

  22. Example of DFS using Stack head 1 7 v 2 3 3 5 4 4 5 8 6 1 2 3 explored vertices: 16/28

  23. Example of DFS using Stack head 1 7 v 2 3 3 5 4 8 7 5 4 5 8 6 1 2 3 explored vertices: 16/28

  24. Example of DFS using Stack head 1 7 2 3 3 5 4 8 7 v 4 5 8 6 1 2 3 explored vertices: 16/28

  25. Example of DFS using Stack head 1 7 2 3 3 5 4 8 7 v 4 5 8 6 1 2 3 5 explored vertices: 16/28

  26. Example of DFS using Stack head 1 7 2 3 3 5 4 8 7 6 4 v 4 5 8 6 1 2 3 5 explored vertices: 16/28

  27. Example of DFS using Stack head 1 7 2 3 v 3 5 4 8 7 6 4 5 8 6 1 2 3 5 explored vertices: 16/28

  28. Example of DFS using Stack head 1 7 2 3 v 3 5 4 8 7 6 4 5 8 6 1 2 3 5 4 explored vertices: 16/28

  29. Example of DFS using Stack head 1 7 2 3 3 5 4 8 7 4 5 8 v 6 1 2 3 5 4 explored vertices: 16/28

  30. Example of DFS using Stack head 1 7 2 3 3 5 4 8 7 4 5 8 v 6 1 2 3 5 4 6 explored vertices: 16/28

  31. Example of DFS using Stack v head 1 7 2 3 3 5 4 8 4 5 8 6 1 2 3 5 4 6 explored vertices: 16/28

  32. Example of DFS using Stack v head 1 7 2 3 3 5 4 8 4 5 8 6 1 2 3 5 4 6 7 explored vertices: 16/28

  33. Example of DFS using Stack v head 1 7 2 3 3 5 4 8 8 4 5 8 6 1 2 3 5 4 6 7 explored vertices: 16/28

  34. Example of DFS using Stack head 1 7 2 3 3 5 4 8 v 4 5 8 6 1 2 3 5 4 6 7 explored vertices: 16/28

  35. Example of DFS using Stack head 1 7 2 3 3 5 4 8 v 4 5 8 6 1 2 3 5 4 6 7 8 explored vertices: 16/28

  36. Example of DFS using Stack head 1 7 2 3 3 5 4 v 4 5 8 6 1 2 3 5 4 6 7 8 explored vertices: 16/28

  37. Example of DFS using Stack head 1 7 2 3 v 3 5 4 5 8 6 1 2 3 5 4 6 7 8 explored vertices: 16/28

  38. Example of DFS using Stack head 1 7 2 3 3 v 4 5 8 6 1 2 3 5 4 6 7 8 explored vertices: 16/28

  39. Example of DFS using Stack head 1 7 v 2 3 4 5 8 6 1 2 3 5 4 6 7 8 explored vertices: 16/28

  40. Implementing DFS using Recurrsion DFS( s ) mark all vertices as “unexplored” 1 recursive-DFS( s ) 2 recursive-DFS ( v ) if v is explored then return 1 mark v as “explored” 2 for all neighbours u of v 3 recursive-DFS( u ) 4 17/28

  41. Outline Graphs 1 Connectivity and Graph Traversal 2 Testing Bipartiteness Topological Ordering 3 18/28

  42. Testing Bipartiteness: Applications of BFS Def. A graph G = ( V, E ) is a bipartite graph if there is a partition of V into two sets L and R such that for every edge ( u, v ) ∈ E , we have either u ∈ L, v ∈ R or v ∈ L, u ∈ R . 19/28

  43. Testing Bipartiteness Taking an arbitrary vertex s ∈ V 20/28

  44. Testing Bipartiteness Taking an arbitrary vertex s ∈ V Assuming s ∈ L w.l.o.g 20/28

  45. Testing Bipartiteness Taking an arbitrary vertex s ∈ V Assuming s ∈ L w.l.o.g Neighbors of s must be in R 20/28

  46. Testing Bipartiteness Taking an arbitrary vertex s ∈ V Assuming s ∈ L w.l.o.g Neighbors of s must be in R Neighbors of neighbors of s must be in L 20/28

  47. Testing Bipartiteness Taking an arbitrary vertex s ∈ V Assuming s ∈ L w.l.o.g Neighbors of s must be in R Neighbors of neighbors of s must be in L · · · 20/28

  48. Testing Bipartiteness Taking an arbitrary vertex s ∈ V Assuming s ∈ L w.l.o.g Neighbors of s must be in R Neighbors of neighbors of s must be in L · · · Report “not a bipartite graph” if contradiction was found 20/28

  49. Testing Bipartiteness Taking an arbitrary vertex s ∈ V Assuming s ∈ L w.l.o.g Neighbors of s must be in R Neighbors of neighbors of s must be in L · · · Report “not a bipartite graph” if contradiction was found If G contains multiple connected components, repeat above algorithm for each component 20/28

  50. Test Bipartiteness 21/28

  51. Test Bipartiteness 21/28

  52. Test Bipartiteness 21/28

  53. Test Bipartiteness 21/28

  54. Test Bipartiteness 21/28

  55. Test Bipartiteness 21/28

Recommend


More recommend