0%

Union Find

Typically, Union-find use Tree data structure and provide two methods,

• Union: combine two sets into one
• Find: Determine which data set that an element belongs to.

Example in life: Friend Circle

Imaging that we have n students in school. At the beginning, they don’t know each other.

When the time goes by, circle of friends start to form and grow. If a and b are friends, b and c are friend, then we say that a and c becomes indirect friends, and a, b, c are in the same friend circle.

We can use Union function to establish connections between two students, and use find function to determine if student i and student j are in the same friend circle.

Basic Idea

we can use Tree data structure to store the root (group ID) information of a
node. In the above example, each student is in a group of themselves at the beginning.

we define an array group[], each value denote as group ID. (Obviously, at the beginning, group ID are index of themselves)

Union

Union(x,y)

The idea of union is that, we unite their root. (which means that, we find roots of x and y respectively, and let one root point to another root)

Find

Find(x,y)

the function is quite simple, just check if they have the same root (ID).

Find Root

How do we know that the value is the Group ID?

When root(i) == i, we are sure that id(i) is the root and i is the group ID.

Problem

We’ve known that we use tree to store the parent information so that we can traverse and find the root.

but if the tree is very deep, it may take long time to operate root function.

The worst case for finding root (group ID) is O(N)

Improvement

To avoid this problem, one possible solution is Path Compression

Just after computing the root of i, set the id of each examined node to root(i).

Sample Question: Friend Circle

There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. And we defined a friend circle is a group of students who are direct or indirect friends.

Given a N*N matrix M representing the friend relationship between students in the class. If M[i][j] = 1, then the ith and jth students are direct friends with each other, otherwise not. And you have to output the total number of friend circles among all the students.

Example 1:

Output: 2

Explanation:The 0th and 1st students are direct friends, so they are in a friend circle.
The 2nd student himself is in a friend circle. So return 2.

Example 2:

Output: 1

Explanation:The 0th and 1st students are direct friends, the 1st and 2nd students are direct friends,
so the 0th and 2nd students are indirect friends. All of them are in the same friend circle, so return 1.

Reference Algorithm-Union-Find