-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis-graph-bipartite.go
68 lines (56 loc) · 1.27 KB
/
is-graph-bipartite.go
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
package main
import "fmt"
// source: https://leetcode.com/problems/is-graph-bipartite/
const (
black = iota + 1
white
)
// while going through the graph, painting each next node with one two colors
// every adjacent nodes must have different colors for graph to be bipartite
func isBipartite(graph [][]int) bool {
if len(graph) < 2 {
return true
}
colors := make([]int, len(graph))
var dfs func(node, color int) bool
dfs = func(node, color int) bool {
if colors[node] != 0 && colors[node] != color {
return false
}
if colors[node] == color {
return true
}
colors[node] = color
switch color {
case black:
color = white
case white:
color = black
}
for _, sibling := range graph[node] {
if !dfs(sibling, color) {
return false
}
}
return true
}
for i := range graph {
if colors[i] == 0 {
if !dfs(i, black) {
return false
}
}
}
return true
}
func main() {
// Example 3
var graph3 = [][]int{nil}
fmt.Println("Expected: true Output: ", isBipartite(graph3))
// Example 2
var graph2 = [][]int{{1, 3}, {0, 2}, {1, 3}, {0, 2}}
fmt.Println("Expected: true Output: ", isBipartite(graph2))
// Example 1
var graph1 = [][]int{{1, 2, 3}, {0, 2}, {0, 1, 3}, {0, 2}}
fmt.Println("Expected: false Output: ", isBipartite(graph1))
}