- Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathGraphValidTree261.java
More file actions
Latest commit
139 lines (120 loc) · 3.97 KB
/
Copy pathGraphValidTree261.java
File metadata and controls
139 lines (120 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Given n nodes labeled from 0 to n-1 and a list of undirected edges (each
* edge is a pair of nodes), write a function to check whether these edges make
* up a valid tree.
*
* Example 1:
* Input: n = 5, and edges = [[0,1], [0,2], [0,3], [1,4]]
* Output: true
*
* Example 2:
* Input: n = 5, and edges = [[0,1], [1,2], [2,3], [1,3], [1,4]]
* Output: false
* Note: you can assume that no duplicate edges will appear in edges. Since all
* edges are undirected, [0,1] is the same as [1,0] and thus will not appear
* together in edges.
*/
publicclassGraphValidTree261 {
// 1. construct Graph
// 2. detection cycle -- DFS
// 3. check not more than one tree
publicbooleanvalidTree(intn, int[][] edges) {
if (n == 0) returnfalse;
if (n == 1) returntrue;
Map<Integer, Set<Integer>> graph = constructGraph(n, edges);
boolean[] visited = newboolean[n];
// if cycle detected, return false
if (!isValid(graph, visited, 0, -1)) returnfalse;
for (inti=0; i<n; i++) {
if (!visited[i]) returnfalse;
}
returntrue;
}
// return false if cycle detected
privatebooleanisValid(Map<Integer, Set<Integer>> graph, boolean[] visited, intcurr, intparent) {
if (visited[curr]) {
returnfalse;
}
visited[curr] = true;
if (!graph.containsKey(curr)) returntrue;
for (inti: graph.get(curr)) {
if (i != parent && !isValid(graph, visited, i, curr)) returnfalse;
}
returntrue;
}
privateMap<Integer, Set<Integer>> constructGraph(intn, int[][] edges) {
Map<Integer, Set<Integer>> graph = newHashMap<>();
for (inti=0; i<n; i++) {
graph.put(i, newHashSet<>());
}
for (int[] edge: edges) {
graph.get(edge[0]).add(edge[1]);
graph.get(edge[1]).add(edge[0]);
}
returngraph;
}
publicbooleanvalidTree2(intn, int[][] edges) {
DisjointSetdjs = newDisjointSet(n);
for (int[] edge: edges) {
intx = djs.find(edge[0]);
inty = djs.find(edge[1]);
if (x == y) returnfalse;
djs.union(x, y);
}
introot = djs.find(0);
for (inti=1; i<n; i++) {
if (djs.find(i) != root) returnfalse;
}
returntrue;
}
classDisjointSet {
int[] parent;
int[] rank;
publicDisjointSet(intn) {
this.parent = newint[n];
for (inti=0; i<n; i++) this.parent[i] = i;
this.rank = newint[n];
}
publicintfind(intx) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
returnparent[x];
}
publicvoidunion(intx, inty) {
intxx = find(x);
intyy = find(y);
if (rank[xx] > rank[yy]) {
parent[yy] = xx;
} elseif (rank[xx] < rank[yy]) {
parent[xx] = yy;
} else {
parent[xx] = yy;
rank[yy]++;
}
}
}
/**
* https://leetcode.com/problems/graph-valid-tree/discuss/69018/AC-Java-Union-Find-solution
*/
publicbooleanvalidTree3(intn, int[][] edges) {
// initialize n isolated islands
int[] nums = newint[n];
Arrays.fill(nums, -1);
// perform union find
for (inti = 0; i < edges.length; i++) {
intx = find(nums, edges[i][0]);
inty = find(nums, edges[i][1]);
// if two vertices happen to be in the same set
// then there's a cycle
if (x == y) returnfalse;
// union
nums[x] = y;
}
returnedges.length == n - 1;
}
intfind(intnums[], inti) {
if (nums[i] == -1) returni;
returnfind(nums, nums[i]);
}
}