Computer Notes - Data Structures - 30
-
Upload
ecomputernotes -
Category
Documents
-
view
221 -
download
0
Transcript of Computer Notes - Data Structures - 30
-
8/3/2019 Computer Notes - Data Structures - 30
1/24
Class No.30
Data Structures
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
2/24
Running Time Analysis
Unionis clearly a constant time operation.
Running time of find(i) is proportional tothe height of the tree containing node i.
This can be proportional to n in the worstcase (but not always)
Goal: Modify unionto ensure that heightsstay small
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
3/24
Unionby Size
Maintain sizes (number of nodes) of alltrees, and during union.
Make smaller tree the subtree of the largerone.
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.
This also called union-by-weight.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
4/24
Unionby Size
union(i,j):
root1 = find(i);root2 = find(j);
if (root1 != root2)
if (parent[root1]
-
8/3/2019 Computer Notes - Data Structures - 30
5/24
Unionby Size
Eight elements, initially in different sets.
1 2 3 4 5 6 7 8
-1 -1 -1 -1 -1 -1 -1 -1
1 2 3 4 5 6 7 8
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
6/24
Unionby Size
Union(4,6)
1 2 3 4 5
6
7 8
-1 -1 -1 -2 -1 4 -1 -1
1 2 3 4 5 6 7 8
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
7/24
Unionby Size
Union(2,3)
1 2
3
4 5
6
7 8
-1 -2 2 -2 -1 4 -1 -1
1 2 3 4 5 6 7 8
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
8/24
-
8/3/2019 Computer Notes - Data Structures - 30
9/24
Unionby Size
Union(2,4)
12
3
4 5
6
7 8
4 4 2 -5 -1 4 -1 -1
1 2 3 4 5 6 7 8
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
10/24
Unionby Size
Union(5,4)
12
3
4
56
7 8
4 4 2 -6 4 4 -1 -1
1 2 3 4 5 6 7 8
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
11/24
Analysis of Unionby Size
If unions are done by weight (size), the depth ofany element is never greater than log2n.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
12/24
Analysis of Unionby Size
Intuitive Proof: Initially, every element is at depth zero.
When its depth increases as a result of a
union operation (its in the smaller tree), it isplaced in a tree that becomes at least twiceas large as before (union of two equal sizetrees).
How often can each union be done? -- log2ntimes, because after at most log2nunions,the tree will contain all nelements.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
13/24
Unionby Height
Alternative to union-by-size strategy:maintain heights,
During union, make a tree with smaller
height a subtree of the other.
Details are left as an exercise.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
14/24
Sprucing up Find
So far we have tried to optimize union.
Can we optimize find?
Yes, using path compression(orcompaction).
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
15/24
Sprucing up Find
During find(i), as we traverse the path fromito root, update parent entries for all thesenodes to the root.
This reduces the heights of all thesenodes.
Pay now, and reap benefits later!
Subsequent findmay do less work
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
16/24
Sprucing up Find
Updated code for find
find (i)
{if (parent[i] < 0)
return i;
else
return parent[i] = find(parent[i]);
}
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
17/24
Path Compression
Find(1)
121420
10
22
7
8 3 6
16
4
2
930
5
13
11
1
31 32
35
13
191817
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
18/24
Path Compression
Find(1)
121420
10
22
7
8 3 6
16
4
2
930
5
13
11
1
31 32
35
13
191817
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
19/24
Path Compression
Find(1)
121420
10
22
7
8 3 6
16
4
2
930
5
13
11
1
31 32
35
13
191817
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
20/24
Path Compression
Find(1)
121420
10
22
7
8 3 6
16
4
2
930
5
13
11
1
31 32
35
13
191817
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
21/24
Path Compression
Find(1)
121420
10
22
7
8 3 6
16
4
2
930
5
13
11
1
31 32
35
13
191817
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
22/24
Path Compression
Find(a)
a
b
f
c
d
e
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
23/24
Path Compression
Find(a)
a b
f
c d e
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 30
24/24
Timing with Optimization
Theorem: A sequence of m unionand findoperations, nof which are findoperations, canbe performed on a disjoint-set forest with union
by rank (weight or height) and path compressionin worst case time proportional to (m(n)).
(n)is the inverse Ackermanns function whichgrows extremely slowly. For all practical
puposes,(n) 4. Union-find is essentially proportional to mfor a
sequence of moperations, linear in m.
http://ecomputernotes com
http://ecomputernotes.com/http://ecomputernotes.com/