Skip to main content

Cycle Detection in Undirected Graph (DFS)

Problem Statement:​

Given an undirected graph with V vertices and E edges, represented as a 2D vector edges[][] , where each entry edges[i] = [u, v] denotes an edge between vertices u and v , determine whether the graph contains a cycle or not.

Examples:

Input:V = 4, E = 4, edges[][] = [[0, 1], [0, 2], [1, 2], [2, 3]]
Output:true
Explanation:
1 -> 2 -> 0 -> 1 is a cycle.

Input:V = 4, E = 3, edges[][] = [[0, 1], [1, 2], [2, 3]]
Output:false
Explanation:

No cycle in the graph.

  • Example:


βœ… Solution: Detect Cycle in Undirected Graph Using DFS (With Parent Tracking)​


// βœ… Cycle Detection in Undirected Graph Using DFS (C++) - Parent Tracking

class Solution {
public:
bool detect(int node, int parent, vector<bool>& visited, vector<vector<int>>& adj) {
visited[node] = true;

for (auto adjNode : adj[node]) {
if (!visited[adjNode]) {
if (detect(adjNode, node, visited, adj)) return true;
}
else if (adjNode != parent) {
// If already visited and not the parent, it's a cycle
return true;
}
}

return false;
}

bool isCycle(int V, vector<vector<int>>& edges) {
vector<vector<int>> adj(V);

// Build adjacency list from edge list
for (auto edge : edges) {
adj[edge[0]].push_back(edge[1]);
adj[edge[1]].push_back(edge[0]);
}

vector<bool> visited(V, false);

// Check each component
for (int i = 0; i < V; i++) {
if (!visited[i]) {
if (detect(i, -1, visited, adj)) return true;
}
}

return false;
}
};


πŸ“ How It Works​

  • Step 1: Convert edge list to adjacency list.
  • Step 2: Run DFS on each unvisited node:
    • Mark node as visited.
    • If an already visited neighbor is found that is not the parent, it’s a cycle.
  • Step 3: If DFS finds any cycle, return true; otherwise false.

βœ… It checks each connected component using standard DFS with parent check.


🧩 Key Formula / Recurrence​

  • DFS Cycle Formula:

    if (visited[adjNode] && adjNode != parent) β†’ cycle exists


⏱️ Time & Space Complexity​

MetricValue
TimeO(V + E)
SpaceO(V + E)

Where:

  • V is number of vertices.
  • E is number of edges.

⚠️ Edge Cases​

  • Self-loop β†’ Cycle detected.
  • Disconnected components β†’ Handled by running DFS from all nodes.
  • No edges β†’ Returns false.

πŸ’‘ Other Approaches​

ApproachTime ComplexityNotes
BFS with Parent TrackingO(V + E)Works similarly, uses a queue.
Union-Find (Disjoint Set)O(E log V)Fast for dynamic edge addition queries.

  • Detect Cycle in Directed Graph
  • Redundant Connection
  • Connected Components Counting
  • Spanning Tree Validation

πŸ’¬

Discussion & Doubts