Redundant Connection
Medium
Topics
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
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
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
- Initialize DSU over the nodes.
- For each edge, if find(a) == find(b) the edge is redundant — return it.
- 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 withIndexError.
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 []import java.util.*;
class Solution {
public int[] findRedundantConnection(int[][] edges) {
int mx=edges.length+1;
int[] par=new int[mx+1];
for(int i=0;i<=mx;i++)par[i]=i;
for(int[] e:edges) {
int ra=find(par,e[0]),rb=find(par,e[1]);
if(ra==rb)return new int[]{e[0],e[1]};
par[ra]=rb;
}
return new int[]{};
}
int find(int[] par,int x) {
while(par[x]!=x) {
par[x]=par[par[x]];
x=par[x];
}
return x;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: vector<int> findRedundantConnection(vector<vector<int>>& edges) {
int mx=edges.size()+1;
vector<int> par(mx+1);
for(int i=0;i<=mx;i++)par[i]=i;
function<int(int)> find=[&](int x) {
while(par[x]!=x) {
par[x]=par[par[x]];
x=par[x];
}
return x;
};
for(auto& e:edges) {
int ra=find(e[0]),rb=find(e[1]);
if(ra==rb)return vector<int> {
e[0],e[1]
};
par[ra]=rb;
}
return {
};
}
};/**
* @param {number[][]} edges
* @return {number[]}
*/ var findRedundantConnection = function(edges) {
const par=[];
for(let i=0;i<=edges.length;i++)par[i]=i;
function find(x) {
while(par[x]!==x) {
par[x]=par[par[x]];
x=par[x];
}
return x;
}
for(const e of edges) {
const ra=find(e[0]),rb=find(e[1]);
if(ra===rb)return [e[0],e[1]];
par[ra]=rb;
}
return [];
};