COT 5405 Fall 09 Homework 2 Solution
1. (10pts, 1 page)
Prove that every node has rank at most
⌊
(
lg n
)
⌋
in unionFind algorithm
with two heuristics (1) union by rank and (2) path compression.
Sol:
By Induction:
Claim:
A node with rank r, then it is the root of a subtree of size at least 2
r
.
Base case:
A node of rank 0 is the root of a subtree that contains at least itself (and
so is of size = at least 1).
Inductive case:
A node X can have rank (
r
+ 1) only if,at some previous stage, it had
a rank
r
and it was the root of a tree that was joined with another tree
whose root had rank
r
. Then X became the root of the union of the two
trees. Each tree, by inductive hypothesis is of size at least 2
r
, and so now
X is the root of a tree of size at least 2
r
+ 2
r
= 2
(
r
+1)
.
Now the number of nodes in the forest is
n
and we have at least 2
r
nodes
in every tree with rank
r
.So,
n
≥
2
r
⇒
r
≤ ⌊
(
lg n
)
⌋
2. (15pts, 1/2 page per part)
Consider the following code segment:
for
i
= 1 to
n
makeset(
i
)
for
i
= 1 to
n
−
1
union(find(
i
), find(
i
+ 1))
for
i
= 1 to
n
find(
i
)
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 UNGOR
 Algorithms, Priority queue

Click to edit the document details