Coding interview problems › Graphs & Grids
🗺️ Graphs & Grids Medium
Redundant Connection
Redundant Connection explained: add edges with Union-Find; the first edge joining two already-connected nodes is the answer. Tested Python and a live visual.
Also searched as: find the edge that creates a cycle · detect cycle in an undirected graph with union find
Try it live Free
Union-Find (DSU)
Stream edges into the Union-Find lab and watch the redundant edge get flagged as a cycle the moment both ends already share a set.
The problem
A graph started as a tree on nodes 1 to n, then one extra edge was added, creating exactly one cycle. Given the edges in order, return an edge you can remove to get a tree again; if several work, return the one that appears last in the input.
The key insight
Add the edges one at a time with Union-Find. An edge whose two endpoints are already in the same set closes a cycle, because a path between them already existed. Since the graph has exactly one cycle, the first such edge found while scanning in order is also the last cycle edge in the input.
Step-by-step approach
- Initialise
parent[i] = ifor nodes 1 to n. - Process edges in input order.
- For edge
(u, v), find both roots. - If the roots are equal, return
[u, v]: this edge is redundant. - Otherwise union the two sets and continue.
Python solution
def find_redundant_connection(edges):
parent = list(range(len(edges) + 1)) # nodes are 1..n, n == len(edges)
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
for u, v in edges:
ru, rv = find(u), find(v)
if ru == rv: # already connected: this edge closes the cycle
return [u, v]
parent[ru] = rv
return []Complexity
There are n edges and each needs two near-constant find operations. The parent array has n + 1 entries.
Common mistakes
- Sizing the parent array as
nwhen nodes are numbered from 1, causing an index error on node n. - Unioning the nodes themselves (
parent[u] = v) instead of their roots, which corrupts the sets. - Applying this to the directed version (Redundant Connection II), which also needs to handle a node with two parents.
Frequently asked questions
What is the time and space complexity of Redundant Connection?
O(n·α(n)) time, which is effectively linear, for n edges with path compression, and O(n) space for the parent array.
Why is the first edge that closes a cycle the right answer?
The graph has exactly one cycle. Every edge before the cycle-closing one is needed to connect its nodes, and the cycle-closing edge is the last cycle edge in input order, which is what the problem asks for.
Can DFS solve it?
Yes. Before adding each edge, DFS to check whether u can already reach v; if so, return the edge. That is O(n²) overall, slower than Union-Find.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Graphs & Grids Graph Search: BFS vs DFS lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.