Redundant Connection

Difficulty: Medium

You start with a tree of n nodes labeled 1 to n, joined by n - 1 edges. One extra edge is added, creating exactly one cycle.

You're given all n edges in the order they were added. Return an edge you can remove to make the graph a tree again. If several edges would work, return the one that appears last in the input.

Examples

Input: edges = [[1, 2], [1, 3], [2, 3]]
Output: [2, 3]

1-2 and 1-3 already form a tree connecting all three nodes; adding 2-3 on top closes a cycle, and it's the last edge added.

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

1-2-3-4 is a path, and 1-4 closes it into a cycle. 1-5 is added afterward but doesn't touch the cycle.

Constraints

  • n == edges.length

  • 3 <= n <= 1000

  • Each edges[i] = [ai, bi] with 1 <= ai, bi <= n and ai != bi

  • No repeated edges appear in the input.

Approach

Process edges in order with a Union-Find structure, where every node starts in its own group. For each edge [a, b]:

  • If a and b are already in the same group, they could already reach each other. This edge creates the cycle, so return it.
  • Otherwise, merge their groups. It's a real tree edge.

The first edge that closes the cycle is the cycle edge that appears last in the input, which is what the problem asks for.

A brute-force alternative: before adding each edge, run a DFS/BFS over the edges so far to check whether its endpoints are already connected. Same question, but recomputed from scratch each time.

Solutions

Brute Force - Reachability Check Per Edge

Build the graph edge by edge. Before adding each edge, run a DFS to check whether its endpoints are already connected. The first edge where they are closes the cycle.

function findRedundantConnection(edges) {
  const n = edges.length;
  const graph = Array.from({ length: n + 1 }, () => []);

  function canReach(start, target, visited) {
    if (start === target) return true;
    visited.add(start);
    for (const neighbor of graph[start]) {
      if (!visited.has(neighbor) && canReach(neighbor, target, visited)) {
        return true;
      }
    }
    return false;
  }

  for (const [a, b] of edges) {
    if (graph[a].length > 0 || graph[b].length > 0) {
      if (canReach(a, b, new Set())) return [a, b];
    }
    graph[a].push(b);
    graph[b].push(a);
  }

  return [];
}

Time: O(n²) - up to n edges, each requiring an O(n) reachability search · Space: O(n)

Optimal - Union-Find

Start with every node in its own group. For each edge, union its endpoints' groups. If they're already in the same group, the edge is redundant, so return it.

function findRedundantConnection(edges) {
  const n = edges.length;
  const parent = Array.from({ length: n + 1 }, (_, i) => i);

  function find(x) {
    if (parent[x] !== x) {
      parent[x] = find(parent[x]); // path compression
    }
    return parent[x];
  }

  for (const [a, b] of edges) {
    const rootA = find(a);
    const rootB = find(b);

    if (rootA === rootB) return [a, b]; // already connected - this edge closes the cycle

    parent[rootA] = rootB; // union
  }

  return [];
}

Time: O(n × α(n)) - nearly O(n), where α is the inverse Ackermann function · Space: O(n)