CS2040 Tutorial 06 Slides 1
CS2040 Tutorial 06 Slides 1
Representative
1 4 5 7
2 6
3
Union-Find Disjoint Sets (UFDS)
Find
1 4 5 7
2 6
3
Union-Find Disjoint Sets (UFDS)
Find
1 4 5
7
2 6
3
Union-Find Disjoint Sets (UFDS)
Merge
1 4 5 7
2 6
3
Union-Find Disjoint Sets (UFDS)
Merge
1 4 5
7
2 6
3
Union-Find Disjoint Sets (UFDS)
Merge
1 5 7
4
2 6
3
Union-Find Disjoint Sets (UFDS)
After a series of merges, a set might be very long, such that it looks like a chain. This is not
good as find operations may now run in O(n) for a set of n elements.
1 2 3 4 5
Union-Find Disjoint Sets (UFDS)
We can try to shorten the chains by linking elements directly to their representative during a find,
so that future finds will be faster.
1 2 3 4 5
Union-Find Disjoint Sets (UFDS)
We can try to shorten the chains by linking elements directly to their representative during a find,
so that future finds will be faster. e.g. find(4)
1 2 3 4 5
Union-Find Disjoint Sets (UFDS)
We can try to shorten the chains by linking elements directly to their representative during a find,
so that future finds will be faster. e.g. find(4)
1 2 3 4 5
Union-Find Disjoint Sets (UFDS)
We can try to shorten the chains by linking elements directly to their representative during a find,
so that future finds will be faster. e.g. find(4)
1 2 3 4 5
Union-Find Disjoint Sets (UFDS)
We can try to shorten the chains by linking elements directly to their representative during a find,
so that future finds will be faster. e.g. find(4)
1 2 3 4 5
Union-Find Disjoint Sets (UFDS)
We can try to shorten the chains by linking elements directly to their representative during a find,
so that future finds will be faster. e.g. find(4)
1 2 3 4 5
Union-Find Disjoint Sets (UFDS)
0 0 0 0
1 2 3 4
Union-Find Disjoint Sets (UFDS)
When merging two representatives of the same rank, merge in either direction, and increase
the rank of the new representative by 1.
0 0 0 0
1 2 3 4
Union-Find Disjoint Sets (UFDS)
When merging two representatives of the same rank, merge in either direction, and increase
the rank of the new representative by 1.
0 1 0 0
1 2 3 4
Union-Find Disjoint Sets (UFDS)
When merging two representatives of the different ranks, merge the one with the smaller rank to
that with the greater rank. Rank of new representative is unchanged.
0 1 0 0
1 2 3 4
Union-Find Disjoint Sets (UFDS)
When merging two representatives of the different ranks, merge the one with the smaller rank to
that with the greater rank. Rank of new representative is unchanged.
0 1 0 0
1 2 3 4
Union-Find Disjoint Sets (UFDS)
If we are using union by rank only (without path compression), notice that the rank actually
represents the height of tree rooted at the representative.
Union-Find Disjoint Sets (UFDS)
If we are using union by rank only (without path compression), notice that the rank actually
represents the height of tree rooted at the representative.
If we are using union by rank only (without path compression), notice that the rank actually
represents the height of tree rooted at the representative.
Since we increase the rank only when merging trees of equal rank, notice that every time the rank
of a representative increases, the number of elements in the tree doubles.
Union-Find Disjoint Sets (UFDS)
If we are using union by rank only (without path compression), notice that the rank actually
represents the height of tree rooted at the representative.
Since we increase the rank only when merging trees of equal rank, notice that every time the rank
of a representative increases, the number of elements in the tree doubles.
The maximum attainable rank would thus be bounded by the number of time we can double the
size of a tree, which would be O(log n) given n elements.
Union-Find Disjoint Sets (UFDS)
Thus the maximum height of a tree would be O(log n) given a tree with n elements. As such, if
we use union by rank only, both find and merge will run in O(log n).
Problem 2: Intergalactic Wars
Given n warlords and n worlds, where initially, each warlord has one unique world under its
domain, support the following two types of queries in sub O(log n) time:
Given n warlords and n worlds, where initially, each warlord has one unique world under its
domain, support the following two types of queries in sub O(log n) time:
The first type of query looks like a union operation, and the second type of query looks like a
findset operation. This hints us towards a UFDS solution.
Problem 2: Intergalactic Wars
Add an additional parameter to every set: the warlord that is in charge of the worlds in the set.
When we merge two sets of worlds together, ensure that the representative of the merged
set is updated with the warlord in charge of the worlds in the entire set.
The second type of queries can then be answered by first doing a findset to find the representative
of the set, then checking the warlord in charge of all the worlds in the set.
Problem 2: Intergalactic Wars
1 4 5 3
2 6
Problem 2: Intergalactic Wars
1 4 5 3
2 6
Problem 2: Intergalactic Wars
Merge the two sets together using the merge operation in UFDS.
Warlord 2 Warlord 4 Warlord 5
Warlord 3
1 4 5
3
2 6
Problem 2: Intergalactic Wars
Warlord 5
1 4 5 3
2 6
Problem 2: Intergalactic Wars
To check if a world is under a certain warlord, simply find the representative of the set that the world
is in, and check the warlord that rules the worlds in that set.
1 4 5 3
2 6
Problem 2: Intergalactic Wars
Note that since merges are performed, after every merge, the number of different sets of worlds
decreases by 1. After n - 1 merges, no more merges can be performed, since all worlds will be
under one warlord.
Thus, we can simply check if n - 1 merges have been performed, and then finding the warlord in
the representative of the whole set of worlds.
Problem 3: Number Candies
e.g. If you have the following sweets, the longest unbroken sequence has length 4.
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Observation: When a new candy is bought, if it’s next to a candy that was bought before, it
joins the unbroken sequence / group of candies.
e.g. Buys candy 5
one group another group
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Observation: When a new candy is bought, if it’s next to a candy that was bought before, it
joins the unbroken sequence / group of candies.
e.g. Buys candy 5 ⇒ 5 is next to 4 ⇒ 5 joins the group of candies with 4
one group another group
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Observation: When a new candy is bought, if it’s next to a candy that was bought before, it
joins the unbroken sequence / group of candies.
Joining a group is like the ‘merge’ operation in UFDS!
one group another group
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Observation: What is the longest unbroken sequence of candies that you have starting
from candy 1?
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Observation: What is the longest unbroken sequence of candies that you have starting from
candy 1? The number of candies in the same group as candy 1. Augment the
UFDS to maintain the size of each set.
one group. size = 5 another group. size = 2
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Example
GotCandy?
UFDS
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Example
Buy(5).
GotCandy?
UFDS
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Example
GotCandy?
UFDS
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Example
Buy(1).
GotCandy?
UFDS
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Example
GotCandy?
UFDS
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Example
Buy(3).
GotCandy?
UFDS
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Example
GotCandy?
UFDS
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Example
Buy(2).
GotCandy?
UFDS
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Example
GotCandy?
UFDS
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Example
Buy(6).
GotCandy?
UFDS
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Example
GotCandy?
UFDS
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Example
Buy(4).
GotCandy?
UFDS
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Example
GotCandy?
UFDS
1 2 3 4 5 6 7 8 9 10
Problem 3: Number Candies
Each merge can be done in O(α(n)) time. Checking the size of the set of 1 takes O(α (n)) time.
Overall, each query can be answered in O(α(n)) time.
GotCandy?
UFDS
1 2 3 4 5 6 7 8 9 10
Problem 4: Funfair Ride
There is a circular ring of n seats (seat 1 is next to seat 2 and n − 1, seat 2 is next to seat 1 and
3, and so on). m children will arrive one by one.
Each child has a desired seat. For each child, do the following:
● If the desired seat is not taken, assign that seat to the child.
● If the desired seat is taken, ‘linear probe’ for the next empty seat, and assigned that seat to the
child.
Seats
1 2 3 4 5 6 7 8 9 10
Problem 4: Funfair Ride
Seats
1 2 3 4 5 6 7 8 9 10
Problem 4: Funfair Ride
Seats
1 2 3 4 5 6 7 8 9 10
Problem 4: Funfair Ride
Seats
1 2 3 4 5 6 7 8 9 10
Problem 4: Funfair Ride
Seats
1 2 3 4 5 6 7 8 9 10
Problem 4: Funfair Ride
● If a new seat is assigned and adjacent seats are occupied, merge the two groups of seats
together. Merge operation in UFDS.
Seats
1 2 3 4 5 6 7 8 9 10
Problem 4: Funfair Ride
● If a new seat is assigned and adjacent seats are occupied, merge the two groups of seats
together. Merge operation in UFDS.
Seats
1 2 3 4 5 6 7 8 9 10
Problem 4: Funfair Ride
Seats
1 2 3 4 5 6 7 8 9 10
Problem 4: Funfair Ride
Seats
1 2 3 4 5 6 7 8 9 10
Problem 4: Funfair Ride
Seats
1 2 3 4 5 6 7 8 9 10
Problem 4: Funfair Ride
1 2 3 4 5 6 7 8 9 10
Seats
Problem 4: Funfair Ride
Seats
1 2 3 4 5 6 7 8 9 10
Problem 4: Funfair Ride
function merge(x, y)
x = find(x)
y = find(y)
if rank[x] <
rank[y]
parent[x] = y
else
parent[y] = x
if rank[x] == rank[y]
rank[x] ← rank[x] + 1
Problem 4: Funfair Ride
function merge(x, y)
x = find(x)
y = find(y)
if rank[x] <
rank[y]
parent[x] = y
max[y] =
max(max[x],
max[y])
else if rank[x] == rank[y]
parent[y] = x
rank[x] ← rank[x] + 1
max[x] =
max(max[x],
max[y])
Thank you.