# Can't write and search sets? Take a look at this article that implements and searches sets in Java

Posted by SBukoski on Sun, 23 Jan 2022 02:49:55 +0100

Joint search is to merge the contents that are not originally in a set into a set.
It is not very useful in the actual scene.

Except that you need to query in several sets at the same time, avoid querying many times, so as to query together.

The following is a simple implementation example. Let's give an example to illustrate what is a joint query set and what problems are solved by the joint query set.

#### Code parsing

```package com.chaojilaji.book.andcheck;

public class AndCheckSet {

public static Integer getFather(int[] father, int u) {
if (father[u] != u) {
father[u] = getFather(father, father[u]);
}
return father[u];
}

public static void join(int[] father,int x, int y) {
int fx = getFather(father,x);
int fy = getFather(father,y);
if (fx != fy){
father[fx] = fy;
}
}

public static void main(String[] args) {
int n = 10;
int[] a = new int[n];
for (int i = 0;i<n;i++){
a[i] = i; // Initialization defines one hundred sets
}
for (int i=0;i<n;i++){
System.out.println(i+" "+getFather(a, i)); // For each collection, the parent node is its own
}
}
}
```

First, we define two functions, getFather and join, respectively, to obtain the root of the set where u is located and merge the two sets.

Let's take a look at the getFather method first

```public static Integer getFather(int[] father, int u) {
if (father[u] != u) {
father[u] = getFather(father, father[u]);
}
return father[u];
}
```

Is to find out the root node of the set where the value u is located. Generally speaking, if father[u] is equal to u itself, it means that the node where u is located is the root node, and this algorithm does not exit until it is equal, that is, the father of each node from u to the root node is directly set as the root node, and the root node of the current node is returned.

Then let's look at the join method

```public static void join(int[] father,int x, int y) {
int fx = getFather(father,x);
int fy = getFather(father,y);
if (fx != fy){
father[fx] = fy;
}
}
```

Find out the root node of the set where the x and y nodes are located respectively. If the root nodes are different, set the father node of one node as the other node, so as to merge into a set.

#### Code application

```public static void main(String[] args) {
int n = 10;
int[] a = new int[n];
for (int i = 0;i<n;i++){
a[i] = i; // Initialize and define n sets
}
for (int i=0;i<n;i++){
System.out.println(i+" "+getFather(a, i)); // For each collection, the parent node is its own
}
System.out.println("-------------------------");
join(a,0,1); // Merge 0 and 1
for (int i=0;i<n;i++){
System.out.println(i+" "+getFather(a, i));
}
// Because 0 and 1 are merged, the set becomes 9, and the roots of node 0 and node 1 are node 1
System.out.println("-------------------------");
join(a,2,3); // Merge 2 and 3
for (int i=0;i<n;i++){
System.out.println(i+" "+getFather(a, i));
}
// Since 2 and 3 are merged, the set becomes 8, and the roots of node 2 and node 3 are node 3
System.out.println("-------------------------");
join(a,1,3); // Merge 1 and 3
for (int i=0;i<n;i++){
System.out.println(i+" "+getFather(a, i));
}
// Since 1 and 3 are merged, the set becomes 7, and the root of nodes 0, 1, 2 and 3 is node 3
}
```

Firstly, we define N sets whose values are 0~n-1, and then their parent nodes are equal to themselves, so these are n independent sets. The results are as follows

The parent node of 0 is 0
The parent node of 1 is 1
The parent node of 2 is 2
The parent node of 3 is 3
The parent node of 4 is 4
The parent node of 5 is 5
The parent node of 6 is 6
The parent node of 7 is 7
The parent node of 8 is 8
The parent node of 9 is 9

Then call join(a,0,1) to merge the 0 set and the 1 set, and then output the parent set of the node.

The parent node of 0 is 1
The parent node of 1 is 1
The parent node of 2 is 2
The parent node of 3 is 3
The parent node of 4 is 4
The parent node of 5 is 5
The parent node of 6 is 6
The parent node of 7 is 7
The parent node of 8 is 8
The parent node of 9 is 9

It can be seen that since 0 and 1 are merged, the set becomes 9, and the roots of node 0 and node 1 are node 1.
Then call join(a,2,3) to merge the 2 set and the 3 set, and output the parent set condition of the node.

The parent node of 0 is 1
The parent node of 1 is 1
The parent node of 2 is 3
The parent node of 3 is 3
The parent node of 4 is 4
The parent node of 5 is 5
The parent node of 6 is 6
The parent node of 7 is 7
The parent node of 8 is 8
The parent node of 9 is 9

It can be seen that since 2 and 3 are merged, the set becomes 8, and the roots of node 2 and node 3 are node 3.
Finally, we call join(a,1,3) to merge set 1 and set 3. The parent set of the output node is

The parent node of 0 is 3
The parent node of 1 is 3
The parent node of 2 is 3
The parent node of 3 is 3
The parent node of 4 is 4
The parent node of 5 is 5
The parent node of 6 is 6
The parent node of 7 is 7
The parent node of 8 is 8
The parent node of 9 is 9

It can be seen that since 1 and 3 are merged, the set becomes 7, and the roots of nodes 0, 1, 2 and 3 are node 3.

#### practical application

At the level of code, and search set is well implemented. However, we can also find that its application scenario seems to be very limited.
First, we need to define an array with father[x] = x, and then we merge it. It seems hard to think of application scenarios.

Then we can imagine a scenario. Now there is a network topology, which contains n and network facilities and equipment. Then we give you the connection relationship between these n facilities and equipment, and ask how many local Unicom networks you have in total.
For this problem, we can first define that each device is connected to itself, and then take a join operation on the two devices every time an edge appears. Finally, after traversing all nodes, we get how many different father s, that is, how many different local Unicom networks.

Such problems can also be extended to our interpersonal circle. First of all, everyone is a separate collection, which produces connectivity in the process of constantly knowing people. Finally, let you confirm how many non interconnected interpersonal circles there are.

So you will find that this is essentially finding the number of connected blocks in graph theory.

Topics: Java data structure Union Find