graph algorithms
play

Graph Algorithms L.F.O.A. Lecture Full Of Acronyms Graph Search - PDF document

15-251: Great Theoretical Ideas in Computer Science Lecture 11 Graph Algorithms L.F.O.A. Lecture Full Of Acronyms Graph Search Algorithms The most basic graph algorithms: Given a graph G = (V,E) Check if vertex s can reach vertex t.


  1. 15-251: Great Theoretical Ideas in Computer Science Lecture 11 Graph Algorithms L.F.O.A. Lecture Full Of Acronyms Graph Search Algorithms The most basic graph algorithms: Given a graph G = (V,E) … • Check if vertex s can reach vertex t. BFS: Breadth-first search • Decide if G is connected. DFS: Depth-first search • Identify connected components of G. All reduce to: AFS: Arbitrary-first search “Given s∈V , identify all nodes reachable from s .” (We’ll call this set C ONN C OMP (s).) What problems do these algorithms solve? Algorithm AFS(G,s) does exactly this. Bonus of AFS(G,s): Bonus of AFS(G,s): Finds a spanning tree of C ONN C OMP (s) rooted at s. Finds a spanning tree of C ONN C OMP (s) rooted at s. Given G = (V,E), a spanning tree is Given G = (V,E), a spanning tree is a tree T = ( V,Eʹ) such that Eʹ ⊆ E. a tree T = ( V,Eʹ) such that Eʹ ⊆ E. More informally, a minimal set of edges p q r connecting up all vertices of G. w x s y z t u v 1

  2. Bonus of AFS(G,s): Bonus of AFS(G,s): Finds a spanning tree of C ONN C OMP (s) rooted at s. Finds a spanning tree of C ONN C OMP (s) rooted at s. Given G = (V,E), a spanning tree is Given G = (V,E), a spanning tree is a tree T = ( V,Eʹ) such that Eʹ ⊆ E. a tree T = ( V,Eʹ) such that Eʹ ⊆ E. p q r p q r w x s y z w x s y z t u v t u v AFS(G,s): Finding all nodes reachable from s AFS(G,s): Finding all nodes reachable from s G p q r V = { a,b,c,p,q,r,s,t,u,v,w,x,y,z } a w x s y z E = { {a,b},{a,c},{b,c},{p,q},{p,x},{q,r}, b c {q,s},{r,y},{s,u},{s,x},{s,y},{t,u}, t u v {t,x},{u,v},{v,y},{w,x},{y,z} } “Duh, it’s these ones.” But it’s not so obvious when the input looks like… AFS(G,s): Finding all nodes reachable from s G: 1 2 3 4 // Has a “bag” data structure holding tiles // Each tile has a vertex name written on it s = 1 Put s into bag 5 6 7 8 While bag is not empty: Pick an Arbitrary tile v from bag If v is “unmarked”: AFS(G,s): “Mark” v For each neighbor w of v: Put s into bag Put w into bag While bag is not empty: Pick arbitrary tile v from bag Intent: If v is “unmarked”: 1 “Marked” vertices should be those reachable from s. “Mark” v For each neighbor w of v: w in bag means we want to keep exploring from w. Put w into bag 2

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

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

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

  6. ✓ ✓ G: G: 1 2 3 4 1 2 3 4 s = 1 s = 1 ✓ ✓ ✓ ✓ 5 6 7 8 5 6 7 8 1 et cetera AFS(G,s): AFS(G,s): Put s into bag Put s into bag While bag is not empty: While bag is not empty: Pick arbitrary tile v from bag Pick arbitrary tile v from bag If v is “unmarked”: If v is “unmarked”: 2 5 2 5 “Mark” v “Mark” v 2 2 2 2 For each neighbor w of v: For each neighbor w of v: 6 6 5 3 5 3 Put w into bag Put w into bag Analysis of AFS Why does AFS halt? Every time a bunch of tiles is added to bag, Want to show: When this algorithm halts, it’s because some vertex v just got marked. { marked vertices } ∴ we add at most |V| bunches of tiles to the bag = (since each vertex is marked ≤ 1 time). .{ vertices reachable from s }. ∴ at most finitely many AFS(G,s): tiles ever go into the bag. Put s into bag { marked } ⊆ { reachable } : This is clear. While bag is not empty: Each iteration through Pick arbitrary tile v from bag loop removes 1 tile. { reachable } ⊆ { marked } : If v is “unmarked”: “Mark” v ∴ AFS halts after finitely Wait, why does the algorithm even halt?! For each neighbor w of v: many iterations. Put w into bag A more careful analysis A more careful analysis Every time a bunch of tiles is added to bag, Every time a bunch of tiles is added to bag, it’s because some vertex v just got marked. it’s because some vertex v just got marked. In this case, we add deg(v) tiles to the bag. In this case, we add deg(v) tiles to the bag. ∴ total number of tiles that ever enter the bag is ∴ total number of tiles that ever enter the bag is = 2|E| = 2|E| ≤ ≤ AFS(G,s): AFS(G,s): Put s into bag Put s into bag While bag is not empty: While bag is not empty: Each iteration through Each iteration through Pick arbitrary tile v from bag Pick arbitrary tile v from bag loop removes 1 tile. loop removes 1 tile. If v is “unmarked”: If v is “unmarked”: “Mark” v “Mark” v ∴ AFS halts after finitely ∴ AFS halts after ≤ 2|E| For each neighbor w of v: For each neighbor w of v: many iterations. many iterations. Put w into bag Put w into bag 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend