lecture 15
play

Lecture 15 Minimum Spanning Trees Announcements HW5 due Friday - PowerPoint PPT Presentation

Lecture 15 Minimum Spanning Trees Announcements HW5 due Friday HW6 released Friday Last time Greedy algorithms Make a series of choices. Choose this activity, then that one, .. Never backtrack. Show that, at each step,


  1. Does it work? • We need to show that our greedy choices don’t rule out success. • That is, at every step: • There exists an MST that contains all of the edges we have added so far. • Now it is time to use our lemma!

  2. Lemma • Let A be a set of edges, and consider a cut that respects A. • Suppose there is an MST containing A. • Let (u,v) be a light edge. • Then there is an MST containing A ∪ {(u,v)} This edge is light 8 B C D 7 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F A is the thick orange edges

  3. Suppose we are partway through Prim • Assume that our choices A so far are safe. • they don’t rule out success • Consider the cut { visited , unvisited } • A respects this cut. A is the set of edges selected so far. 8 7 B C D 4 9 2 11 4 14 A I E 7 6 8 10 2 H G F 1

  4. Suppose we are partway through Prim • Assume that our choices A so far are safe. • they don’t rule out success • Consider the cut { visited , unvisited } • A respects this cut. • The edge we add next is a light edge . • Least weight of any edge crossing the cut. A is the set of • By the Lemma, edges selected so far. 8 this edge is safe . 7 B C D • it also doesn’t 4 9 2 rule out 11 4 success. 14 A I E 7 6 8 10 2 H G F add this one next 1

  5. Hooray! • Our greedy choices don’t rule out success . • This is enough (along with an argument by induction) to guarantee correctness of Prim’s algorithm.

  6. This is what we needed • Inductive hypothesis: • After adding the t’th edge, there exists an MST with the edges added so far. • Base case: • After adding the 0’th edge, there exists an MST with the edges added so far. YEP. • Inductive step: • If the inductive hypothesis holds for t (aka, the choices so far are safe), then it holds for t+1 (aka, the next edge we add is safe). • That’s what we just showed. • Conclusion: • After adding the n-1’st edge, there exists an MST with the edges added so far. • At this point we have a spanning tree, so it better be minimal.

  7. Two questions 1. Does it work? • That is, does it actually return a MST? • Yes! 2. How do we actually implement this? • the pseudocode above says “slowPrim”…

  8. How do we actually implement this? • Each vertex keeps: • the distance from itself to the growing spanning tree if you can get there in one edge. • how to get there . I’m 7 away. C is the closest. 8 7 B C D 9 4 2 4 11 14 A I E 6 7 8 10 I can’t get to the 1 2 tree in one edge H G F

  9. How do we actually implement this? • Each vertex keeps: • the distance from itself to the growing spanning tree if you can get there in one edge. • how to get there . • Choose the closest vertex, add it. I’m 7 away. C is the closest. 8 7 B C D 9 4 2 4 11 14 A I E 6 7 8 10 I can’t get to the 1 2 tree in one edge H G F

  10. How do we actually implement this? • Each vertex keeps: • the distance from itself to the growing spanning tree if you can get there in one edge. • how to get there . • Choose the closest vertex, add it. I’m 7 away. C is the closest. 8 7 B C D 9 4 2 4 11 14 A I E 6 7 8 10 I can’t get to the 1 2 tree in one edge H G F

  11. How do we actually implement this? • Each vertex keeps: • the distance from itself to the growing spanning tree if you can get there in one edge. • how to get there . • Choose the closest vertex, add it. • Update the stored info. I’m 7 away. C is the closest. 8 7 B C D 9 4 2 4 11 14 A I E 6 7 8 10 I’m 10 away. 1 2 F is the closest. H G F

  12. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x k[x] from the growing tree p[b] = a, meaning that a b a was the vertex that k[b] comes from. ∞ ∞ ∞ 7 8 B C D 9 4 2 ∞ 4 11 14 A I E ∞ 0 6 7 8 10 1 2 H G F ∞ ∞ ∞

  13. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x Activate the unreached vertex u with the smallest key. • k[x] from the growing tree p[b] = a, meaning that a b a was the vertex that k[b] comes from. ∞ ∞ ∞ 7 8 B C D 9 4 2 ∞ 4 11 14 A I E ∞ 0 6 7 8 10 1 2 H G F ∞ ∞ ∞

  14. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x Activate the unreached vertex u with the smallest key. • k[x] from the growing tree for each of u’s neighbors v: • k[v] = min( k[v], weight(u,v) ) • p[b] = a, meaning that a b if k[v] updated, p[v] = u • a was the vertex that k[b] comes from. ∞ 4 ∞ 7 8 B C D 9 4 2 ∞ 4 11 14 A I E ∞ 0 6 7 8 10 1 2 H G F ∞ 8 ∞

  15. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x Activate the unreached vertex u with the smallest key. • k[x] from the growing tree for each of u’s neighbors v: • k[v] = min( k[v], weight(u,v) ) • p[b] = a, meaning that a b if k[v] updated, p[v] = u • a was the vertex that Mark u as reached, and add (p[u],u) to MST. • k[b] comes from. ∞ 4 ∞ 7 8 B C D 9 4 2 ∞ 4 11 14 A I E ∞ 0 6 7 8 10 1 2 H G F ∞ 8 ∞

  16. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x Activate the unreached vertex u with the smallest key. • k[x] from the growing tree for each of u’s neighbors v: • k[v] = min( k[v], weight(u,v) ) • p[b] = a, meaning that a b if k[v] updated, p[v] = u • a was the vertex that Mark u as reached, and add (p[u],u) to MST. • k[b] comes from. ∞ 4 ∞ 7 8 B C D 9 4 2 ∞ 4 11 14 A I E ∞ 0 6 7 8 10 1 2 H G F ∞ 8 ∞

  17. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x Activate the unreached vertex u with the smallest key. • k[x] from the growing tree for each of u’s neighbors v: • k[v] = min( k[v], weight(u,v) ) • p[b] = a, meaning that a b if k[v] updated, p[v] = u • a was the vertex that Mark u as reached, and add (p[u],u) to MST. • k[b] comes from. 8 4 ∞ 7 8 B C D 9 4 2 ∞ 4 11 14 A I E ∞ 0 6 7 8 10 1 2 H G F ∞ 8 ∞

  18. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x Activate the unreached vertex u with the smallest key. • k[x] from the growing tree for each of u’s neighbors v: • k[v] = min( k[v], weight(u,v) ) • p[b] = a, meaning that a b if k[v] updated, p[v] = u • a was the vertex that Mark u as reached, and add (p[u],u) to MST. • k[b] comes from. 8 4 ∞ 7 8 B C D 9 4 2 ∞ 4 11 14 A I E ∞ 0 6 7 8 10 1 2 H G F ∞ 8 ∞

  19. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x Activate the unreached vertex u with the smallest key. • k[x] from the growing tree for each of u’s neighbors v: • k[v] = min( k[v], weight(u,v) ) • p[b] = a, meaning that a b if k[v] updated, p[v] = u • a was the vertex that Mark u as reached, and add (p[u],u) to MST. • k[b] comes from. 8 4 ∞ 7 8 B C D 9 4 2 ∞ 4 11 14 A I E ∞ 0 6 7 8 10 1 2 H G F ∞ 8 ∞

  20. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x Activate the unreached vertex u with the smallest key. • k[x] from the growing tree for each of u’s neighbors v: • k[v] = min( k[v], weight(u,v) ) • p[b] = a, meaning that a b if k[v] updated, p[v] = u • a was the vertex that Mark u as reached, and add (p[u],u) to MST. • k[b] comes from. 8 4 7 7 8 B C D 9 4 2 ∞ 4 11 14 A I E 2 0 6 7 8 10 1 2 H G F 4 8 ∞

  21. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x Activate the unreached vertex u with the smallest key. • k[x] from the growing tree for each of u’s neighbors v: • k[v] = min( k[v], weight(u,v) ) • p[b] = a, meaning that a b if k[v] updated, p[v] = u • a was the vertex that Mark u as reached, and add (p[u],u) to MST. • k[b] comes from. 8 4 7 7 8 B C D 9 4 2 ∞ 4 11 14 A I E 2 0 6 7 8 10 1 2 H G F 4 8 ∞

  22. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x Activate the unreached vertex u with the smallest key. • k[x] from the growing tree for each of u’s neighbors v: • k[v] = min( k[v], weight(u,v) ) • p[b] = a, meaning that a b if k[v] updated, p[v] = u • a was the vertex that Mark u as reached, and add (p[u],u) to MST. • k[b] comes from. 8 4 7 7 8 B C D 9 4 2 ∞ 4 11 14 A I E 2 0 6 7 8 10 1 2 H G F 4 8 ∞

  23. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x Activate the unreached vertex u with the smallest key. • k[x] from the growing tree for each of u’s neighbors v: • k[v] = min( k[v], weight(u,v) ) • p[b] = a, meaning that a b if k[v] updated, p[v] = u • a was the vertex that Mark u as reached, and add (p[u],u) to MST. • k[b] comes from. 8 4 7 7 8 B C D 9 4 2 ∞ 4 11 14 A I E 2 0 6 7 8 10 1 2 H G F 4 7 6

  24. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x Activate the unreached vertex u with the smallest key. • k[x] from the growing tree for each of u’s neighbors v: • k[v] = min( k[v], weight(u,v) ) • p[b] = a, meaning that a b if k[v] updated, p[v] = u • a was the vertex that Mark u as reached, and add (p[u],u) to MST. • k[b] comes from. 8 4 7 7 8 B C D 9 4 2 ∞ 4 11 14 A I E 2 0 6 7 8 10 1 2 H G F 4 7 6

  25. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x Activate the unreached vertex u with the smallest key. • k[x] from the growing tree for each of u’s neighbors v: • k[v] = min( k[v], weight(u,v) ) • p[b] = a, meaning that a b if k[v] updated, p[v] = u • a was the vertex that Mark u as reached, and add (p[u],u) to MST. • k[b] comes from. 8 4 7 7 8 B C D 9 4 2 ∞ 4 11 14 A I E 2 0 6 7 8 10 1 2 H G F 4 7 6

  26. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x Activate the unreached vertex u with the smallest key. • k[x] from the growing tree for each of u’s neighbors v: • k[v] = min( k[v], weight(u,v) ) • p[b] = a, meaning that a b if k[v] updated, p[v] = u • a was the vertex that Mark u as reached, and add (p[u],u) to MST. • k[b] comes from. 8 4 7 7 8 B C D 9 4 2 10 4 11 14 A I E 2 0 6 7 8 10 1 2 H G F 4 7 2

  27. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x Activate the unreached vertex u with the smallest key. • k[x] from the growing tree for each of u’s neighbors v: • k[v] = min( k[v], weight(u,v) ) • p[b] = a, meaning that a b if k[v] updated, p[v] = u • a was the vertex that Mark u as reached, and add (p[u],u) to MST. • k[b] comes from. 8 4 7 7 8 B C D 9 4 2 10 4 11 14 A I E 2 0 6 7 8 10 1 2 H G F 4 7 2

  28. Efficient implementation x Can’t reach x yet x is “active” x Every vertex has a key and a parent Can reach x x Until all the vertices are reached: k[x] is the distance of x Activate the unreached vertex u with the smallest key. • k[x] from the growing tree for each of u’s neighbors v: • k[v] = min( k[v], weight(u,v) ) • p[b] = a, meaning that a b if k[v] updated, p[v] = u • a was the vertex that Mark u as reached, and add (p[u],u) to MST. • k[b] comes from. 8 4 7 7 8 B C D 9 4 2 10 4 11 14 A I E 2 0 6 7 8 10 etc. 1 2 H G F 4 7 2

  29. This should look pretty familiar • Very similar to Dijkstra’s algorithm! • Differences: 1. Keep track of p[v] in order to return a tree at the end • But Dijkstra’s can do that too, that’s not a big difference. 2. Instead of d[v] which we update by • d[v] = min( d[v], d[u] + w(u,v) ) we keep k[v] which we update by • k[v] = min( k[v], w(u,v) ) • To see the difference, consider: U 2 2 S T 3

  30. One thing that is similar: Running time • Exactly the same as Dijkstra: • O(mlog(n)) using a Red-Black tree as a priority queue. • O(m + nlog(n)) if we use a Fibonacci Heap*. *See CS166

  31. Two questions 1. Does it work? • That is, does it actually return a MST? • Yes! 2. How do we actually implement this? • the pseudocode above says “slowPrim”… • Implement it basically the same way we’d implement Dijkstra!

  32. What have we learned? • Prim’s algorithm greedily grows a tree • smells a lot like Dijkstra’s algorithm • It finds a Minimum Spanning Tree in time O(mlog(n)) • if we implement it with a Red-Black Tree • To prove it worked, we followed the same recipe for greedy algorithms we saw last time. • Show that, at every step, we don’t rule out success.

  33. That’s not the only greedy algorithm what if we just always take the cheapest edge? whether or not it’s connected to what we have so far? 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  34. That’s not the only greedy algorithm what if we just always take the cheapest edge? whether or not it’s connected to what we have so far? 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  35. That’s not the only greedy algorithm what if we just always take the cheapest edge? whether or not it’s connected to what we have so far? 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  36. That’s not the only greedy algorithm what if we just always take the cheapest edge? whether or not it’s connected to what we have so far? 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  37. That’s not the only greedy algorithm what if we just always take the cheapest edge? whether or not it’s connected to what we have so far? 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  38. That’s not the only greedy algorithm what if we just always take the cheapest edge? whether or not it’s connected to what we have so far? 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  39. That’s not the only greedy algorithm what if we just always take the cheapest edge? whether or not it’s connected to what we have so far? That won’t cause a cycle 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  40. That’s not the only greedy algorithm what if we just always take the cheapest edge? whether or not it’s connected to what we have so far? That won’t cause a cycle 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  41. That’s not the only greedy algorithm what if we just always take the cheapest edge? whether or not it’s connected to what we have so far? That won’t cause a cycle 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  42. That’s not the only greedy algorithm what if we just always take the cheapest edge? whether or not it’s connected to what we have so far? That won’t cause a cycle 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  43. That’s not the only greedy algorithm what if we just always take the cheapest edge? whether or not it’s connected to what we have so far? That won’t cause a cycle 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  44. We’ve discovered Kruskal’s algorithm! • slowKruskal (G = (V,E)): • Sort the edges in E by non-decreasing weight. • MST = {} m iterations through this loop • for e in E (in sorted order): • if adding e to MST won’t cause a cycle: • add e to MST. How do we check this? • return MST How would you Naively, the running time is ???: figure out if added e For each of m iterations of the for loop: • would make a cycle Check if adding e would cause a cycle… • in this algorithm?

  45. Two questions 1. Does it work? • That is, does it actually return a MST? Let’s do this 2. How do we actually implement this? one first • the pseudocode above says “slowKruskal”…

  46. At each step of Kruskal’s, A forest is a collection of we are maintaining a forest. disjoint trees 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  47. At each step of Kruskal’s, A forest is a collection of we are maintaining a forest. disjoint trees 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  48. At each step of Kruskal’s, A forest is a collection of we are maintaining a forest. disjoint trees When we add an edge, we merge two trees: 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  49. At each step of Kruskal’s, A forest is a collection of we are maintaining a forest. disjoint trees When we add an edge, we merge two trees: 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  50. At each step of Kruskal’s, A forest is a collection of we are maintaining a forest. disjoint trees When we add an edge, we merge two trees: 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F We never add an edge within a tree since that would create a cycle.

  51. Keep the trees in a special data structure “treehouse”?

  52. Union-find data structure also called disjoint-set data structure • Used for storing collections of sets • Supports: • makeSet(u): create a set {u} • find(u): return the set that u is in • union(u,v): merge the set that u is in with the set that v is in. x makeSet(x) y makeSet(y) makeSet(z) union(x,y) z

  53. Union-find data structure also called disjoint-set data structure • Used for storing collections of sets • Supports: • makeSet(u): create a set {u} • find(u): return the set that u is in • union(u,v): merge the set that u is in with the set that v is in. x y makeSet(x) makeSet(y) makeSet(z) union(x,y) z

  54. Union-find data structure also called disjoint-set data structure • Used for storing collections of sets • Supports: • makeSet(u): create a set {u} • find(u): return the set that u is in • union(u,v): merge the set that u is in with the set that v is in. x y makeSet(x) makeSet(y) makeSet(z) union(x,y) z find(x)

  55. Kruskal pseudo-code • kruskal (G = (V,E)): • Sort E by weight in non-decreasing order • MST = {} // initialize an empty tree • for v in V: • makeSet (v) // put each vertex in its own tree in the forest • for (u,v) in E: // go through the edges in sorted order • if find (u) != find (v): // if u and v are not in the same tree • add (u,v) to MST • union (u,v) // merge u’s tree with v’s tree • return MST

  56. Once more… To start, every vertex is in it’s own tree. 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  57. Once more… Then start merging. 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  58. Once more… Then start merging. 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  59. Once more… Then start merging. 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  60. Once more… Then start merging. 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  61. Once more… Then start merging. 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

  62. Once more… Then start merging. 7 8 B C D 9 4 2 4 11 14 A I E 6 7 8 10 1 2 H G F

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