# Java Program for Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)

• Last Updated : 22 Dec, 2021

A disjoint-set data structure is a data structure that keeps track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets. A union-find algorithm is an algorithm that performs two useful operations on such a data structure:

Find: Determine which subset a particular element is in. This can be used for determining if two elements are in the same subset.

Union: Join two subsets into a single subset.

In this post, we will discuss the application of Disjoint Set Data Structure. The application is to check whether a given graph contains a cycle or not.

Union-Find Algorithm can be used to check whether an undirected graph contains cycle or not. Note that we have discussed an algorithm to detect cycle. This is another method based on Union-Find. This method assumes that the graph doesn\’t contain any self-loops.
We can keep track of the subsets in a 1D array, let\’s call it parent[].

Let us consider the following graph: For each edge, make subsets using both the vertices of the edge. If both the vertices are in the same subset, a cycle is found.

Initially, all slots of parent array are initialized to -1 (means there is only one item in every subset).

```0   1   2
-1 -1  -1 ```

Now process all edges one by one.

Edge 0-1: Find the subsets in which vertices 0 and 1 are. Since they are in different subsets, we take the union of them. For taking the union, either make node 0 as parent of node 1 or vice-versa.

```0   1   2    <----- 1 is made parent of 0 (1 is now representative of subset {0, 1})
1  -1  -1```

Edge 1-2: 1 is in subset 1 and 2 is in subset 2. So, take union.

```0   1   2    <----- 2 is made parent of 1 (2 is now representative of subset {0, 1, 2})
1   2  -1```

Edge 0-2: 0 is in subset 2 and 2 is also in subset 2. Hence, including this edge forms a cycle.

How subset of 0 is same as 2?
0->1->2 // 1 is parent of 0 and 2 is parent of 1

## Java

 `// Java Program for union-find algorithm to detect cycle in a graph``import` `java.util.*;``import` `java.lang.*;``import` `java.io.*;`` ` `class` `Graph``{``    ``int` `V, E;    ``// V-> no. of vertices & E->no.of edges``    ``Edge edge[]; ``// /collection of all edges`` ` `    ``class` `Edge``    ``{``        ``int` `src, dest;``    ``};`` ` `    ``// Creates a graph with V vertices and E edges``    ``Graph(``int` `v,``int` `e)``    ``{``        ``V = v;``        ``E = e;``        ``edge = ``new` `Edge[E];``        ``for` `(``int` `i=``0``; i

Please refer complete article on Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph) for more details!

My Personal Notes arrow_drop_up