Skip to content

785.Is Graph Bipartite? #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions leetcode/785.Is Graph Bipartite?
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
func isBipartite(graph [][]int) bool {
colors := make([]int,len(graph))


for i := range colors {
colors[i] = -1
}


for i := range graph {
if !dfs(i, graph, colors, -1) {
fmt.Println(colors)
return false
}
}

fmt.Println(colors)
return true

}

func dfs(n int, graph [][]int, colors []int, parentCol int) bool {
if colors[n] == -1 {
if parentCol == 1 {
colors[n] = 0
} else {
colors[n] = 1
}
} else if colors[n] == parentCol {
fmt.Println(n)
return false
} else if colors[n] != parentCol {
return true
}


for _, c := range graph[n] {
if !dfs(c, graph, colors, colors[n]) {
fmt.Println(c)
return false
}
}
return true
}