Redundant Connection

Medium

Topics
GraphUnion Find

A tree on nodes 1..n had one extra edge added, creating exactly one cycle. Given the edges in input order, return the edge that can be removed so the result is a tree. If several qualify, return the one that appears last in the input.

Example 1

123
A tree on 3 nodes has 2 edges; one extra closes exactly one cycle.
Edges over nodes 1..n; the answer must be the last such edge in input order.
Input:  edges = [[1,2],[1,3],[2,3]]
Output: [2,3]

Example 2

12345
Input:  edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
Output: [1,4]

Constraints

  • 3 <= n <= 1000
  • The graph is a tree plus exactly one extra edge.
Editorial — intuition, approach, complexity and pitfalls

Intuition

Process edges in order with union-find. The first edge whose two endpoints are already connected is the one closing the cycle — and because there is exactly one extra edge, that is the answer.

Approach

  1. Initialize DSU over the nodes.
  2. For each edge, if find(a) == find(b) the edge is redundant — return it.
  3. Otherwise union them.

Dry Run

edges 1-2 1-3 2-3
The parent array is sized len(edges) + 1 = 4. That is not arbitrary: nodes are
1-indexed, and a tree plus one extra edge has exactly as many nodes as edges, so
len(edges) + 1 covers node 0 through node n.
  par [0, 1, 2, 3]
  edge 1-2   find(1)=1  find(2)=2   differ, union   par [0, 2, 2, 3]
  edge 1-3   find(1)=2  find(3)=3   differ, union   par [0, 2, 3, 3]
  edge 2-3   find(2)=3  find(3)=3   SAME root
             1, 2 and 3 are already one component, so this edge closes the cycle
answer 2 3
The condition to return on is `ra == rb`, the edge whose endpoints were ALREADY
connected. That reads backwards at first -- it is the edge that does nothing that
turns out to be the answer, because every earlier edge was load-bearing. Return
on the roots differing instead and you get the first edge in the list every time.
Scanning front to back returns the edge that closes the FIRST cycle the scan
runs into, which is that cycle's last edge in input order. On edges
1-2 2-3 1-3 3-4 the answer is 1-3, not the later 3-4, because 3-4 is not on any
cycle and removing it would disconnect node 4.
Size the array by node count alone and node 3 is out of range: the run ends in
IndexError: list index out of range rather than a wrong answer.

Complexity

  • Time — O(n alpha): one near-constant union-find op per edge.
  • Space — O(n): the parent array.

Common Pitfalls

  • Return the edge whose endpoints are already connected -- ra == rb. Returning the first edge whose roots differ returns the very first edge in the list every time.
  • Size the parent array len(edges) + 1. Nodes are 1-indexed and a tree plus one edge has exactly as many nodes as edges, so sizing it by node count alone walks off the end with IndexError.

Full Solution — all languages

class Solution:
    def findRedundantConnection(self, edges):
        par=list(range(len(edges)+1))
        def find(x):
            while par[x]!=x:
                par[x]=par[par[x]]; x=par[x]
            return x
        for a,b in edges:
            ra,rb=find(a),find(b)
            if ra==rb: return [a,b]
            par[ra]=rb
        return []
SensAI
Beta
Tap the mic to stop voice mode