-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path886. Possible Bipartition.java
116 lines (92 loc) · 3.35 KB
/
886. Possible Bipartition.java
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
class Solution {
public boolean possibleBipartition(int n, int[][] dislikes) {
Map<Integer, List<Integer>> graph = buildGraph(n, dislikes);
// return isBipartite(graph);
UnionFind uf = new UnionFind(n);
for (int i = 1; i < n + 1; i++) {
List<Integer> neighbors = graph.get(i);
if (neighbors == null || neighbors.size() == 0)
continue;
int firstNeighbor = neighbors.iterator().next(); // gets the first item in the set
for (int neighbor : neighbors) {
if (uf.isConnected(i, neighbor))
return false; // if vertex i is connected with any of its neighbors, graph is not bipartite
uf.union(firstNeighbor, neighbor); // unionize all its neighbors
}
}
return true;
}
private static class UnionFind {
int[] parent;
int[] rank;
public UnionFind(int n) {
parent = new int[n + 1];
rank = new int[n + 1];
for (int i = 0; i < n + 1; i++) {
parent[i] = i;
}
}
public void union(int x, int y) {
int rootX = find(x);
int rootY = find(y);
if (rootX != rootY) {
if (rank[rootX] > rank[rootY]) {
rank[rootX]++;
parent[rootY] = rootX;
} else {
rank[rootY]++;
parent[rootX] = rootY;
}
}
}
public int find(int x) {
if (parent[x] == x)
return x;
int root = find(parent[x]);
parent[x] = root; // path compression
return root;
}
public boolean isConnected(int x, int y) {
return find(x) == find(y);
}
}
public boolean isBipartite(Map<Integer, List<Integer>> graph) {
// Array representing the colors
int[] colors = new int[graph.size()];
// DFS of each node
for (int i = 1; i < graph.size(); i++) {
// If uncolored, then perform DFS
if (colors[i] == 0 && !hasEvenCycle(graph, colors, i, 1))
return false;
}
return true;
}
// Return true when graph is bipartite
public boolean hasEvenCycle(Map<Integer, List<Integer>> graph, int[] colors, int node, int c) {
// if node is colored, node color is same as sent in func definition, return
// true
if (colors[node] != 0)
return colors[node] == c;
// Color the current node with color sent in func definition
colors[node] = c;
// Check for all the adjecent nodes of the current node "node"
for (int n : graph.get(node)) {
if (!hasEvenCycle(graph, colors, n, -c))
return false;
}
return true;
}
private Map<Integer, List<Integer>> buildGraph(int n, int[][] dislikes) {
// TODO Auto-generated method stub
Map<Integer, List<Integer>> graph = new HashMap<>();
for (int[] edge : dislikes) {
int u = edge[0];
int v = edge[1];
graph.putIfAbsent(u, new ArrayList<>());
graph.putIfAbsent(v, new ArrayList<>());
graph.get(u).add(v);
graph.get(v).add(u);
}
return graph;
}
}