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

Function dfs

cpp/Graphs/bipartite_graph_validation.cpp:14–29  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

bipartiteGraphValidationFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected