0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# lecture 20

616

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
616
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
26
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. CS 332: Algorithms Single-Source Shortest Path
• 2. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>
• 3. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>14 10 3 6 4 5 2 9 15 8 Run on example graph
• 4. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>        14 10 3 6 4 5 2 9 15 8 Run on example graph
• 5. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>   0     14 10 3 6 4 5 2 9 15 8 Pick a start vertex r r
• 6. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>   0     14 10 3 6 4 5 2 9 15 8 Red vertices have been removed from Q u
• 7. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>   0   3  14 10 3 6 4 5 2 9 15 8 Red arrows indicate parent pointers u
• 8. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>14   0   3  14 10 3 6 4 5 2 9 15 8 u
• 9. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>14   0   3  14 10 3 6 4 5 2 9 15 8 u
• 10. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>14   0 8  3  14 10 3 6 4 5 2 9 15 8 u
• 11. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>10   0 8  3  14 10 3 6 4 5 2 9 15 8 u
• 12. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>10   0 8  3  14 10 3 6 4 5 2 9 15 8 u
• 13. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>10 2  0 8  3  14 10 3 6 4 5 2 9 15 8 u
• 14. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>10 2  0 8 15 3  14 10 3 6 4 5 2 9 15 8 u
• 15. Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>10 2  0 8 15 3  14 10 3 6 4 5 2 9 15 8 u
• 16. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>10 2 9 0 8 15 3  14 10 3 6 4 5 2 9 15 8 u
• 17. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>10 2 9 0 8 15 3 4 14 10 3 6 4 5 2 9 15 8 u
• 18. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>5 2 9 0 8 15 3 4 14 10 3 6 4 5 2 9 15 8 u
• 19. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>5 2 9 0 8 15 3 4 14 10 3 6 4 5 2 9 15 8 u
• 20. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>5 2 9 0 8 15 3 4 14 10 3 6 4 5 2 9 15 8 u
• 21. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>5 2 9 0 8 15 3 4 14 10 3 6 4 5 2 9 15 8 u
• 22. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>5 2 9 0 8 15 3 4 14 10 3 6 4 5 2 9 15 8 u
• 23. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>What is the hidden cost in this code?
• 24. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>DecreaseKey(v, w(u,v)); </li></ul>
• 25. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>DecreaseKey(v, w(u,v)); </li></ul>How often is ExtractMin() called? How often is DecreaseKey() called?
• 26. Review: Prim’s Algorithm <ul><li>MST-Prim(G, w, r) </li></ul><ul><li>Q = V[G]; </li></ul><ul><li>for each u  Q </li></ul><ul><li>key[u] =  ; </li></ul><ul><li>key[r] = 0; </li></ul><ul><li>p[r] = NULL; </li></ul><ul><li>while (Q not empty) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>for each v  Adj[ u ] </li></ul><ul><li>if (v  Q and w( u,v ) < key[ v ]) </li></ul><ul><li>p[v] = u; </li></ul><ul><li>key[v] = w(u,v); </li></ul>What will be the running time? A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)
• 27. Single-Source Shortest Path <ul><li>Problem: given a weighted directed graph G, find the minimum-weight path from a given source vertex s to another vertex v </li></ul><ul><ul><li>“Shortest-path” = minimum weight </li></ul></ul><ul><ul><li>Weight of path is sum of edges </li></ul></ul><ul><ul><li>E.g., a road map: what is the shortest path from Chapel Hill to Charlottesville? </li></ul></ul>
• 28. Shortest Path Properties <ul><li>Again, we have optimal substructure : the shortest path consists of shortest subpaths: </li></ul><ul><ul><li>Proof: suppose some subpath is not a shortest path </li></ul></ul><ul><ul><ul><li>There must then exist a shorter subpath </li></ul></ul></ul><ul><ul><ul><li>Could substitute the shorter subpath for a shorter path </li></ul></ul></ul><ul><ul><ul><li>But then overall path is not shortest path. Contradiction </li></ul></ul></ul>
• 29. Shortest Path Properties <ul><li>Define  (u,v) to be the weight of the shortest path from u to v </li></ul><ul><li>Shortest paths satisfy the triangle inequality :  (u,v)   (u,x) +  (x,v) </li></ul><ul><li>“Proof”: </li></ul>x u v This path is no longer than any other path
• 30. Shortest Path Properties <ul><li>In graphs with negative weight cycles, some shortest paths will not exist ( Why ? ): </li></ul>< 0
• 31. Relaxation <ul><li>A key technique in shortest path algorithms is relaxation </li></ul><ul><ul><li>Idea: for all v , maintain upper bound d[ v ] on  (s,v) </li></ul></ul><ul><ul><li>Relax(u,v,w) { </li></ul></ul><ul><ul><li>if (d[v] > d[u]+w) then d[v]=d[u]+w; </li></ul></ul><ul><ul><li>} </li></ul></ul>9 5 2 7 5 2 Relax 6 5 2 6 5 2 Relax
• 32. Bellman-Ford Algorithm <ul><li>BellmanFord() </li></ul><ul><li>for each v  V </li></ul><ul><li>d[v] =  ; </li></ul><ul><li>d[s] = 0; </li></ul><ul><li>for i=1 to |V|-1 </li></ul><ul><li>for each edge (u,v)  E </li></ul><ul><li>Relax(u,v, w(u,v)); </li></ul><ul><li>for each edge (u,v)  E </li></ul><ul><li>if (d[v] > d[u] + w(u,v)) </li></ul><ul><li>return “no solution”; </li></ul><ul><li>Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w </li></ul>Initialize d[], which will converge to shortest-path value  Relaxation: Make |V|-1 passes, relaxing each edge Test for solution Under what condition do we get a solution?
• 33. Bellman-Ford Algorithm <ul><li>BellmanFord() </li></ul><ul><li>for each v  V </li></ul><ul><li>d[v] =  ; </li></ul><ul><li>d[s] = 0; </li></ul><ul><li>for i=1 to |V|-1 </li></ul><ul><li>for each edge (u,v)  E </li></ul><ul><li>Relax(u,v, w(u,v)); </li></ul><ul><li>for each edge (u,v)  E </li></ul><ul><li>if (d[v] > d[u] + w(u,v)) </li></ul><ul><li>return “no solution”; </li></ul><ul><li>Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w </li></ul>What will be the running time?
• 34. Bellman-Ford Algorithm <ul><li>BellmanFord() </li></ul><ul><li>for each v  V </li></ul><ul><li>d[v] =  ; </li></ul><ul><li>d[s] = 0; </li></ul><ul><li>for i=1 to |V|-1 </li></ul><ul><li>for each edge (u,v)  E </li></ul><ul><li>Relax(u,v, w(u,v)); </li></ul><ul><li>for each edge (u,v)  E </li></ul><ul><li>if (d[v] > d[u] + w(u,v)) </li></ul><ul><li>return “no solution”; </li></ul><ul><li>Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w </li></ul>What will be the running time? A: O(VE)
• 35. Bellman-Ford Algorithm <ul><li>BellmanFord() </li></ul><ul><li>for each v  V </li></ul><ul><li>d[v] =  ; </li></ul><ul><li>d[s] = 0; </li></ul><ul><li>for i=1 to |V|-1 </li></ul><ul><li>for each edge (u,v)  E </li></ul><ul><li>Relax(u,v, w(u,v)); </li></ul><ul><li>for each edge (u,v)  E </li></ul><ul><li>if (d[v] > d[u] + w(u,v)) </li></ul><ul><li>return “no solution”; </li></ul><ul><li>Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w </li></ul>B E D C A -1 2 2 1 -3 5 3 4 Ex: work on board s
• 36. Bellman-Ford <ul><li>Note that order in which edges are processed affects how quickly it converges </li></ul><ul><li>Correctness: show d[v] =  (s,v) after |V|-1 passes </li></ul><ul><ul><li>Lemma: d[v]   (s,v) always </li></ul></ul><ul><ul><ul><li>Initially true </li></ul></ul></ul><ul><ul><ul><li>Let v be first vertex for which d[v] <  (s,v) </li></ul></ul></ul><ul><ul><ul><li>Let u be the vertex that caused d[v] to change: d[v] = d[u] + w(u,v) </li></ul></ul></ul><ul><ul><ul><li>Then d[v] <  (s,v)  (s,v)   (s,u) + w(u,v) ( Why? )  (s,u) + w(u,v)  d[u] + w(u,v) ( Why? ) </li></ul></ul></ul><ul><ul><ul><li>So d[v] < d[u] + w(u,v). Contradiction. </li></ul></ul></ul>
• 37. Bellman-Ford <ul><li>Prove: after |V|-1 passes, all d values correct </li></ul><ul><ul><li>Consider shortest path from s to v: s  v 1  v 2  v 3  v 4  v </li></ul></ul><ul><ul><ul><li>Initially, d[s] = 0 is correct, and doesn’t change ( Why? ) </li></ul></ul></ul><ul><ul><ul><li>After 1 pass through edges, d[v 1 ] is correct ( Why? ) and doesn’t change </li></ul></ul></ul><ul><ul><ul><li>After 2 passes, d[v 2 ] is correct and doesn’t change </li></ul></ul></ul><ul><ul><ul><li>… </li></ul></ul></ul><ul><ul><ul><li>Terminates in |V| - 1 passes: ( Why? ) </li></ul></ul></ul><ul><ul><ul><li>What if it doesn’t? </li></ul></ul></ul>
• 38. DAG Shortest Paths <ul><li>Problem: finding shortest paths in DAG </li></ul><ul><ul><li>Bellman-Ford takes O(VE) time. </li></ul></ul><ul><ul><li>How can we do better? </li></ul></ul><ul><ul><li>Idea: use topological sort </li></ul></ul><ul><ul><ul><li>If were lucky and processes vertices on each shortest path from left to right, would be done in one pass </li></ul></ul></ul><ul><ul><ul><li>Every path in a dag is subsequence of topologically sorted vertex order, so processing verts in that order, we will do each path in forward order (will never relax edges out of vert before doing all edges into vert). </li></ul></ul></ul><ul><ul><ul><li>Thus: just one pass. What will be the running time? </li></ul></ul></ul>
• 39. Dijkstra’s Algorithm <ul><li>If no negative edge weights, we can beat BF </li></ul><ul><li>Similar to breadth-first search </li></ul><ul><ul><li>Grow a tree gradually, advancing from vertices taken from a queue </li></ul></ul><ul><li>Also similar to Prim’s algorithm for MST </li></ul><ul><ul><li>Use a priority queue keyed on d[v] </li></ul></ul>
• 40. Dijkstra’s Algorithm <ul><li>Dijkstra(G) </li></ul><ul><li>for each v  V </li></ul><ul><li>d[v] =  ; </li></ul><ul><li>d[s] = 0; S =  ; Q = V; </li></ul><ul><li>while (Q   ) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>S = S U {u}; </li></ul><ul><li>for each v  u->Adj[] </li></ul><ul><li>if (d[v] > d[u]+w(u,v)) </li></ul><ul><li>d[v] = d[u]+w(u,v); </li></ul>Relaxation Step Note: this is really a call to Q->DecreaseKey() B C D A 10 4 3 2 1 5 Ex: run the algorithm
• 41. Dijkstra’s Algorithm <ul><li>Dijkstra(G) </li></ul><ul><li>for each v  V </li></ul><ul><li>d[v] =  ; </li></ul><ul><li>d[s] = 0; S =  ; Q = V; </li></ul><ul><li>while (Q   ) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>S = S U {u}; </li></ul><ul><li>for each v  u->Adj[] </li></ul><ul><li>if (d[v] > d[u]+w(u,v)) </li></ul><ul><li>d[v] = d[u]+w(u,v); </li></ul>How many times is ExtractMin() called? How many times is DecraseKey() called? What will be the total running time?
• 42. Dijkstra’s Algorithm <ul><li>Dijkstra(G) </li></ul><ul><li>for each v  V </li></ul><ul><li>d[v] =  ; </li></ul><ul><li>d[s] = 0; S =  ; Q = V; </li></ul><ul><li>while (Q   ) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>S = S U {u}; </li></ul><ul><li>for each v  u->Adj[] </li></ul><ul><li>if (d[v] > d[u]+w(u,v)) </li></ul><ul><li>d[v] = d[u]+w(u,v); </li></ul>How many times is ExtractMin() called? How many times is DecraseKey() called? A: O(E lg V) using binary heap for Q Can acheive O(V lg V + E) with Fibonacci heaps
• 43. Dijkstra’s Algorithm <ul><li>Dijkstra(G) </li></ul><ul><li>for each v  V </li></ul><ul><li>d[v] =  ; </li></ul><ul><li>d[s] = 0; S =  ; Q = V; </li></ul><ul><li>while (Q   ) </li></ul><ul><li>u = ExtractMin(Q); </li></ul><ul><li>S = S U {u}; </li></ul><ul><li>for each v  u->Adj[] </li></ul><ul><li>if (d[v] > d[u]+w(u,v)) </li></ul><ul><li>d[v] = d[u]+w(u,v); </li></ul>Correctness: we must show that when u is removed from Q, it has already converged
• 44. Correctness Of Dijkstra's Algorithm <ul><li>Note that d[v]   (s,v)  v </li></ul><ul><li>Let u be first vertex picked s.t.  shorter path than d[u]  d[u] >  (s,u) </li></ul><ul><li>Let y be first vertex  V-S on actual shortest path from s  u  d[y] =  (s,y) </li></ul><ul><ul><li>Because d[x] is set correctly for y's predecessor x  S on the shortest path, and </li></ul></ul><ul><ul><li>When we put x into S, we relaxed (x,y), giving d[y] the correct value </li></ul></ul>s x y u p 2 p 2
• 45. Correctness Of Dijkstra's Algorithm <ul><li>Note that d[v]   (s,v)  v </li></ul><ul><li>Let u be first vertex picked s.t.  shorter path than d[u]  d[u] >  (s,u) </li></ul><ul><li>Let y be first vertex  V-S on actual shortest path from s  u  d[y] =  (s,y) </li></ul><ul><li>d[u] >  (s,u) =  (s,y) +  (y,u) ( Why? ) = d[y] +  (y,u)  d[y] But if d[u] > d[y], wouldn't have chosen u. Contradiction. </li></ul>s x y u p 2 p 2
• 46. Disjoint-Set Union Problem <ul><li>Want a data structure to support disjoint sets </li></ul><ul><ul><li>Collection of disjoint sets S = {S i }, S i ∩ S j =  </li></ul></ul><ul><li>Need to support following operations: </li></ul><ul><ul><li>MakeSet(x): S = S U {{x}} </li></ul></ul><ul><ul><li>Union(S i , S j ): S = S - {S i , S j } U {S i U S j } </li></ul></ul><ul><ul><li>FindSet(X): return S i  S such that x  S i </li></ul></ul><ul><li>Before discussing implementation details, we look at example application: MSTs </li></ul>
• 47. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>
• 48. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
• 49. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
• 50. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
• 51. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1? 5 13 17 25 14 8 21 Run the algorithm:
• 52. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
• 53. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2? 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
• 54. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
• 55. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5? 13 17 25 14 8 21 Run the algorithm:
• 56. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
• 57. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17 25 14 8? 21 Run the algorithm:
• 58. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
• 59. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9? 1 5 13 17 25 14 8 21 Run the algorithm:
• 60. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
• 61. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13? 17 25 14 8 21 Run the algorithm:
• 62. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
• 63. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17 25 14? 8 21 Run the algorithm:
• 64. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
• 65. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17? 25 14 8 21 Run the algorithm:
• 66. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19? 9 1 5 13 17 25 14 8 21 Run the algorithm:
• 67. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17 25 14 8 21? Run the algorithm:
• 68. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17 25? 14 8 21 Run the algorithm:
• 69. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
• 70. Kruskal’s Algorithm <ul><li>Kruskal() </li></ul><ul><li>{ </li></ul><ul><li>T =  ; </li></ul><ul><li>for each v  V </li></ul><ul><li>MakeSet(v); </li></ul><ul><li>sort E by increasing edge weight w </li></ul><ul><li>for each (u,v)  E (in sorted order) </li></ul><ul><li>if FindSet(u)  FindSet(v) </li></ul><ul><li>T = T U {{u,v}}; </li></ul><ul><li>Union(FindSet(u), FindSet(v)); </li></ul><ul><li>} </li></ul>2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
• 71. Correctness Of Kruskal’s Algorithm <ul><li>Sketch of a proof that this algorithm produces an MST for T : </li></ul><ul><ul><li>Assume algorithm is wrong: result is not an MST </li></ul></ul><ul><ul><li>Then algorithm adds a wrong edge at some point </li></ul></ul><ul><ul><li>If it adds a wrong edge, there must be a lower weight edge (cut and paste argument) </li></ul></ul><ul><ul><li>But algorithm chooses lowest weight edge at each step. Contradiction </li></ul></ul><ul><li>Again, important to be comfortable with cut and paste arguments </li></ul>
• 72. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } What will affect the running time?
• 73. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } What will affect the running time? 1 Sort O(V) MakeSet() calls O(E) FindSet() calls O(V) Union() calls ( Exactly how many Union()s? )
• 74. Kruskal’s Algorithm: Running Time <ul><li>To summarize: </li></ul><ul><ul><li>Sort edges: O(E lg E) </li></ul></ul><ul><ul><li>O(V) MakeSet()’s </li></ul></ul><ul><ul><li>O(E) FindSet()’s </li></ul></ul><ul><ul><li>O(V) Union()’s </li></ul></ul><ul><li>Upshot: </li></ul><ul><ul><li>Best disjoint-set union algorithm makes above 3 operations take O(E  (E,V)),  almost constant </li></ul></ul><ul><ul><li>Overall thus O(E lg E), almost linear w/o sorting </li></ul></ul>
• 75. Disjoint Set Union <ul><li>So how do we implement disjoint-set union? </li></ul><ul><ul><li>Naïve implementation: use a linked list to represent each set: </li></ul></ul><ul><ul><ul><li>MakeSet(): ??? time </li></ul></ul></ul><ul><ul><ul><li>FindSet(): ??? time </li></ul></ul></ul><ul><ul><ul><li>Union(A,B): “copy” elements of A into B: ??? time </li></ul></ul></ul>
• 76. Disjoint Set Union <ul><li>So how do we implement disjoint-set union? </li></ul><ul><ul><li>Naïve implementation: use a linked list to represent each set: </li></ul></ul><ul><ul><ul><li>MakeSet(): O(1) time </li></ul></ul></ul><ul><ul><ul><li>FindSet(): O(1) time </li></ul></ul></ul><ul><ul><ul><li>Union(A,B): “copy” elements of A into B: O(A) time </li></ul></ul></ul><ul><ul><li>How long can a single Union() take? </li></ul></ul><ul><ul><li>How long will n Union()’s take? </li></ul></ul>
• 77. Disjoint Set Union: Analysis <ul><li>Worst-case analysis: O(n 2 ) time for n Union’s </li></ul><ul><ul><ul><li>Union(S 1 , S 2 ) “copy” 1 element </li></ul></ul></ul><ul><ul><ul><li>Union(S 2 , S 3 ) “copy” 2 elements </li></ul></ul></ul><ul><ul><ul><li>… </li></ul></ul></ul><ul><ul><ul><li>Union(S n-1 , S n ) “copy” n-1 elements </li></ul></ul></ul><ul><ul><ul><li>O(n 2 ) </li></ul></ul></ul><ul><li>Improvement: always copy smaller into larger </li></ul><ul><ul><li>Why will this make things better? </li></ul></ul><ul><ul><li>What is the worst-case time of Union()? </li></ul></ul><ul><li>But now n Union’s take only O(n lg n) time! </li></ul>
• 78. Amortized Analysis of Disjoint Sets <ul><li>Amortized analysis computes average times without using probability </li></ul><ul><li>With our new Union(), any individual element is copied at most lg n times when forming the complete set from 1-element sets </li></ul><ul><ul><li>Worst case: Each time copied, element in smaller set </li></ul></ul><ul><ul><ul><li>1st time resulting set size  2 </li></ul></ul></ul><ul><ul><ul><li>2nd time  4 </li></ul></ul></ul><ul><ul><ul><li>… </li></ul></ul></ul><ul><ul><ul><li>(lg n)th time  n </li></ul></ul></ul>
• 79. Amortized Analysis of Disjoint Sets <ul><li>Since we have n elements each copied at most lg n times, n Union()’s takes O(n lg n) time </li></ul><ul><li>We say that each Union() takes O(lg n) amortized time </li></ul><ul><ul><li>Financial term: imagine paying \$(lg n) per Union </li></ul></ul><ul><ul><li>At first we are overpaying; initial Union \$O(1) </li></ul></ul><ul><ul><li>But we accumulate enough \$ in bank to pay for later expensive O(n) operation. </li></ul></ul><ul><ul><li>Important: amount in bank never goes negative </li></ul></ul>
• 80. The End