|
| 1 | +## 684. Redundant Connection |
| 2 | + |
| 3 | +### Question: |
| 4 | + In this problem, a tree is an undirected graph that is connected and has no cycles. |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +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. |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +``` |
| 13 | +Example 1: |
| 14 | +
|
| 15 | +Input: [[1,2], [1,3], [2,3]] |
| 16 | +Output: [2,3] |
| 17 | +Explanation: The given undirected graph will be like this: |
| 18 | + 1 |
| 19 | + / \ |
| 20 | +2 - 3 |
| 21 | +
|
| 22 | +Example 2: |
| 23 | +
|
| 24 | +Input: [[1,2], [2,3], [3,4], [1,4], [1,5]] |
| 25 | +Output: [1,4] |
| 26 | +Explanation: The given undirected graph will be like this: |
| 27 | +5 - 1 - 2 |
| 28 | + | | |
| 29 | + 4 - 3 |
| 30 | +``` |
| 31 | + |
| 32 | +Note: |
| 33 | +* The size of the input 2D-array will be between 3 and 1000. |
| 34 | +* Every integer represented in the 2D-array will be between 1 and N, where N is the size of the input array. |
| 35 | + |
| 36 | +### Solution: |
| 37 | +* Method 1: dfs |
| 38 | + * how to find a cycle? |
| 39 | + * before we add current path (u, v) to the graph, we can already find the path from u to v, so after adding current edge, there exists a cycle. |
| 40 | + ```Java |
| 41 | + class Solution { |
| 42 | + private Map<Integer, List<Integer>> graph; |
| 43 | + public int[] findRedundantConnection(int[][] edges) { |
| 44 | + graph = new HashMap<>(); |
| 45 | + for(int[] edge : edges){ |
| 46 | + Set<Integer> visited = new HashSet<>(); |
| 47 | + if(hasPath(edge[0], edge[1], visited)) |
| 48 | + return edge; |
| 49 | + List<Integer> temp = graph.containsKey(edge[0]) ? graph.get(edge[0]): new ArrayList<>(); |
| 50 | + temp.add(edge[1]); |
| 51 | + graph.put(edge[0], temp); |
| 52 | + temp = graph.containsKey(edge[1]) ? graph.get(edge[1]): new ArrayList<>(); |
| 53 | + temp.add(edge[0]); |
| 54 | + graph.put(edge[1], temp); |
| 55 | + } |
| 56 | + return new int[]{-1, -1}; |
| 57 | + } |
| 58 | + private boolean hasPath(int u, int v, Set<Integer> visited){ |
| 59 | + if(u == v) return true; |
| 60 | + visited.add(u); |
| 61 | + if(!graph.containsKey(u) || !graph.containsKey(v)) return false; |
| 62 | + List<Integer> nexts = graph.get(u); |
| 63 | + for(Integer next : nexts){ |
| 64 | + if(visited.contains(next)) continue; |
| 65 | + if(hasPath(next, v, visited)) return true; |
| 66 | + } |
| 67 | + return false; |
| 68 | + } |
| 69 | + } |
| 70 | + ``` |
| 71 | + |
| 72 | +* Method 2: Union find set to be added |
| 73 | + |
| 74 | +### Reference |
| 75 | +1. [花花酱 LeetCode 684. Redundant Connection](http://zxi.mytechroad.com/blog/tree/leetcode-684-redundant-connection/) |
0 commit comments