Upcoming SlideShare
×

# Computer notes - Analysis of Union

859 views

Published on

Union is clearly a constant time operation. Running time of find(i) is proportional to the height of the tree containing node i.If unions are done by weight (size), the depth of any element is never greater than log2n.

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
859
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
33
0
Likes
0
Embeds 0
No embeds

No notes for slide
• End of lecture 35, start of lecture 36
• End of lecture 36
• ### Computer notes - Analysis of Union

1. 1. Class No.30 Data Structures http://ecomputernotes.com
2. 2. Running Time Analysis <ul><li>Union is clearly a constant time operation. </li></ul><ul><li>Running time of find ( i ) is proportional to the height of the tree containing node i . </li></ul><ul><li>This can be proportional to n in the worst case (but not always) </li></ul><ul><li>Goal: Modify union to ensure that heights stay small </li></ul>http://ecomputernotes.com
3. 3. Union by Size <ul><li>Maintain sizes (number of nodes) of all trees, and during union. </li></ul><ul><li>Make smaller tree the subtree of the larger one. </li></ul><ul><li>Implementation: for each root node i , instead of setting parent[i] to -1 , set it to -k if tree rooted at i has k nodes. </li></ul><ul><li>This also called union-by-weight . </li></ul>http://ecomputernotes.com
4. 4. Union by Size <ul><li>union(i,j): </li></ul><ul><ul><li>root1 = find(i); </li></ul></ul><ul><ul><li>root2 = find(j); </li></ul></ul><ul><ul><li>if (root1 != root2) </li></ul></ul><ul><ul><ul><li>if (parent[root1] <= parent[root2]) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><ul><li>// first tree has more nodes </li></ul></ul></ul></ul><ul><ul><ul><ul><li>parent[root1] += parent[root2]; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>parent[root2] = root1; </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>else { // second tree has more nodes </li></ul></ul></ul><ul><ul><ul><ul><li>parent[root2] += parent[root1]; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>parent[root1] = root2; </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>http://ecomputernotes.com
5. 5. Union by Size <ul><li>Eight elements, initially in different sets. </li></ul>http://ecomputernotes.com 1 2 3 4 5 6 7 8 -1 -1 -1 -1 -1 -1 -1 -1 1 2 3 4 5 6 7 8
6. 6. Union by Size <ul><li>Union(4,6) </li></ul>http://ecomputernotes.com 1 2 3 4 5 6 7 8 -1 -1 -1 -2 -1 4 -1 -1 1 2 3 4 5 6 7 8
7. 7. Union by Size <ul><li>Union(2,3) </li></ul>http://ecomputernotes.com 1 2 3 4 5 6 7 8 -1 -2 2 -2 -1 4 -1 -1 1 2 3 4 5 6 7 8
8. 8. Union by Size <ul><li>Union(1,4) </li></ul>http://ecomputernotes.com 1 2 3 4 5 6 7 8 4 -2 2 -3 -1 4 -1 -1 1 2 3 4 5 6 7 8
9. 9. Union by Size <ul><li>Union(2,4) </li></ul>http://ecomputernotes.com 1 2 3 4 5 6 7 8 4 4 2 -5 -1 4 -1 -1 1 2 3 4 5 6 7 8
10. 10. Union by Size <ul><li>Union(5,4) </li></ul>http://ecomputernotes.com 1 2 3 4 5 6 7 8 4 4 2 -6 4 4 -1 -1 1 2 3 4 5 6 7 8
11. 11. Analysis of Union by Size <ul><li>If unions are done by weight (size), the depth of any element is never greater than log 2 n . </li></ul>http://ecomputernotes.com
12. 12. Analysis of Union by Size <ul><li>Intuitive Proof: </li></ul><ul><ul><li>Initially, every element is at depth zero. </li></ul></ul><ul><ul><li>When its depth increases as a result of a union operation (it’s in the smaller tree), it is placed in a tree that becomes at least twice as large as before (union of two equal size trees). </li></ul></ul><ul><ul><li>How often can each union be done? -- log 2 n times, because after at most log 2 n unions, the tree will contain all n elements. </li></ul></ul>http://ecomputernotes.com
13. 13. Union by Height <ul><li>Alternative to union-by-size strategy: maintain heights, </li></ul><ul><li>During union, make a tree with smaller height a subtree of the other. </li></ul><ul><li>Details are left as an exercise. </li></ul>http://ecomputernotes.com
14. 14. Sprucing up Find <ul><li>So far we have tried to optimize union . </li></ul><ul><li>Can we optimize find ? </li></ul><ul><li>Yes, using path compression (or compaction). </li></ul>http://ecomputernotes.com
15. 15. Sprucing up Find <ul><li>During find ( i ), as we traverse the path from i to root, update parent entries for all these nodes to the root. </li></ul><ul><li>This reduces the heights of all these nodes. </li></ul><ul><li>Pay now, and reap benefits later! </li></ul><ul><li>Subsequent find may do less work </li></ul>http://ecomputernotes.com
16. 16. Sprucing up Find <ul><li>Updated code for find </li></ul><ul><ul><li>find (i) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><ul><li>if (parent[i] < 0) </li></ul></ul></ul><ul><ul><ul><li>return i; </li></ul></ul></ul><ul><ul><ul><li>else </li></ul></ul></ul><ul><ul><ul><li>return parent[i] = find(parent[i]); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul>http://ecomputernotes.com
17. 17. Path Compression <ul><li>Find(1) </li></ul>12 14 20 10 22 7 3 6 16 4 2 9 30 5 13 11 1 http://ecomputernotes.com 8 31 32 35 13 19 18 17
18. 18. Path Compression <ul><li>Find(1) </li></ul>12 14 20 10 22 7 3 6 16 4 2 9 30 5 13 11 1 http://ecomputernotes.com 8 31 32 35 13 19 18 17
19. 19. Path Compression <ul><li>Find(1) </li></ul>12 14 20 10 22 7 3 6 16 4 2 9 30 5 13 11 1 http://ecomputernotes.com 8 31 32 35 13 19 18 17
20. 20. Path Compression <ul><li>Find(1) </li></ul>12 14 20 10 22 7 3 6 16 4 2 9 30 5 13 11 1 http://ecomputernotes.com 8 31 32 35 13 19 18 17
21. 21. Path Compression <ul><li>Find(1) </li></ul>12 14 20 10 22 7 3 6 16 4 2 9 30 5 13 11 1 http://ecomputernotes.com 8 31 32 35 13 19 18 17
22. 22. Path Compression <ul><li>Find ( a ) </li></ul>http://ecomputernotes.com a b f c d e
23. 23. Path Compression <ul><li>Find ( a ) </li></ul>http://ecomputernotes.com a b f c d e
24. 24. Timing with Optimization <ul><li>Theorem: A sequence of m union and find operations, n of which are find operations, can be performed on a disjoint-set forest with union by rank (weight or height) and path compression in worst case time proportional to ( m  ( n ))  </li></ul><ul><li> ( n )  is the inverse Ackermann’s function which grows extremely slowly. For all practical puposes,  ( n )  4. </li></ul><ul><li>Union-find is essentially proportional to m for a sequence of m operations, linear in m . </li></ul>http://ecomputernotes.com