Skip to content
AI360Xpert
Beta
Difficulty: MediumGraphs

Redundant Connection

Problem in Plain English

A tree of nodes labelled 1 to n has one extra edge added, which creates exactly one cycle. Return the last input edge whose removal turns the graph back into a tree.

Problem Statement

You are given an undirected graph that started as a tree with n nodes labelled from 1 to n, with one additional edge added. The added edge connects two different vertices, and it was not an edge that already existed.

Return an edge that can be removed so that the resulting graph is a tree of n nodes. If there are multiple answers, return the answer that occurs last in the input.

Constraints

  • n == edges.length
  • 3 <= n <= 1000
  • edges[i].length == 2
  • 1 <= ai < bi <= n
  • No edge appears twice in the input.
  • The given graph is connected.

Examples

Example 1

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

Edges (1,2) and (1,3) already connect all three nodes, so adding (2,3) closes the only cycle and removing it restores a tree.

Example 2

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

The first three edges plus (1,4) form the cycle 1-2-3-4-1, and (1,4) is the last edge of that cycle in the input.

Example 3

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

The triangle 1-2-3-1 is the only cycle, and [1,3] is its last input edge. Later edges [3,4] and [4,5] are bridges: removing either disconnects the graph. The answer is therefore not the last edge overall.

Example 4

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

All three triangle edges can be removed to restore a tree. Their order makes [1,2] the answer, even though it is lexicographically smallest.

Intuition

A tree has a unique path between any two nodes. Adding one edge creates exactly one cycle; removing any edge on that cycle restores a tree, but the input order decides which one to return. In Example 1, when [2,3] arrives, earlier edges already provide 2 -> 1 -> 3. This is the last edge of the only cycle, even if later input edges exist outside the cycle. We can detect the existing path by a graph search or remember connected groups with union find.

Approaches

Path Check Per Edge

Solution Details

Reveal Path Check Per Edge: intuition, complexity, and code

Hints

Hint 1
A connected graph with n vertices and n edges has exactly one cycle. Every removable edge lies on that cycle.
Hint 2
Before inserting [u,v], ask whether earlier edges already connect u and v. The insertion that first finds a path is the last input edge on the unique cycle.
Hint 3
A disjoint set represents components with roots; equal roots detect a cycle, different roots can be merged. Use path halving and union by size.

Edge Cases

  • The three-node triangle is the minimum input; changing its edge order changes the answer.
  • A cycle can complete before the last input edge. Later bridges do not replace the answer, as in Example 3.
  • A 1000-node chain plus a closing edge requires a deep path check; an explicit stack handles it under ordinary recursion limits.
  • Node n is a legal endpoint, so allocate n+1 positions for 1-based labels. The answer is an original ordered endpoint pair, not a pair of representatives.

Common Mistakes and Interview Tips

  • Claiming the first cycle-closing edge is wrong because the statement says last: under the single-cycle promise they are the same edge. Explain why all earlier cycle edges must precede that closure.
  • Returning the final input edge or sorting edges violates the input-order tie-break. A later bridge cannot be removed without disconnection.
  • Inserting an edge before the DFS check lets the new edge supply its own endpoint path. Store each accepted edge in both directions.
  • Merging endpoint labels rather than their component roots can split the parent invariant. Update and compare component sizes only at roots.
  • Calling the size array rank obscures the implementation: sizes add after union, while rank tracks a height bound and increments only on a tie.
  • Returning an input edge row aliases caller-owned storage in Java. These snippets return a fresh pair in every language and leave every input row untouched.
  • Interview tip: distinguish amortized O(alpha(n)) per union-find operation from strict constant time; show how the single-cycle assumption justifies early return.

Key Takeaway

When undirected edges arrive in order, track connected components to answer whether the next edge closes an existing path. A single-cycle promise turns the first failed union into the last cycle edge required by the input-order tie-break.