Professional Documents
Culture Documents
2
Given a set {1, 2, , n} of n elements. Initially each element is in a different set.
{1}, {2}, , {n}
An intermixed sequence of union and find operations is performed. A union operation combines two sets into one.
Each of the n elements is in exactly one set at any time.
Uses of Union/Find
Task scheduling.
A factory has a single machine that is to perform n tasks. Task i has an integer release time ri and an integer deadline di. The completion of each task requires one unit of time on this machine. A feasible schedule is an assignment of tasks to time slots on the machine such tat task I is assigned to a time slot between its release time and deadline and no slot has more than one task assigned to it.
Uses of Union/Find
Task scheduling.
Consider the following four tasks:
Task Release time Deadline A 0 4 B 0 4 C 1 2 D 2 3
Uses of Union/Find
Task scheduling.
The following task-to-slot assignment is a feasible schedule: 0
A
1
C
2
D
3
B
A 0 4
B 0 4
C 1 2
D 2 3
Uses of Union/Find
Task scheduling.
Intuitive algorithm: 1. Sort the tasks into nonincreasing order of release time.
2. Consider the tasks in this nonincreasing order. For each task determine the free slot nearest to, but not after, its deadline. If this free slot is before the tasks release time, fail. Otherwise, assign the task to this slot.
Uses of Union/Find
Task scheduling. Can use Union/Find for part 2
Let d denote the latest deadline of any task Usable time slots are j-1 to j where 1<=j <= d Refer to these slots as slots 1 through d For any slot a, define near(a) as the largest j such that j <= a and slot j is free. If no such j exists, then near(a)=near(0)=0 Two slots a and b are in the same equivalence class iff near(a) = near(b)
Uses of Union/Find
Task scheduling. Can use Union/Find for part 2
Beginning: near(a) = a for all slots and each slot is in its own equivalence class. When slot a is assigned a task, near changes for all slots b with near(b) = a. The new value of near is near(a-1) Thus perform a union on the equivalence classes that currently contain slots a and a-1. F Find the equivalence class of a-1 by doing a find(a)
Uses of Union/Find
Task scheduling. Can use Union/Find for part 2
For each equivalence class e must retain in nearest[e] the value of near of its members Get near(a) by doing nearest[find(a)] Assume that the equivalence class name is taken to be whatever the find returns.
0
0 1 1
1
1 2 2
2
2 3 3
3
3
0
0 1 1
1
1 2 1
2 2
3 3
3
3
0
0 1 0
1 1
2 2
2 1 3 3
3 3
0
0 1 0
1 1
2 2
2 1
3 3
3 0
0
0 1 0
1 1
2 2 2
0
3 3
3 0
Task Scheduling
Resulting task schedule.
The following task-to-slot assignment is a feasible schedule: 0
B
1
C
2
D
3
A
A 0 4
B 0 4
C 1 2
D 2 3
A Set As A Tree
S = {2, 4, 5, 9, 11, 13, 30} Some possible tree representations:
4 5 13
11
30
13
13
4
11
4
9 2 11
5
30
2
9 30
4
9 11
5
30
Start at the node that represents element i and climb up the tree until the root is reached. Return the element in the root. To climb the tree, each node must have a parent pointer.
2
1 20 16
10
14 12
Example
13
4
9 2 1 11
5
30
table[]
0 5 10 15
Better Representation
Use an integer array parent[] such that parent[i] is the element that is the parent of element i. 13
4 9 2 1 11 5 30
parent[]
0
2 9
13 13 5
4 10
0 15
Union Operation
union(i,j)
i and j are the roots of two different trees, i != j.
Union Example
7 13 4 9 2 11 5 8 3 22 6
10
30 20 16 14 12
union(7,13)
2
1 20 16
10
14 12
Height Rule
Make tree with smaller height a subtree of the other tree. Break ties arbitrarily.
13 7
4
9 11
5
8 30 10 20 16 14 12 3 22 6
2
1
union(7,13)
Weight Rule
Make tree with fewer number of elements a subtree of the other tree. 7 Break ties arbitrarily.
13 8 3 22 6
4
9 11
5
30 20 16 10 14 12
2
1
union(7,13)
Implementation
Root of each tree must record either its height or the number of elements in the tree. When a union is done using the height rule, the height increases only when two trees of equal height are united. When the weight rule is used, the weight of the new tree is the sum of the weights of the trees that are united.
Height Of A Tree
If we start with single element trees and perform unions using either the height or the weight rule. The height of a tree with p elements is at most floor (log2p) + 1. Proof is by induction on p. See next slide.
Height Of A Tree
Proof is by induction on p.
Trivial for p=1 assume true for all trees with i nodes, i<=p-1. show for trees with p nodes. consider the last union operation union(k,j) used to create tree t which has p nodes. Let m be the number of nodes in tree j and let p-m be the number of nodes in tree k. assume that 1<=m<=p/2. So j is made a subtree of k.
Height Of A Tree
Proof is by induction on p.
assume that 1<=m<=p/2. So j is made a subtree of k. therefore height of t is either same as that of k or is one more that that of j. if former then, since num nodes in k is p-m and by induction, height of t is
log 2 ( p m) 1 log 2 ( p) 1
Height Of A Tree
Proof is by induction on p.
if latter then height of t is by induction
8
4 9 5
22
2
1
e
d
g 11
30 20 16
10 14 12
Path Compaction
Make all nodes on find path point to tree root. find(1)
7
13 8 4 9 2 1 5 3 22 6
g 11
30
10
d
c
20
16
14
12
Path Splitting
Nodes on find path point to former grandparent. find(1)
7
13 8 4 9 2 1 5 3 22 6
g 11
30
10
d
c
20
16
14
12
Path Halving
Parent pointer in every other node on find path is changed to former grandparent. 7 find(1)
13 8 4 9 2 1 5 3 22 6
g 11
30
10
d
c
20
16
14
12
Time Complexity
Ackermanns function.
A(i,j) = 2j, i = 1 and j >= 1 A(i,j) = A(i-1,2), i >= 2 and j = 1 A(i,j) = A(i-1,A(i,j-1)), i, j >= 2
Time Complexity
Ackermanns function grows very rapidly as p and q are increased.
A(2,1) = A(1,2) = 22 = 4 For j >= 2, A(2,j) = A(1,A(2,j-1)) = 2A(2,j-1). So A(2,2) = 2A(2,1) = 24 =16 A(2,3) = 2A(2,2) = 216 =65,536 A(2,4) = 265,536 .
2. 2
A(2,j) = 2 Where there are j + 1 rows of twos. A(2,j) for j > 3 is very large
Time Complexity
Ackermanns function grows very rapidly as p and q are increased.
A(2,4) = 265,536
In the analysis of the union-find problem, q is the number, n, of elements; p = n + f; and u >= n/2. For all practical purposes, alpha(p,q) < 5.
Time Complexity
Theorem 12.2 [Tarjan and Van Leeuwen] Let T(f,u) be the time required to process any intermixed sequence of f finds and u unions. Assume that u >= n/2.
a*(n + f*alpha(f+n, n) <= T(f,u) <= b*(n + f*alpha(f+n, n)
where a and b are constants. These bounds apply when we start with singleton sets and use either the weight or height rule for unions and any one of the path compression methods for a find.