Redundant Connection
Master this topic with zero to advance depth.
Redundant Connection
In this problem, a tree (undirected graph with no cycles) was modified by adding one extra edge. Return the edge that can be removed so the resulting graph is a tree.
Examples
Level I: DFS (Incremental Cycles)
Intuition
For each new edge , check if is already reachable from . If yes, this edge forms a cycle and is redundant.
Detailed Dry Run
Edge [1,2]: Added. Edge [1,3]: Added. Edge [2,3]: DFS(2) finds 3 via 1. Redundant!
Level III: Union-Find (Efficiency)
Intuition
Use DSU to process edges. If an edge connects two nodes that already share the same root, that edge is the redundant one.
Detailed Dry Run
Edge [1,2]: Union(1,2). Edge [1,3]: Union(1,3). Edge [2,3]: find(2)=3, find(3)=3. Same component. Return [2,3].
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.