MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / dfs

Function dfs

kotlin/Graphs/BipartiteGraphValidation.kt:12–27  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10}
11
12fun dfs(node: Int, color: Int, graph: List<List<Int>>, colors: IntArray): Boolean {
13 colors[node] = color
14 for (neighbor in graph[node]) {
15 // If the current neighbor has the same color as the current
16 // node, the graph is not bipartite.
17 if (colors[neighbor] == color) {
18 return false
19 }
20 // If the current neighbor is not colored, color it with the
21 // other color and continue the DFS.
22 if (colors[neighbor] == 0 && !dfs(neighbor, -color, graph, colors)) {
23 return false
24 }
25 }
26 return true
27}

Callers 1

bipartiteGraphValidationFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected