Disjoint Union-Find (Merge-Find) (CHAPTER 21)
Equivalence relations of Fortran.
S={A,B,C,D,E,F,G}
Equivalence (A,E), (B,F), (C,D), (A,D) - {A,C,D,E} {B,F} {G}
1
2
3
FIND(A) - 1
FIND(B) - 2
UNION (A,B) - {A,B,C,D,E,F} {G}
1
2
For Kruskal MinSpanTree - Mix of operations - INITIALize - 1 time
FIND - O E O n 2 times
UNION - n-1 times
Approach 1
FIND - (1)
UNION - (n)
sequence of n UNIONs - (n2)
Approach 2
To merge COMPi into COMPj, visit (k) vertices of COMPi & make them j
Worst-case: at merge k, |COMPj|=1 & |COMPi|=k.
n n 1
UNION - (k) & sequence of n UNION s k
n 2
2
1 k n
Approach 3 Keep track of sizes of components & always merge smaller into larger
Each vertex changes names (belongs to smaller component, at least doubles in size) at
most lgn times.
n-1 UNION s - (n*lg n)
Approach 4 In Approach 1-3, when COMP1 & COMP2 merged, why use the initial
table?
LN6‐Amortization
Page1
FIND(D) - A
FIND(B) - B
UNION (D,B) -
UNION - O(1)
FIND - worst-case - O(n)
UNION (A,B), UNION (B,C), UNION (C,D),...
n FINDs - (n2)
Approach 5 Max path length only increases if longer tree merges into shorter tree.
Thus, keep track of max path length & only merge shorter into longer tree.
n FINDs - (n*lg n)
Approach 6 (Path Compression) If we want shorter trees & we've just traversed a long
path (in FIND),
then compress the path (let each v on path point directly @ root of tree).
LN6‐Amortization
Page2
n FINDs - (n*(n))
(n) grows slower than you can imagine
(1)=0
(2)= (3)=1
(4)=...= (15)=2
(16)=...= (65,535)=3
(65,536)=...= (265,536-1)=4
Ackermann's Function A(x,y)
A(0,y)=1, y0
A(1,0)=2
A(x,0)=x+2, x2
A(x,y)=A(A(x-1,y),y-1), x,y1
Plugging-in: A(x,1)=2x
A(x,2)=2x
A(x,3)=22... (tower of x 2s)
n min
n A x, x
x
Returning to Kruskal:
while SIZE(B)<n-1 do begin
(v,w) EXTRACTMIN(Q);
if FIND(v) FIND(w) then begin
UNION (v,w)
T T {(v,w)}
end
end
Analysis: We perform loop O( E ) times. The EXTRACTMINs require (lg E ) time
each. Total time for EXTRACTMINs: O( E *lg E ). The FINDs and UNIONs take
O( E *( V )). Algorithm takes O( E *lg E ).
LN6‐Amortization
Page3
PRIM'S ALGORITHM (1957): At is always a tree, with edges Bt, V At independent set of
vertices.
At v1
Bt
CREATE(MIN)PRIORITYQUEUE(Q)
for each v V v1
INSERT(Q,v) with priority w v1v and closest[v] v1
while NOTEMPTY(Q) do
y EXTRACTMIN(Q)
Bt Bt
y, closest y
for each u in Q do {
if w yu priority[u] then do
closest u y
DECREASE-KEY(Q,u, w yu )
ANALYSIS: Data Structure for Q: priority, closest and InQ are each arrays of length n .
n 2 Optimal for dense graph. Why?
CHAPTER 19 (BINOMIAL)/FIBONACCI HEAPS
Suppose we want to form UNION of 2 (min)-heaps
5
4
/ \
/ \
8 12 6 22
What kind of tree do you get when, given 2 similar (shaped) trees, 1 becomes child of
other?
Def: Binomial tree B0
B1 B2 … (stored with LEFTCHILD-RIGHTSIBLING
pointers)
o
o
o
| / |
o o o
|
o
Bk+1 = o (root of Bk)
/ |
o (root of Bk)
How many nodes in each tree?
•B0 - 1, B1 - 2B0=2, Bk - 2Bk-1 = 2k
•Bk has height k.
k
•Bk has nodes at depth i.
i
Def: A binomial (min)-heap is a set of heap ordered binomial trees (every node all of
its children) of different sizes, with roots linked.
Note: A binomial heap of n elements has ≤ lg n 1 binomial trees. (Note analogy with
binary representation of n).
LN6‐Amortization
Page4
FIND-MINIMUM: Examine each of ≤ lg n 1roots. O(lgn) or O(1)
MAKE-HEAP(x) - return binomial heap w/ x - (1)
(eager)-UNION: UNION(B4,B7) B4-B7 UNION(B4,B4) B5 UNION(B4-B7,B7) B4-B8
UNION(B5-B6,B5) B6-B7 Analysis: O(lgn) time.
INSERT(H,x) - UNION(H,MAKE-HEAP(x)) = complexity of UNION But how often do we
need lgn?
Amortized Analysis CHAPTER 17 Doesn’t affect code, just analysis.
For each INSERT, we pay $2, $1 for the actual INSERT and $1 -> account of the new tree.
If the INSERTion causes 2 Bi’s to be joined, payed with $1 from one of their accounts, &
the other $1 stays in the account of the new Bi+1. So INSERT cost is $2 O(1) amortized
time.
O(1)
EXTRACT-MIN- FIND-MINIMUM
Remove the root (exposing O(lgn) new roots)
O(lgn)
O(lgn)
make new binomial heap with UNION of exposed trees
(lazy)-UNION-Assume children of root are linked. Don’t eagerly link Bis until necessary,
i.e. next EXTRACT-MIN
however: keep pointer to MIN root, in order to support FIND-MINIMUM
start with m binomial trees & k links are performed during EXTRACT-MIN
Use array of pointers to Bi of new updated heap.
• can use O(k+lgn) - k credits from links & lgn from EXTRACT-MIN
• time spent O(m+k+lgn)
m+k+lgn =2k+(m-k) +lgn ≤2k+2lgn=O(k+lgn)
^ end up with m-k trees, all of distinct ranks so ≤lgn of them
So, in amortized time, only EXTRACT-MIN costs time in O(lgn).
O(1)
MAKEHEAP –
O(1)
FINDMIN –
amortized O(1)
INSERT –
O(lgn)
EXTRACTMIN –
O(lgn) eager
UNION –
O(1) lazy
CHAPTER 19 FIBONACCI HEAPS
Want to support DECREASE-KEY and DELETE. Can do DECREASE-KEY in O(1) if new
priority still parent's. If < parent's priority or if doing DELETE, why not just remove
node & then UNION it into heap? Because log of EXTRACTMIN assumes size of Bk
exponential in k; don't want scraggly trees. Fibonacci Tree is Binomial tree such that
every node loses 1 child.
Solution- Charge $4 for DECREASE-KEY.
If DECREASE-KEY( ) < parent
· remove from tree (for $1)
· put tree rooted at into list (with $1)
· give $2 – parent of . (So roots have $1 & nodes missing a child have $2.)
If node missing child loses another child (has $4), then
·cut tie of to parent (for $1)
·put in list of trees (give it $1)
·give $2 to 's parent.
ANALYSIS: Can we still do EXTRACTMIN (and DELETE) in O(lgn)?
LN6‐Amortization
Page5
Let f(i) – min # nodes in Fibonacci tree with root of degree i. Start with Bi, each
internal node loses 1 child, & root loses 0 children.
F0
F1
F2
F3
F4
F5
o
o
o
o
o
o
| / |
/ | \
/ / | \
/ / | \ \
o o o o o o
o o o o o o o o o
|
| /\
| /\ | \ \
o
ooo
o o oo o o
|
o
f 0 1, f 1 2, f 2 3,... f i f i 1 f i 1 for i 2
1 5
, 1.61803...
2
************************************************************************
Implementation of Priority Queue
operation
Binary Heap
Binomal Heap
Fibonacci Heap
MAKEHEAP
1
1
1
f i i ,
INSERT
EXTRACTMIN
UNION
DECREASE-KEY
DELETE
O lg n
(lazy, amortized) 1
O lg n
n
O lg n
(lazy, amortized) 1
(lazy, amortized) 1
O lg n
(lazy, amortized) 1
O lg n
O lg n
1
O lg n
O lg n
O lg n
Return to PRIM'S ALGORITHM – Keep Q in Fibonacci queue - O m n lg n .
BORUVKA'S ALGORITHM: (Otakar Borůvka. 1926)
for each vi V do i vi ,
repeat
for each i do {color blue the shortest edge from i
merge the trees with endpoints on this edge}
until there is only 1 tree
ANALYSIS: Algorithm makes O lg n passes. So O m lg n
LN6‐Amortization
Page6
© Copyright 2026 Paperzz