Union Find | Disjoint Set Union Introductory Problem
Prereq: Depth First Search Review
Now we will start with an introductory problem to get you familiar with the data structure. Complete the class below to support the following two operations:
merge(x, y)
merges the sets that thex
andy
belong to,is_same(x, y)
determines ifx
andy
belong to the same set. If so returntrue
, otherwisefalse
.
Example:
1merge(1, 2)
2merge(2, 3)
3is_same(1, 3) => true
4is_same(2, 4) => false
Explanation:
We merge elements 1
and 2
then we merge the set of 1
and 2
with the element 3
,
so we should have now have 2 sets, [1, 2, 3]
and [4]
.
Therefore 1
and 3
are in the same set, while 2
and 4
are in different sets.
Try it yourself
Solution
Title
Script
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem
Ipsum
has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
Contrary to popular belief, Lorem
Ipsum
is not simply random text.
1 >>> a = [1, 2, 3] 2 >>> a[-1] 3 3