Redundant Connection

Similar Problems:

- LeetCode: Redundant Connection II
- CheatSheet: Leetcode For Code Interview
- CheatSheet: Common Code Problems & Follow-ups
- Tag: #unionfind, #circleingraph, #graph, #classic

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

The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, …, 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 resulting graph is given as a 2D-array of edges. Each element of edges is a pair [u, v] with u < v, that represents an undirected edge connecting nodes u and v.

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 given 2D-array. The answer edge [u, v] should be in the same format, with u < v.

Example 1:

Input: [[1,2], [1,3], [2,3]] Output: [2,3] Explanation: The given undirected graph will be like this: 1 / \ 2 - 3

Example 2:

Input: [[1,2], [2,3], [3,4], [1,4], [1,5]] Output: [1,4] Explanation: The given undirected graph will be like this: 5 - 1 - 2 | | 4 - 3 Note: The size of the input 2D-array will be between 3 and 1000. Every integer represented in the 2D-array will be between 1 and N, where N is the size of the input array.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.

- Solution:

// https://code.dennyzhang.com/redundant-connection // Basic Ideas: unionfind // For a given edge, if the two nodes already connected, remove this node // Complexity: Time O(n), Space O(n) type UF struct { parent []int } func constructor(size int) UF { parent := make([]int, size) for i, _ := range parent { parent[i] = i } return UF{parent:parent} } func (uf *UF) union(x, y int) { uf.parent[uf.find(y)] = uf.parent[uf.find(x)] } func (uf *UF) find(x int) int { for x != uf.parent[x] { x = uf.parent[x] } return x } func findRedundantConnection(edges [][]int) []int { uf := constructor(len(edges)) for _, edge := range edges { n1, n2 := edge[0]-1, edge[1]-1 if uf.find(n1) == uf.find(n2) { return edge } uf.union(n1, n2) } return []int{} }