Detect Cycle in Graph using DSU
Last Updated :
23 Jul, 2025
Given an undirected graph, the task is to check if the graph contains a cycle or not, using DSU.
Examples:
Input: The following is the graph

Output: Yes
Explanation: There is a cycle of vertices {0, 1, 2}.
We already have discussed an algorithm to detect cycle in directed graph. Here Union-Find Algorithm can be used to check whether an undirected graph contains cycle or not. The idea is that,
Initially create subsets containing only a single node which are the parent of itself. Now while traversing through the edges, if the two end nodes of the edge belongs to the same set then they form a cycle. Otherwise, perform union to merge the subsets together.
Note: This method assumes that the graph doesn't contain any self-loops.
Illustration:
Follow the below illustration for a better understanding
Let us consider the following graph:

Use an array to keep track of the subsets and which nodes belong to that subset. Let the array be parent[].
Initially, all slots of parent array are initialized to hold the same values as the node.
parent[] = {0, 1, 2}. Also when the value of the node and its parent are same, that is the root of that subset of nodes.
Now process all edges one by one.
Edge 0-1:
=> Find the subsets in which vertices 0 and 1 are.
=> 0 and 1 belongs to subset 0 and 1.
=> Since they are in different subsets, take the union of them.
=> For taking the union, either make node 0 as parent of node 1 or vice-versa.
=> 1 is made parent of 0 (1 is now representative of subset {0, 1})
=> parent[] = {1, 1, 2}
Edge 1-2:
=> 1 is in subset 1 and 2 is in subset 2.
=> Since they are in different subsets, take union.
=> Make 2 as parent of 1. (2 is now representative of subset {0, 1, 2})
=> parent[] = {1, 2, 2}
Edge 0-2:
=> 0 is in subset 2 and 2 is also in subset 2.
=> Because 1 is parent of 0 and 2 is parent of 1. So 0 also belongs to subset 2
=> Hence, including this edge forms a cycle.
Therefore, the above graph contains a cycle.
Follow the below steps to implement the idea:
- Initially create a parent[] array to keep track of the subsets.
- Traverse through all the edges:
- Check to which subset each of the nodes belong to by finding the parent[] array till the node and the parent are the same.
- If the two nodes belong to the same subset then they belong to a cycle.
- Otherwise, perform union operation on those two subsets.
- If no cycle is found, return false.
Below is the implementation of the above approach.
C++
// A union-find algorithm to detect cycle in a graph
#include <bits/stdc++.h>
using namespace std;
// a structure to represent an edge in graph
class Edge {
public:
int src, dest;
};
// a structure to represent a graph
class Graph {
public:
// V-> Number of vertices, E-> Number of edges
int V, E;
// graph is represented as an array of edges
Edge* edge;
};
// Creates a graph with V vertices and E edges
Graph* createGraph(int V, int E)
{
Graph* graph = new Graph();
graph->V = V;
graph->E = E;
graph->edge = new Edge[graph->E * sizeof(Edge)];
return graph;
}
// A utility function to find the subset of an element i
int find(int parent[], int i)
{
if (parent[i] == i)
return i;
return find(parent, parent[i]);
}
// A utility function to do union of two subsets
void Union(int parent[], int x, int y) { parent[x] = y; }
// The main function to check whether a given graph contains
// cycle or not
int isCycle(Graph* graph)
{
// Allocate memory for creating V subsets
int* parent = new int[graph->V];
// Initialize all subsets as single element sets
for(int i = 0; i < graph->V; i++) {
parent[i] = i;
}
// Iterate through all edges of graph, find subset of
// both vertices of every edge, if both subsets are
// same, then there is cycle in graph.
for (int i = 0; i < graph->E; ++i) {
int x = find(parent, graph->edge[i].src);
int y = find(parent, graph->edge[i].dest);
if (x == y)
return 1;
Union(parent, x, y);
}
return 0;
}
// Driver code
int main()
{
/* Let us create the following graph
0
| \
| \
1---2 */
int V = 3, E = 3;
Graph* graph = createGraph(V, E);
// add edge 0-1
graph->edge[0].src = 0;
graph->edge[0].dest = 1;
// add edge 1-2
graph->edge[1].src = 1;
graph->edge[1].dest = 2;
// add edge 0-2
graph->edge[2].src = 0;
graph->edge[2].dest = 2;
if (isCycle(graph))
cout << "Graph contains cycle";
else
cout << "Graph doesn't contain cycle";
return 0;
}
// This code is contributed by rathbhupendra
C
// A union-find algorithm to detect cycle in a graph
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// a structure to represent an edge in graph
struct Edge {
int src, dest;
};
// a structure to represent a graph
struct Graph {
// V-> Number of vertices, E-> Number of edges
int V, E;
// graph is represented as an array of edges
struct Edge* edge;
};
// Creates a graph with V vertices and E edges
struct Graph* createGraph(int V, int E)
{
struct Graph* graph
= (struct Graph*)malloc(sizeof(struct Graph));
graph->V = V;
graph->E = E;
graph->edge = (struct Edge*)malloc(
graph->E * sizeof(struct Edge));
return graph;
}
// A utility function to find the subset of an element i
int find(int parent[], int i)
{
if (parent[i] == -1)
return i;
return find(parent, parent[i]);
}
// A utility function to do union of two subsets
void Union(int parent[], int x, int y)
{
parent[y] = x;
}
// The main function to check whether a given graph contains
// cycle or not
int isCycle(struct Graph* graph)
{
// Allocate memory for creating V subsets
int* parent = (int*)malloc(graph->V);
// Initialize all subsets as single element sets
memset(parent, -1, sizeof(graph->V));
// Iterate through all edges of graph, find subset of
// both vertices of every edge, if both subsets are
// same, then there is cycle in graph.
for (int i = 0; i < graph->E; ++i) {
int x = find(parent, graph->edge[i].src);
int y = find(parent, graph->edge[i].dest);
if (x == y && (x!=-1 && y!=-1))
return 1;
Union(parent, x,y);
}
return 0;
}
// Driver program to test above functions
int main()
{
/* Let us create the following graph
0
| \
| \
1---2 */
int V = 3, E = 3;
struct Graph* graph = createGraph(V, E);
// // add edge 0-1
graph->edge[0].src = 0;
graph->edge[0].dest = 1;
// add edge 1-2
graph->edge[1].src = 1;
graph->edge[1].dest = 2;
//add edge 0-2
graph->edge[2].src = 0;
graph->edge[2].dest = 2;
if (isCycle(graph))
printf("Graph contains cycle");
else
printf("Graph doesn't contain cycle");
return 0;
}
Java
// Java Program for union-find algorithm to detect cycle in
// a graph
import java.io.*;
import java.lang.*;
import java.util.*;
public 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 < e; ++i)
edge[i] = new Edge();
}
// A utility function to find the subset of an element i
int find(int parent[], int i)
{
if (parent[i] == i)
return i;
return find(parent, parent[i]);
}
// A utility function to do union of two subsets
void Union(int parent[], int x, int y)
{
parent[x] = y;
}
// The main function to check whether a given graph
// contains cycle or not
int isCycle(Graph graph)
{
// Allocate memory for creating V subsets
int parent[] = new int[graph.V];
// Initialize all subsets as single element sets
for (int i = 0; i < graph.V; ++i)
parent[i] = i;
// Iterate through all edges of graph, find subset
// of both vertices of every edge, if both subsets
// are same, then there is cycle in graph.
for (int i = 0; i < graph.E; ++i) {
int x = graph.find(parent, graph.edge[i].src);
int y = graph.find(parent, graph.edge[i].dest);
if (x == y)
return 1;
graph.Union(parent, x, y);
}
return 0;
}
// Driver Method
public static void main(String[] args)
{
/* Let us create the following graph
0
| \
| \
1---2 */
int V = 3, E = 3;
Graph graph = new Graph(V, E);
// add edge 0-1
graph.edge[0].src = 0;
graph.edge[0].dest = 1;
// add edge 1-2
graph.edge[1].src = 1;
graph.edge[1].dest = 2;
// add edge 0-2
graph.edge[2].src = 0;
graph.edge[2].dest = 2;
if (graph.isCycle(graph) == 1)
System.out.println("Graph contains cycle");
else
System.out.println(
"Graph doesn't contain cycle");
}
}
Python3
# Python Program for union-find algorithm
# to detect cycle in a undirected graph
# we have one edge for any two vertex
# i.e 1-2 is either 1-2 or 2-1 but not both
from collections import defaultdict
# This class represents a undirected graph
# using adjacency list representation
class Graph:
def __init__(self, vertices):
self.V = vertices # No. of vertices
self.graph = defaultdict(list) # default dictionary to store graph
# function to add an edge to graph
def addEdge(self, u, v):
self.graph[u].append(v)
# A utility function to find the subset of an element i
def find_parent(self, parent, i):
if parent[i] == i:
return i
if parent[i] != i:
return self.find_parent(parent, parent[i])
# A utility function to do union of two subsets
def union(self, parent, x, y):
parent[x] = y
# The main function to check whether a given graph
# contains cycle or not
def isCyclic(self):
# Allocate memory for creating V subsets and
# Initialize all subsets as single element sets
parent = [0]*(self.V)
for i in range(self.V):
parent[i] = i
# Iterate through all edges of graph, find subset of both
# vertices of every edge, if both subsets are same, then
# there is cycle in graph.
for i in self.graph:
for j in self.graph[i]:
x = self.find_parent(parent, i)
y = self.find_parent(parent, j)
if x == y:
return True
self.union(parent, x, y)
# Create a graph given in the above diagram
g = Graph(3)
g.addEdge(0, 1)
g.addEdge(1, 2)
g.addEdge(2, 0)
if g.isCyclic():
print("Graph contains cycle")
else:
print("Graph does not contain cycle ")
# This code is contributed by Neelam Yadav
C#
// C# Program for union-find
// algorithm to detect cycle
// in a graph
using System;
class Graph {
// V-> no. of vertices &
// E->no.of edges
public int V, E;
// collection of all edges
public Edge[] edge;
public class Edge {
public int src, dest;
};
// Creates a graph with V
// vertices and E edges
public Graph(int v, int e)
{
V = v;
E = e;
edge = new Edge[E];
for (int i = 0; i < e; ++i)
edge[i] = new Edge();
}
// A utility function to find
// the subset of an element i
int find(int[] parent, int i)
{
if (parent[i] == i)
return i;
return find(parent, parent[i]);
}
// A utility function to do
// union of two subsets
void Union(int[] parent, int x, int y)
{
parent[x] = y;
}
// The main function to check
// whether a given graph
// contains cycle or not
int isCycle(Graph graph)
{
// Allocate memory for
// creating V subsets
int[] parent = new int[graph.V];
// Initialize all subsets as
// single element sets
for (int i = 0; i < graph.V; ++i)
parent[i] = i;
// Iterate through all edges of graph,
// find subset of both vertices of every
// edge, if both subsets are same, then
// there is cycle in graph.
for (int i = 0; i < graph.E; ++i) {
int x = graph.find(parent, graph.edge[i].src);
int y = graph.find(parent, graph.edge[i].dest);
if (x == y)
return 1;
graph.Union(parent, x, y);
}
return 0;
}
// Driver code
public static void Main(String[] args)
{
/* Let us create the following graph
0
| \
| \
1---2 */
int V = 3, E = 3;
Graph graph = new Graph(V, E);
// add edge 0-1
graph.edge[0].src = 0;
graph.edge[0].dest = 1;
// add edge 1-2
graph.edge[1].src = 1;
graph.edge[1].dest = 2;
// add edge 0-2
graph.edge[2].src = 0;
graph.edge[2].dest = 2;
if (graph.isCycle(graph) == 1)
Console.WriteLine("Graph contains cycle");
else
Console.WriteLine(
"Graph doesn't contain cycle");
}
}
// This code is contributed by Princi Singh
JavaScript
<script>
// Javascript program for union-find
// algorithm to detect cycle
// in a graph
// V-> no. of vertices &
// E->no.of edges
var V, E;
// Collection of all edges
var edge;
class Edge
{
constructor()
{
this.src = 0;
this.dest = 0;
}
};
// Creates a graph with V
// vertices and E edges
function initialize(v,e)
{
V = v;
E = e;
edge = Array.from(Array(E), () => Array());
}
// A utility function to find
// the subset of an element i
function find(parent, i)
{
if (parent[i] == i)
return i;
return find(parent, parent[i]);
}
// A utility function to do
// union of two subsets
function Union(parent, x, y)
{
parent[x] = y;
}
// The main function to check
// whether a given graph
// contains cycle or not
function isCycle()
{
// Allocate memory for
// creating V subsets
var parent = Array(V).fill(0);
// Initialize all subsets as
// single element sets
for(var i = 0; i < V; ++i)
parent[i] = i;
// Iterate through all edges of graph,
// find subset of both vertices of every
// edge, if both subsets are same, then
// there is cycle in graph.
for (var i = 0; i < E; ++i)
{
var x = find(parent,
edge[i].src);
var y = find(parent,
edge[i].dest);
if (x == y)
return 1;
Union(parent, x, y);
}
return 0;
}
// Driver code
/* Let us create the following graph
0
| \
| \
1---2 */
var V = 3, E = 3;
initialize(V, E);
// Add edge 0-1
edge[0].src = 0;
edge[0].dest = 1;
// Add edge 1-2
edge[1].src = 1;
edge[1].dest = 2;
// Add edge 0-2
edge[2].src = 0;
edge[2].dest = 2;
if (isCycle() == 1)
document.write("Graph contains cycle");
else
document.write("Graph doesn't contain cycle");
// This code is contributed by rutvik_56
</script>
OutputGraph contains cycle
The time and space complexity of the given code is as follows:
Time Complexity:
- Creating the graph takes O(V + E) time, where V is the number of vertices and E is the number of edges.
- Finding the subset of an element takes O(log V) time in the worst case, where V is the number of vertices. The worst case occurs when the tree is skewed, and the depth of the tree is V.
- Union of two subsets takes O(1) time.
- The loop iterating through all edges takes O(E) time.
- Therefore, the overall time complexity of the algorithm is O(E log V).
However, in practice, it can be much faster than O(E log V) because the worst-case scenario of finding the subset of an element does not happen often.
Space Complexity:
- The space complexity of creating the graph is O(E).
- The space complexity of creating the parent array is O(V).
- The space complexity of the algorithm is O(max(V,E)) because at any point in time, there can be at most max(V,E) subsets.
- Therefore, the overall space complexity of the algorithm is O(max(V,E)).
Detect Cycle using DSU | DSA Problem
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem