Redundant Connection
You are given a graph that started as a tree with n nodes labeled from 1 to n.
One additional edge was added to the tree, creating exactly one cycle.
Return the edge that can be removed so that the resulting graph becomes a tree again.
If multiple answers are possible, return the edge that appears last in the input.
Example
edges =
1 - 2
1 - 3
2 - 3
The graph is:
1
/ \
2---3
The edge:
2 - 3
creates the cycle.
Therefore, the answer is:
[2, 3]
Main Idea
This problem can be efficiently solved using Union Find (Disjoint Set Union).
Initially, every node belongs to its own set.
For every edge:
u - v
check whether u and v already belong to the same set.
If they belong to different sets
Join them:
Union(u, v)
If they already belong to the same set
Adding this edge creates a cycle.
Therefore, this is the redundant edge.
For example:
1 - 2
1 - 3
2 - 3
After processing:
1 - 2
1 and 2 are connected.
Then:
1 - 3
3 joins the same component.
Finally:
2 - 3
Both are already in the same component.
Therefore:
[2, 3]
is the redundant connection.
Example 1
Input
3 1 2 1 3 2 3
Output
2 3
Explanation
The first two edges create:
1 - 2
|
3
When we process:
2 - 3
nodes 2 and 3 are already connected through node 1.
Therefore, adding this edge creates a cycle:
1
| \
2-3
So the redundant edge is:
[2, 3]
Example 2
Input
5 1 2 2 3 3 4 1 4 1 5
Output
1 4
Explanation
The edges initially create:
1 - 2 - 3 - 4
Then:
1 - 4
creates the cycle:
1 - 2 - 3 - 4
| |
└───────────┘
Therefore:
[1, 4]
is the redundant edge.
Constraints
Hints:
Hint 1
A tree with n nodes has exactly:
n - 1 edges
The extra edge creates the cycle.
Hint 2
Use Union Find to keep track of connected components.
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 16, 2026
Last updated
Aug 16, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output