-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0785-is-graph-bipartite.kt
130 lines (109 loc) · 3.61 KB
/
0785-is-graph-bipartite.kt
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
/*
* BFS
*/
class Solution {
fun isBipartite(graph: Array<IntArray>): Boolean {
if (graph.isEmpty()) return true
val nodeColorMap = mutableMapOf<Int, Color>()
fun bfs(startNode: Int): Boolean {
// if the node is already colored, return true
if (startNode in nodeColorMap.keys) return true
nodeColorMap[startNode] = Color.RED
val queue = (LinkedList<Int>() as Queue<Int>).apply { add(startNode) }
while (queue.isNotEmpty()) {
val currentNode = queue.remove()
val colorOfCurrentNode = nodeColorMap.getValue(currentNode)
for (adjacentNode in graph[currentNode]) {
if (adjacentNode in nodeColorMap.keys) {
val colorOfAdjacentNode = nodeColorMap.getValue(adjacentNode)
if (colorOfAdjacentNode == colorOfCurrentNode) return false
continue
}
nodeColorMap[adjacentNode] = colorOfCurrentNode.inverse
queue.add(adjacentNode)
}
}
return true
}
for (node in graph.indices) {
if (!bfs(node)) return false
}
return true
}
private enum class Color { RED, GREEN }
private val Color.inverse
get() = when (this) {
Color.RED -> Color.GREEN
Color.GREEN -> Color.RED
}
}
/*
* DFS
*/
class Solution {
fun isBipartite(graph: Array<IntArray>): Boolean {
if (graph.isEmpty()) return true
val nodeColorMap = mutableMapOf<Int, Color>()
fun dfs(node: Int, defaultColor: Color): Boolean {
if (node !in nodeColorMap.keys) nodeColorMap[node] = defaultColor
for (adjacentNode in graph[node]) {
val isEdgeVisited = nodeColorMap[adjacentNode] != null
if (isEdgeVisited) {
val colorOfCurrentNode = nodeColorMap.getValue(node)
val colorOfAdjacentNode = nodeColorMap.getValue(adjacentNode)
if (colorOfAdjacentNode == colorOfCurrentNode) return false
else continue
}
if (!dfs(adjacentNode, defaultColor.inverse)) return false
}
return true
}
for (node in graph.indices) {
if (node in nodeColorMap.keys) continue
if (!dfs(node, Color.RED)) return false
}
return true
}
private enum class Color { RED, GREEN }
private val Color.inverse
get() = when (this) {
Color.RED -> Color.GREEN
Color.GREEN -> Color.RED
}
}
/*
* Union Find
*/
class Solution {
class DSU(val n: Int) {
val parent = IntArray(n) { it }
val rank = IntArray(n) { 1 }
fun find(x: Int): Int {
if (x != parent[x])
parent[x] = find(parent[x])
return parent[x]
}
fun union(x: Int, y: Int) {
val px = find(x)
val py = find(y)
if (rank[px] >= rank[py]) {
parent[py] = px
rank[px] += rank[py]
} else {
parent[px] = py
rank[py] += rank[px]
}
}
}
fun isBipartite(graph: Array<IntArray>): Boolean {
val dsu = DSU(graph.size)
for (i in 0 until graph.size) {
for (j in graph[i]) {
if (dsu.find(i) == dsu.find(j))
return false
dsu.union(graph[i][0], j)
}
}
return true
}
}