685 Redundant Connection II

1. Question

In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, except for the root node which has no parents.
The given input is a directed graph that started as a rooted tree with N nodes (with distinct values 1, 2, ..., N), with one additional directed 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 ofedges. Each element ofedgesis a pair[u, v]that represents a directed edge connecting nodesuandv, whereuis a parent of childv.
Return an edge that can be removed so that the resulting graph is a rooted tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array.
Example 1:
1
Input: [[1,2], [1,3], [2,3]]
2
3
Output: [2,3]
4
5
Explanation: The given directed graph will be like this:
6
1
7
/ \
8
v v
9
2-->3
Copied!
Example 2:
1
Input: [[1,2], [2,3], [3,4], [4,1], [1,5]]
2
3
Output: [4,1]
4
5
Explanation: The given directed graph will be like this:
6
5 <- 1 -> 2
7
^ |
8
| v
9
4 <- 3
Copied!
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.

2. Implementation

(1) DFS
思路: 有3种情况出现多余的边:
(1) 当没有cycle,当有duplicate parents的时候,我们根据题意返回最后出现的边
(2) 当没有duplicate parents, 有cycle的时候,返回形成cycle的那条边
(3) 当既有duplicate,又有cycle的时候,返回从duplicate parents到当前节点形成cycle的那条边
  1. 1.
    所以我们一步先找是否存在duplicate parents,如果存在,将两个duplicate parents到当前节点边分别存在res1和res2中,其中res2始终是存形成duplicate parents的边中最后出现的那条。同时将当前形成duplicate parent的边删除,这点很关键,因为这让我们在第二步中可以正确的查找cycle是否存在
  2. 2.
    第二步则是查找cycle是否存在,如果没有则返回res2, 如果存在cycle而没有发现duplicate parent的话,我们返回当前的边,如果存在cycle而同时有duplicate parents的话,我们返回res1,原因是第一步中每当我们发现有duplicate parents时,我们都把第二条形成duplicate parents的边删除,如果此时我们发现有cycle,这说明构成cycle的必然形成duplicate parents的第一条边
1
class Solution {
2
public int[] findRedundantDirectedConnection(int[][] edges) {
3
int[] res1 = {-1, -1};
4
int[] res2 = {-1, -1};
5
6
int n = edges.length;
7
int[] parents = new int[n + 1];
8
boolean hasDuplicateParents = false;
9
10
for (int[] edge : edges) {
11
int u = edge[0];
12
int v = edge[1];
13
14
// Find duplicate parents for node v
15
if (parents[v] > 0) {
16
res1[0] = parents[v];
17
res1[1] = v;
18
19
// res2 stores the extra edge that occurs LAST
20
res2[0] = u;
21
res2[1] = v;
22
23
// Remove the edge that cause duplicate parents
24
// If later on we still find cycle in the next step, we should return res1
25
edge[0] = -1;
26
edge[1] = -1;
27
28
hasDuplicateParents = true;
29
}
30
else {
31
parents[v] = u;
32
}
33
}
34
35
parents = new int[n + 1];
36
37
38
// Check if there is a cycle
39
for (int[] edge : edges) {
40
int u = edge[0];
41
int v = edge[1];
42
43
if (u < 0 || v < 0) {
44
continue;
45
}
46
47
parents[v] = u;
48
49
if (hasCycle(v, parents)) {
50
return hasDuplicateParents ? res1 : edge;
51
}
52
}
53
return res2;
54
}
55
56
public boolean hasCycle(int node, int[] parents) {
57
int parent = parents[node];
58
59
while (parent != 0) {
60
if (node == parent) {
61
return true;
62
}
63
parent = parents[parent];
64
}
65
return false;
66
}
67
}
Copied!
(2) Union Find
思路: 这里唯一的区别就是用Union Find查找cycle
1
class Solution {
2
public int[] findRedundantDirectedConnection(int[][] edges) {
3
int[] res1 = new int[] {-1, -1};
4
int[] res2 = new int[] {-1, -1};
5
int n = edges.length;
6
int[] parent = new int[n + 1];
7
8
boolean hasDuplicateParents = false;
9
10
// Step 1: Check if there is duplicate parent
11
for (int[] edge : edges) {
12
// Duplicate parent is found
13
if (parent[edge[1]] > 0) {
14
15
res1 = new int[] {parent[edge[1]], edge[1]};
16
res2 = new int[] {edge[0], edge[1]};
17
18
hasDuplicateParents = true;
19
20
// Delete the current edge
21
// So that we can find cycle correctly in Step 2
22
edge[0] = -1;
23
edge[1] = -1;
24
}
25
else {
26
parent[edge[1]] = edge[0];
27
}
28
}
29
30
// Step 2: Use Union Find to find cycle, same as #684
31
// Here we don't consider the graph is directional
32
UnionFind uf = new UnionFind(n);
33
34
for (int[] edge : edges) {
35
// Skip deleted edge
36
if (edge[0] < 0 || edge[1] < 0) continue;
37
38
// If Cycle is found and no duplicate parent, return current edge
39
// Otherwise, return the edge that causes duplicate parent
40
if (!uf.union(edge[1], edge[0])) {
41
return hasDuplicateParents ? res1 : edge;
42
}
43
}
44
return res2;
45
}
46
47
class UnionFind {
48
int[] sets;
49
int[] size;
50
51
public UnionFind(int n) {
52
sets = new int[n + 1];
53
size = new int[n + 1];
54
55
for (int i = 1; i <= n; i++) {
56
sets[i] = i;
57
size[i] = 1;
58
}
59
}
60
61
public int find(int node) {
62
while (node != sets[node]) {
63
node = sets[node];
64
}
65
return node;
66
}
67
68
public boolean union(int i, int j) {
69
int node1 = find(i);
70
int node2 = find(j);
71
72
// Find Cycle
73
if (node1 == node2) {
74
return false;
75
}
76
77
if (size[node1] < size[node2]) {
78
sets[node1] = node2;
79
size[node2] += size[node1];
80
}
81
else {
82
sets[node2] = node1;
83
size[node1] += size[node2];
84
}
85
return true;
86
}
87
}
88
}
Copied!

3. Time & Space Complexity

DFS: 时间复杂度O(n^2),n是node的个数,空间复杂度O(n)
Union Find: 时间复杂度O(nlogn), n为edge的个数,union()的时间复杂度是O(logn) 但实际上是接近常数的,所以总的时间复杂度可以看成是O(n), 空间复杂度O(n)