Tensorum
LeetCode 684 · Union Find / DSU

Redundant Connection

LeetCode 684 · Medium · solution, intuition & step-by-step walkthrough

First edge connecting an already-connected pair.

MediumDepth-First SearchBreadth-First SearchUnion-FindGraph Theory
1 msruntime · beats 83%
19.6 MBmemory · beats 55%
1approach
1accepted runs · last 25 Apr 2026

This is how I solved Redundant Connection (LeetCode 684), a medium problem from the Union Find / DSU pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.

Intuition

Add edges one at a time with union-find. The first edge whose two ends are already in the same component closes a cycle — that's the redundant one.

Approach

  1. 1

    DSU over n nodes.

  2. 2

    For each edge, if find(u) == find(v), return it.

  3. 3

    Otherwise union.

How I solved it

DSUPythonO(n α(n)) time
class DisjointSet:
    def __init__(self,n):
        self.rank = [0]*(n+1)
        self.parent = [i for i in range(n+1)]
    def find(self,u):
        if self.parent[u]==u:
            return u
        self.parent[u]=self.find(self.parent[u])
        return self.parent[u]
    def union(self,u,v):
        pu,pv = self.find(u),self.find(v)
        if pu==pv:
            return 
        if self.rank[pu]<self.rank[pv]:
            self.parent[pu]=pv
        elif self.rank[pv]<self.rank[pu]:
            self.parent[pv]=pu
        elif self.rank[pv]==self.rank[pu]:
            self.parent[pv]=pu
            self.rank[pu]+=1

class Solution:
    def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
        n = len(edges)
        ds = DisjointSet(n)
        for u,v in edges:
            if ds.find(u)==ds.find(v):
                return [u,v]
            else:
                ds.union(u,v)
Loading the step-through…

Redundant Connection — FAQ

What is the intuition behind Redundant Connection?

Add edges one at a time with union-find. The first edge whose two ends are already in the same component closes a cycle — that's the redundant one.

How do you solve Redundant Connection (LeetCode 684)?

1. DSU over n nodes. 2. For each edge, if find(u) == find(v), return it. 3. Otherwise union.

What is the time and space complexity of Redundant Connection?

Time: O(n α(n)). Space: O(n). (Accepted approach: DSU.)

What are common mistakes in Redundant Connection?

Returning the first such edge matches 'the last one in input order' since only one edge closes the cycle.