Redundant Connection - Find the Cycle-Creating Edge

Imagine you have a perfect tree structure with n nodes, where every node is connected and there are no cycles. Now, someone adds exactly one extra edge that creates a cycle, breaking the tree property.

Your mission: Given an array edges representing this corrupted graph, find which edge should be removed to restore the original tree structure. The graph has n nodes labeled from 1 to n, and edges[i] = [ai, bi] represents an undirected edge between nodes ai and bi.

Important: If multiple edges could be removed, return the one that appears last in the input array.

Example: If edges = [[1,2],[1,3],[2,3]], removing [2,3] leaves us with a valid tree.

Input & Output

example_1.py โ€” Simple Triangle
$ Input: edges = [[1,2],[1,3],[2,3]]
โ€บ Output: [2,3]
๐Ÿ’ก Note: The graph forms a triangle. Removing edge [2,3] leaves a valid tree with edges [1,2] and [1,3], where node 1 is the root connecting to nodes 2 and 3.
example_2.py โ€” Linear with Extra Edge
$ Input: edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
โ€บ Output: [1,4]
๐Ÿ’ก Note: The cycle is formed by nodes 1โ†’2โ†’3โ†’4โ†’1. The edge [1,4] appears later in the input than other cycle-forming edges, so it's the redundant connection to remove.
example_3.py โ€” Minimum Case
$ Input: edges = [[1,2],[1,2]]
โ€บ Output: [1,2]
๐Ÿ’ก Note: Two identical edges between the same nodes. The second occurrence [1,2] is redundant and should be removed.

Visualization

Tap to expand
๐Ÿ•ต๏ธ The Family Tree DetectiveInvestigation Process:๐Ÿ‘ค1๐Ÿ‘ค2๐Ÿ‘ค3Initial: Everyone in separate groups๐Ÿ‘ค1๐Ÿ‘ค2๐Ÿ‘ค3Connect 1-2: โœ… Valid relationship๐Ÿ‘ค1๐Ÿ‘ค2๐Ÿ‘ค3Connect 1-3: โœ… Valid relationship๐Ÿ‘ค1๐Ÿ‘ค2๐Ÿ‘ค3โŒ 2-3 already related through 1!๐ŸŽฏ **Key Insight:** The redundant edge connects already-related people!
Understanding the Visualization
1
Start Investigation
Begin with separate family groups (each person is their own group initially)
2
Process Relationships
For each relationship, check if the people are already related through other connections
3
Detect Impossible Loop
The first relationship that connects already-related people creates an impossible loop
4
Remove Redundant Connection
This impossible relationship is the redundant edge that must be removed
Key Takeaway
๐ŸŽฏ Key Insight: Use Union-Find to efficiently track family groups. The first relationship that connects already-related people is the redundant edge!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nโบ(n))

Where โบ(n) is the inverse Ackermann function, practically constant for all realistic inputs

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space for Union-Find parent and rank arrays

n
2n
โšก Linearithmic Space

Constraints

  • n == edges.length
  • 3 โ‰ค n โ‰ค 1000
  • edges[i].length == 2
  • 1 โ‰ค ai < bi โ‰ค edges.length
  • No duplicate edges in the input
  • The given graph is connected
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
67.5K Views
High Frequency
~18 min Avg. Time
2.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen