graph algorithms l f o a
play

Graph Algorithms L.F.O.A. Lecture Full Of Acronyms The most basic - PowerPoint PPT Presentation

15-251: Great Theoretical Ideas in Computer Science Lecture 12 Graph Algorithms L.F.O.A. Lecture Full Of Acronyms The most basic graph algorithms: BFS: Breadth-first search DFS: Depth-first search AFS: Arbitrary-first search What


  1. 15-251: Great Theoretical Ideas in Computer Science Lecture 12 Graph Algorithms

  2. L.F.O.A. Lecture Full Of Acronyms

  3. The most basic graph algorithms: BFS: Breadth-first search DFS: Depth-first search AFS: Arbitrary-first search What problems do these algorithms solve?

  4. Graph Search Algorithms Given a graph G = (V,E) … • Check if vertex s can reach vertex t. • Decide if G is connected. • Identify connected components of G. All reduce to: “Given s∈V , identify all nodes reachable from s .” (We’ll call this set C ONN C OMP (s).) Algorithm AFS(G,s) does exactly this.

  5. Bonus of AFS(G,s): Finds a spanning tree of C ONN C OMP (s) rooted at s. Given G = (V,E), a spanning tree is a tree T = ( V,Eʹ) such that Eʹ ⊆ E. More informally, a minimal set of edges connecting up all vertices of G.

  6. Bonus of AFS(G,s): Finds a spanning tree of C ONN C OMP (s) rooted at s. Given G = (V,E), a spanning tree is a tree T = ( V,Eʹ) such that Eʹ ⊆ E. p q r w x y z s t u v

  7. Bonus of AFS(G,s): Finds a spanning tree of C ONN C OMP (s) rooted at s. Given G = (V,E), a spanning tree is a tree T = ( V,Eʹ) such that Eʹ ⊆ E. p q r w x y z s t u v

  8. Bonus of AFS(G,s): Finds a spanning tree of C ONN C OMP (s) rooted at s. Given G = (V,E), a spanning tree is a tree T = ( V,Eʹ) such that Eʹ ⊆ E. p q r w x y z s t u v

  9. AFS(G,s): Finding all nodes reachable from s G p q r a w x s y z b c t u v “Duh, it’s these ones.” But it’s not so obvious when the input looks like…

  10. AFS(G,s): Finding all nodes reachable from s V = { a,b,c,p,q,r,s,t,u,v,w,x,y,z } E = { {a,b},{a,c},{b,c},{p,q},{p,x},{q,r}, {q,s},{r,y},{s,u},{s,x},{s,y},{t,u}, {t,x},{u,v},{v,y},{w,x},{y,z} }

  11. AFS(G,s): Finding all nodes reachable from s // Has a “bag” data structure holding tiles // Each tile has a vertex name written on it Put s into bag While bag is not empty: Pick an Arbitrary tile v from bag If v is “unmarked”: “Mark” v For each neighbor w of v: Put w into bag Intent: “Marked” vertices should be those reachable from s. w in bag means we want to keep exploring from w.

  12. G: 1 2 3 4 s = 1 5 6 7 8 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 1 “Mark” v For each neighbor w of v: Put w into bag

  13. G: 1 2 3 4 s = 1 5 6 7 8 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 1 “Mark” v For each neighbor w of v: Put w into bag

  14. G: 1 2 3 4 s = 1 5 6 7 8 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 1 “Mark” v For each neighbor w of v: Put w into bag

  15. G: 1 2 3 4 s = 1 5 6 7 8 1 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: “Mark” v For each neighbor w of v: Put w into bag

  16. ✓ G: 1 2 3 4 s = 1 5 6 7 8 1 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: “Mark” v For each neighbor w of v: Put w into bag

  17. ✓ G: 1 2 3 4 s = 1 5 6 7 8 1 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 2 5 “Mark” v For each neighbor w of v: 6 Put w into bag

  18. ✓ G: 1 2 3 4 s = 1 5 6 7 8 1 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 2 5 “Mark” v For each neighbor w of v: 6 Put w into bag

  19. ✓ G: 1 2 3 4 s = 1 5 6 7 8 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 2 5 “Mark” v For each neighbor w of v: 6 Put w into bag

  20. ✓ G: 1 2 3 4 s = 1 5 6 7 8 6 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 2 5 “Mark” v For each neighbor w of v: Put w into bag

  21. ✓ G: 1 2 3 4 s = 1 ✓ 5 6 7 8 6 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 2 5 “Mark” v For each neighbor w of v: Put w into bag

  22. ✓ G: 1 2 3 4 s = 1 ✓ 5 6 7 8 6 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 2 5 “Mark” v 1 2 For each neighbor w of v: 7 5 Put w into bag

  23. ✓ G: 1 2 3 4 s = 1 ✓ 5 6 7 8 6 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 2 5 “Mark” v 1 2 For each neighbor w of v: 7 5 Put w into bag

  24. ✓ G: 1 2 3 4 s = 1 ✓ 5 6 7 8 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 2 5 “Mark” v 1 2 For each neighbor w of v: 7 5 Put w into bag

  25. ✓ G: 1 2 3 4 s = 1 ✓ 5 6 7 8 7 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 2 5 “Mark” v 1 2 For each neighbor w of v: 5 Put w into bag

  26. ✓ G: 1 2 3 4 s = 1 ✓ ✓ 5 6 7 8 7 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 2 5 “Mark” v 1 2 For each neighbor w of v: 5 Put w into bag

  27. ✓ G: 1 2 3 4 s = 1 ✓ ✓ 5 6 7 8 7 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 2 5 “Mark” v 1 2 2 For each neighbor w of v: 3 6 5 Put w into bag

  28. ✓ G: 1 2 3 4 s = 1 ✓ ✓ 5 6 7 8 7 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 2 5 “Mark” v 1 2 2 For each neighbor w of v: 3 6 5 Put w into bag

  29. ✓ G: 1 2 3 4 s = 1 ✓ ✓ 5 6 7 8 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 2 5 “Mark” v 1 2 2 For each neighbor w of v: 3 6 5 Put w into bag

  30. ✓ G: 1 2 3 4 s = 1 ✓ ✓ 5 6 7 8 1 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 2 5 “Mark” v 2 2 For each neighbor w of v: 3 6 5 Put w into bag

  31. ✓ G: 1 2 3 4 s = 1 ✓ ✓ 5 6 7 8 1 AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 2 5 “Mark” v 2 2 For each neighbor w of v: 3 6 5 Put w into bag

  32. ✓ G: 1 2 3 4 s = 1 ✓ ✓ 5 6 7 8 et cetera AFS(G,s): Put s into bag While bag is not empty: Pick arbitrary tile v from bag If v is “unmarked”: 2 5 “Mark” v 2 2 For each neighbor w of v: 3 6 5 Put w into bag

  33. Analysis of AFS Want to show: When this algorithm halts, { marked vertices } = .{ vertices reachable from s }. { marked } ⊆ { reachable } : This is clear. { reachable } ⊆ { marked } : Wait, why does the algorithm even halt?!

  34. Why does AFS halt? Every time a bunch of tiles is added to bag, it’s because some vertex v just got marked. ♦ we add at most |V| bunches of tiles to the bag (since each vertex is marked ≤ 1 time). ♦ at most finitely many AFS(G,s): tiles ever go into the bag. Put s into bag While bag is not empty: Each iteration through Pick arbitrary tile v from bag loop removes 1 tile. If v is “unmarked”: “Mark” v ♦ AFS halts after finitely For each neighbor w of v: many iterations. Put w into bag

  35. A more careful analysis Every time a bunch of tiles is added to bag, it’s because some vertex v just got marked. In this case, we add deg(v) tiles to the bag. ♦ total number of tiles that ever enter the bag is ≤ = 2|E| AFS(G,s): Put s into bag While bag is not empty: Each iteration through Pick arbitrary tile v from bag loop removes 1 tile. If v is “unmarked”: “Mark” v ♦ AFS halts after finitely For each neighbor w of v: many iterations. Put w into bag

  36. A more careful analysis Every time a bunch of tiles is added to bag, it’s because some vertex v just got marked. In this case, we add deg(v) tiles to the bag. ♦ total number of tiles that ever enter the bag is ≤ = 2|E| AFS(G,s): Put s into bag While bag is not empty: Each iteration through Pick arbitrary tile v from bag loop removes 1 tile. If v is “unmarked”: “Mark” v ♦ AFS halts after ≤ 2|E| For each neighbor w of v: many iterations. Put w into bag

  37. A more careful analysis Every time a bunch of tiles is added to bag, it’s because some vertex v just got marked. In this case, we add deg(v) tiles to the bag. ♦ total number of tiles that ever enter the bag is ≤ = 2|E| AFS(G,s): Put s into bag While bag is not empty: Each iteration through we forgot about Pick arbitrary tile v from bag this line loop removes 1 tile. If v is “unmarked”: “Mark” v ♦ AFS halts after ≤ 2|E| +1 For each neighbor w of v: many iterations. Put w into bag

Recommend


More recommend