> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Redundant Connection Python Solution

> Tested Python solution for LeetCode 684 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 684, Medium. Topics: Depth-First Search, Breadth-First Search, Union-Find, Graph Theory. [View on LeetCode](https://leetcode.com/problems/redundant-connection/description/).

Generate this problem as a practice environment: tested reference solution, 12 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 684   # by problem number
lcpy gen -s redundant_connection   # by problem name
```

## Problem

In this problem, a tree is an **undirected graph** that is connected and has no cycles.

You are given a graph that started as a tree with `n` nodes labeled from `1` to `n`, with one additional edge added. The added edge has two **different** vertices chosen from `1` to `n`, and was not an edge that already existed. The graph is represented as an array `edges` of length `n` where `edges[i] = [a<sub>i</sub>, b<sub>i</sub>]` indicates that there is an edge between nodes `a<sub>i</sub>` and `b<sub>i</sub>` in the graph.

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.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/05/02/reduntant1-1-graph.jpg)

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

![Example 2](https://assets.leetcode.com/uploads/2021/05/02/reduntant1-2-graph.jpg)

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

### Constraints

* n == edges.length
* 3 \<= n \<= 1000
* edges\[i].length == 2
* 1 \<= a\<sub>i\</sub> \< b\<sub>i\</sub> \<= edges.length
* a\<sub>i\</sub> != b\<sub>i\</sub>
* There are no repeated edges.
* The given graph is connected.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/redundant_connection/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/redundant_connection/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * alpha(n)) ≈ O(n) where alpha is inverse Ackermann
    # Space: O(n)
    def find_redundant_connection(self, edges: list[list[int]]) -> list[int]:
        parent: list[int] = list(range(len(edges) + 1))
        rank: list[int] = [0] * (len(edges) + 1)

        def find(node: int) -> int:
            # Path compression
            if parent[node] != node:
                parent[node] = find(parent[node])
            return parent[node]

        def union(node_a: int, node_b: int) -> bool:
            root_a, root_b = find(node_a), find(node_b)
            if root_a == root_b:
                return False  # Already connected → cycle
            # Union by rank
            if rank[root_a] < rank[root_b]:
                parent[root_a] = root_b
            elif rank[root_a] > rank[root_b]:
                parent[root_b] = root_a
            else:
                parent[root_b] = root_a
                rank[root_a] += 1
            return True

        for node_a, node_b in edges:
            if not union(node_a, node_b):
                return [node_a, node_b]
        return []
```

## Complexity

| Time                                                     | Space |
| -------------------------------------------------------- | ----- |
| O(n \* alpha(n)) ≈ O(n) where alpha is inverse Ackermann | O(n)  |

## Tags

[NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
