Detect Cycle in an Undirected Graph
Detect cycles in an undirected graph by tracking parent pointers during DFS.
01The problem
Given an undirected graph with V vertices and E edges, represented as an adjacency list adj, determine if the graph contains any cycle.
text
0 --- 1
\ /
2
Output: true (triangle 0-1-2-0 forms a cycle)Constraints
1 <= V, E <= 10^5- The graph may be disconnected (consist of multiple disjoint components).
Loading the rest of the lesson…