Introduction
A graph is a data structure consisting of vertices (nodes) and edges (connections). In an undirected graph, an edge connects two vertices in both directions.
The objective of this problem is to determine whether an undirected graph contains a cycle. If a path starts at a vertex, travels through other vertices, and returns to an already visited vertex through a different path, a cycle exists.
This problem can be solved efficiently using Depth First Search (DFS) by tracking visited vertices and their parent nodes.
Problem Statement
Given an undirected graph with V vertices numbered from 0 to V - 1, represented using an adjacency list adj[][], determine whether the graph contains a cycle.
- Return
trueif the graph contains a cycle. - Return
falseotherwise.
Note: The graph may contain multiple disconnected components, so every component must be checked.
Examples
Example 1:
Input:
adj = [[1, 2], [0, 2], [0, 1, 3], [2]]Output: true
Explanation: The vertices 0, 1, and 2 form a cycle. The traversal 0 → 1 → 2 → 0 returns to an already visited vertex through a different path.
Example 2:
Input:
adj = [[1], [0, 2], [1, 3], [2]]Output: false
Explanation: The graph contains a linear connection between vertices 0, 1, 2, and 3. There is no alternate path to reach an already visited vertex, so no cycle exists.
Approach: Depth First Search (DFS)
The DFS approach explores the graph by visiting a node and recursively exploring its unvisited neighbors.
During traversal, two important things are maintained:
- Visited array: Tracks which vertices have already been visited.
- Parent node: Tracks the vertex from which the current vertex was reached.
Why is the parent node necessary?
In an undirected graph, every edge is represented in both directions in the adjacency list.
For example, if vertex 0 is connected to vertex 1:
adj[0]contains1.adj[1]contains0.
When DFS moves from 0 to 1, vertex 1 encounters vertex 0 again. Since 0 is already visited, it could appear to be a cycle. However, this is just the reverse of the same edge.
Therefore, an already visited neighbor is considered a cycle only when it is not the parent of the current vertex.
Algorithm
- Create a
visited[]array of sizeVand initialize all elements tofalse. - Traverse every vertex from
0toV - 1. - If a vertex is not visited, start DFS from that vertex and pass
-1as its parent. - Inside DFS:
- Mark the current vertex as visited.
- Traverse each neighbor of the current vertex.
- If the neighbor is not visited, recursively call DFS with the current vertex as its parent.
- If the neighbor is already visited and is not the parent, a cycle exists. Return
true.
- If DFS detects a cycle in any connected component, return
true. - If all vertices are checked and no cycle is found, return
false.
C# Implementation
class Solution
{
public bool isCycle(List<List<int>> adj)
{
int V = adj.Count;
bool[] visited = new bool[V];
for (int i = 0; i < V; i++)
{
if (!visited[i])
{
if (DFS(i, -1, adj, visited))
return true;
}
}
return false;
}
private bool DFS(int node, int parent,
List<List<int>> adj, bool[] visited)
{
visited[node] = true;
foreach (int neighbor in adj[node])
{
if (!visited[neighbor])
{
if (DFS(neighbor, node, adj, visited))
return true;
}
else if (neighbor != parent)
{
return true;
}
}
return false;
}
}Code Explanation
1. Main function – isCycle()
public bool isCycle(List<List<int>> adj)
{
int V = adj.Count;
bool[] visited = new bool[V];adjrepresents the graph using an adjacency list.V = adj.Countcalculates the total number of vertices.visited[]is a Boolean array used to track visited vertices.
Initially, every vertex is unvisited.

Join the conversation! Your thoughts help the community grow.