Detect Cycle in Undirected Graph

00:00
MediumGraphDFSUnion-FindCycle Detection
AmazonMicrosoft

Detect if an undirected graph has a cycle using DFS or Union-Find.

Examples

Input → 0-1-2-0 (triangle)
Output → True
Input → 0-1-2-3 (path)
Output → False
Input → single edge 0-1
Output → False