Skip to content

Commit 22485db

Browse files
authored
Update 0785-is-graph-bipartite.kt
1 parent d5742cb commit 22485db

File tree

1 file changed

+90
-32
lines changed

1 file changed

+90
-32
lines changed

Diff for: kotlin/0785-is-graph-bipartite.kt

+90-32
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,8 @@
1-
package kotlin
2-
3-
import java.util.*
4-
1+
/*
2+
* BFS
3+
*/
54
class Solution {
6-
fun isBipartiteDfs(graph: Array<IntArray>): Boolean {
7-
if (graph.isEmpty()) return true
8-
9-
val nodeColorMap = mutableMapOf<Int, Color>()
10-
fun dfs(node: Int, defaultColor: Color): Boolean {
11-
if (node !in nodeColorMap.keys) nodeColorMap[node] = defaultColor
12-
for (adjacentNode in graph[node]) {
13-
val isEdgeVisited = nodeColorMap[adjacentNode] != null
14-
if (isEdgeVisited) {
15-
val colorOfCurrentNode = nodeColorMap.getValue(node)
16-
val colorOfAdjacentNode = nodeColorMap.getValue(adjacentNode)
17-
if (colorOfAdjacentNode == colorOfCurrentNode) return false
18-
else continue
19-
}
20-
if (!dfs(adjacentNode, defaultColor.inverse)) return false
21-
}
22-
return true
23-
}
24-
25-
for (node in graph.indices) {
26-
if (node in nodeColorMap.keys) continue
27-
if (!dfs(node, Color.RED)) return false
28-
}
29-
return true
30-
}
31-
32-
fun isBipartiteBfs(graph: Array<IntArray>): Boolean {
5+
fun isBipartite(graph: Array<IntArray>): Boolean {
336
if (graph.isEmpty()) return true
347
val nodeColorMap = mutableMapOf<Int, Color>()
358

@@ -46,7 +19,8 @@ class Solution {
4619
if (adjacentNode in nodeColorMap.keys) {
4720
val colorOfAdjacentNode = nodeColorMap.getValue(adjacentNode)
4821
if (colorOfAdjacentNode == colorOfCurrentNode) return false
49-
continue
22+
continue
23+
5024
}
5125
nodeColorMap[adjacentNode] = colorOfCurrentNode.inverse
5226
queue.add(adjacentNode)
@@ -70,3 +44,87 @@ class Solution {
7044
Color.GREEN -> Color.RED
7145
}
7246
}
47+
48+
/*
49+
* DFS
50+
*/
51+
class Solution {
52+
fun isBipartite(graph: Array<IntArray>): Boolean {
53+
if (graph.isEmpty()) return true
54+
55+
val nodeColorMap = mutableMapOf<Int, Color>()
56+
fun dfs(node: Int, defaultColor: Color): Boolean {
57+
if (node !in nodeColorMap.keys) nodeColorMap[node] = defaultColor
58+
for (adjacentNode in graph[node]) {
59+
val isEdgeVisited = nodeColorMap[adjacentNode] != null
60+
if (isEdgeVisited) {
61+
val colorOfCurrentNode = nodeColorMap.getValue(node)
62+
val colorOfAdjacentNode = nodeColorMap.getValue(adjacentNode)
63+
if (colorOfAdjacentNode == colorOfCurrentNode) return false
64+
else continue
65+
}
66+
if (!dfs(adjacentNode, defaultColor.inverse)) return false
67+
}
68+
return true
69+
}
70+
71+
for (node in graph.indices) {
72+
if (node in nodeColorMap.keys) continue
73+
if (!dfs(node, Color.RED)) return false
74+
}
75+
return true
76+
}
77+
78+
private enum class Color { RED, GREEN }
79+
80+
private val Color.inverse
81+
get() = when (this) {
82+
Color.RED -> Color.GREEN
83+
Color.GREEN -> Color.RED
84+
}
85+
}
86+
87+
/*
88+
* Union Find
89+
*/
90+
class Solution {
91+
92+
class DSU(val n: Int) {
93+
94+
val parent = IntArray(n) { it }
95+
val rank = IntArray(n) { 1 }
96+
97+
fun find(x: Int): Int {
98+
if (x != parent[x])
99+
parent[x] = find(parent[x])
100+
return parent[x]
101+
}
102+
103+
fun union(x: Int, y: Int) {
104+
val px = find(x)
105+
val py = find(y)
106+
107+
if (rank[px] >= rank[py]) {
108+
parent[py] = px
109+
rank[px] += rank[py]
110+
} else {
111+
parent[px] = py
112+
rank[py] += rank[px]
113+
}
114+
}
115+
}
116+
117+
fun isBipartite(graph: Array<IntArray>): Boolean {
118+
val dsu = DSU(graph.size)
119+
120+
for (i in 0 until graph.size) {
121+
for (j in graph[i]) {
122+
if (dsu.find(i) == dsu.find(j))
123+
return false
124+
dsu.union(graph[i][0], j)
125+
}
126+
}
127+
128+
return true
129+
}
130+
}

0 commit comments

Comments
 (0)