Redundant Connection
Redundant Connection is LeetCode 684 (Medium). A tree with n nodes labelled 1 to n had one extra edge added, so the undirected graph in edges now contains exactly one cycle.
- Return an edge you can remove so that the rest is a tree of all
nnodes. - If several edges work, return the one that appears last in
edges.
- n == edges.length
- 3 <= n <= 1000
- edges[i].length == 2
- 1 <= ai < bi <= edges.length
- ai != bi
- There are no repeated edges.
- The given graph is connected.
Intuition
Add the edges one at a time and keep track of which nodes are already connected. An edge between two nodes that are already connected gives them a second path, so it is the edge that closes the cycle.
- Tracking groups: union find stores each connected group as a small tree of parent links. Two nodes are connected exactly when their walks up the links end at the same root.
- Why it is the last valid edge: the cycle only exists once all of its edges are present, so the edge that completes it is the latest cycle edge in the list.
"Edges arrive one by one; find the first one that joins two nodes already connected" is the union find signature. The same check builds Kruskal's minimum spanning tree, where an edge whose ends share a root is skipped instead of returned.
Approach
Before reading on, decide how you would know, at the moment an edge arrives, that its two ends are already connected. Then convince yourself why the first edge that fails that test is also the last edge on the cycle. Aim for close to O(n).
Give every node its own group
Set parent[i] = i and size[i] = 1 for every label. Before any edge is read, no two nodes are connected, so each node is the root of a group of one.
Find the root of both ends
For edge [u, v], follow parent links from u until a node points to itself, then do the same from v. While walking, point each node at its grandparent; this path halving keeps later walks short.
Same root: return the edge
If both walks end at the same root, earlier edges already connect u and v, so this edge closes the cycle. Return it straight away; no later edge needs to be read.
Different roots: join the groups
Point the root of the smaller group at the root of the larger one and add the sizes. Hanging the smaller tree under the bigger one keeps every tree shallow, so each walk stays short.
Code in Python, C++ and Java
Common pitfalls
Linking the nodes instead of their roots
if find(u) != find(v):
parent[u] = vru, rv = find(u), find(v)
if ru != rv:
parent[rv] = ruOverwriting parent[u] pulls u out of its old group instead of merging the two groups. On [[1,2],[1,3],[2,3]], node 1 first points to 2, then to 3, so 2 and 3 never share a root and the cycle edge [2,3] is missed.
Sizing the parent array to n
parent = list(range(len(edges)))
parent = list(range(len(edges) + 1))
Nodes are labelled 1 to n, so the array needs index n. With [[1,2],[1,3],[2,3]], looking up node 3 in a 3-slot array raises an index error.
Edge cases
For [[1,2],[2,3],[3,4],[1,4],[1,5]] the answer is [1,4]. Edge [1,5] comes later, but it is not on the cycle: removing it would cut node 5 off. The loop returns at [1,4] before reading it.
Complexity
Redundant Connection FAQ
Can it be solved with DFS?
Yes. Before adding each edge [u, v], run a DFS from u over the edges added so far; if it reaches v, return the edge. That is O(n²), which passes for 1,000 edges. A faster DFS finds the cycle's nodes once, then scans edges from the end and returns the first edge with both ends on the cycle.
Do I need both path halving and union by size?
Either one alone already gives O(log n) per walk, which is fast enough here. Together they bring the cost down to α(n). Without either, a chain like [[1,2],[2,3],[3,4],…] can turn the parent links into one long line, and each walk costs O(n).
How is Redundant Connection II different?
LeetCode 685 makes the edges directed: the input was a rooted tree, and the extra edge can give a node two parents. You first look for a node with two incoming edges, then use union find to decide which of those two edges, or which cycle edge, to remove.