Problem Description
Given an undirected graph that started as a tree (connected and acyclic) with n nodes labeled from 1 to n, an extra edge was added to the tree resulting in exactly one cycle. The task is to detect and return the redundant edge that, when removed, will restore the tree property. If multiple such edges exist, return the one that appears last in the input.
Key Insights
- The problem can be solved using a Union Find (Disjoint Set Union) data structure.
- As we iterate over each edge, we check whether the two vertices are already connected.
- If connecting the two vertices would create a cycle (i.e., they already belong to the same set), then the current edge is redundant.
- Alternatives like DFS or BFS are possible, but Union Find offers a more straightforward and efficient solution.
Space and Time Complexity
Time Complexity: O(n * α(n)) where α(n) is the inverse Ackermann function (almost constant in practice). Space Complexity: O(n), for storing the parent array used in the Union Find structure.
Solution
We use the Union Find algorithm by maintaining an array where each node points to its parent. For each edge, we perform a find operation on both endpoints:
- If the endpoints belong to the same set, adding this edge would create a cycle and it is marked as redundant.
- Otherwise, we union the two sets. This algorithm efficiently detects the extra edge that introduces a cycle in the graph.