Professional Documents
Culture Documents
Correctness of Kruskal's Algorithm: Operations
Correctness of Kruskal's Algorithm: Operations
Correctness of Kruskal's Algorithm: Operations
ADS: lect 12 – slide 1 – 5th Nov, 2010 ADS: lect 12 – slide 3 – 5th Nov, 2010
ADS: lect 12 – slide 2 – 5th Nov, 2010 ADS: lect 12 – slide 4 – 5th Nov, 2010
Implementation of Kruskal’s Algorithm Amortized Analysis
Want to analyse . . .
Algorithm Kruskal(G, W )
1. F ← 0 Given a sequence of m “Disjoint Set” operations
2. for all vertices v of G do (Make-Set, Union, Find-Set), bound the total running
3. Make-Set(v ) time to perform a sequence of m operations.
4. sort edges of G into non-decreasing order by weight
I Usually parametrize in terms of n (no. of Make-Set operations),
5. for all edges (u, v ) of G in non-decreasing order by weight do
not just m.
6. if Find-Set(u) 6= Find-Set(v ) then
7. F ← F ∪ {(u, v )} I (Of course) the bound we get will depend on the particular data
8. Union(u, v ) structure/implementation we design for this problem.
9. return F
ADS: lect 12 – slide 5 – 5th Nov, 2010 ADS: lect 12 – slide 7 – 5th Nov, 2010
ADS: lect 12 – slide 6 – 5th Nov, 2010 ADS: lect 12 – slide 8 – 5th Nov, 2010
Example
Example Example (cont’d)
Example (cont’d)
Linked list representation of
Linked list representation of
{ a, f }, { b }, { g, c, e }, {d} :
a f
a f
b b
g c e g c e
d
d
The representatives are a, b, g and d.
The representatives are a, b, g and d. U NION (g, b)
A&DS Lecture 11 9 Mary Cryan
ADS: lect 12 – slide 9 – 5th Nov, 2010 ADS: lect 12 – slide 11 – 5th Nov, 2010
A&DS Lecture 11 11 Mary Cryan
ADS: lect 12 – slide 10 – 5th Nov, 2010 ADS: lect 12 – slide 12 – 5th Nov, 2010
A nasty example The Forest Implementation of Disjoint-Sets
The Forest Implementation of Disjoint-Sets
Take: n = dm/2e + 1, q = m − n = bm/2c − 1. Each set represented by a rooted tree:
Elements: x1 , x2 , . . . , xn .
Each set represented by a rooted tree:
Operation Number of objects updated
Make-Set(x1 ) 1
Make-Set(x2 ) 1 i g b
. .
. .
Make-Set(xn ) 1
Union(x1 , x2 ) 1 f h e a
Union(x2 , x3 ) 2
Union(x3 , x4 ) 3
. .
. .
c d
Union(xq−1 , xq ) q−1
Total Θ(m2 )
O(m + n lg n)
time.
Crucial Proof Idea: Each element can appear at most lg n times in the shorter
list of a Union. (because if x is in the shortest list, the union operation doubles
the size of x’s list).
ADS: lect 12 – slide 14 – 5th Nov, 2010 ADS: lect 12 – slide 16 – 5th Nov, 2010
Improving the Running Time
f b g d
General Strategy
rank [f] = 0 rank [d] = 0
Keep trees low.
a e c
U NION (f, d) rank [g] = 1 rank [d] = 1
Two Heuristics
1. Union-by-Rank: Attach lower tree to the root of b g d
higher tree in Union
2. Path Compression: Update tree during Find-Set a e c f
operations. U NION (f, g) rank [g] = 2
b g
a d e c
f
A&DS Lecture 11 19 Mary Cryan
ADS: lect 12 – slide 17 – 5th Nov, 2010 ADS: lect 12 – slide 19 – 5th Nov, 2010
d f
F IND -S ET (a)
ADS: lect 12 – slide 21 – 5th Nov, 2010 ADS: lect 12 – slide 23 – 5th Nov, 2010
ADS: lect 12 – slide 22 – 5th Nov, 2010 ADS: lect 12 – slide 24 – 5th Nov, 2010
Small table of A(i, j) Reading
[CLRS] Chapter 21 or [CLR] Chapter 22
j =1 j =2 j =3 j =4 [CLRS] Chapter 23 This is Chapter 24 of [CLR].
i =1 21 22 23 24
2
2 22 22
i =2 22 22 22 22 Chapters and Exercises have same labels in both editions of [CLRS]
·2 ·2 ·2 ·2 ·2 ·2
·· ·· ·· ·· ·· ··
2 16 2 2 16 2 2 2 16
22
i =3 2 2 2 2
Historical importance of A(i, j): Showed that the class of primitive recursive
functions does not include all computable functions.
A(i, j) grows faster than any primitive recursive function.
ADS: lect 12 – slide 25 – 5th Nov, 2010 ADS: lect 12 – slide 27 – 5th Nov, 2010
ADS: lect 12 – slide 26 – 5th Nov, 2010 ADS: lect 12 – slide 28 – 5th Nov, 2010