graph + disjoint sets
1
2
3
parent[]
| 1 | 2 | 3 |
|---|---|---|
| 1 | 2 | 3 |
Union-Find detects the cycle
▸1parent[i] ← i // each node its own set2find(x): while parent[x] != x: x = parent[x]; return x3for (a, b) in edges:4 if find(a) == find(b): return [a, b] // same set → cycle5 parent[find(b)] = find(a) // union6// first same-set edge is the redundant one
state
- edges[[1,2],[1,3],[2,3]]
- parent[1,2,3]
- sets3