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

Function dfs

python3/Graphs/bipartite_graph_validation.py:12–23  ·  view source on GitHub ↗
(node: int, color: int, graph: List[List[int]], colors: List[int])

Source from the content-addressed store, hash-verified

10 return True
11
12def dfs(node: int, color: int, graph: List[List[int]], colors: List[int]) -> bool:
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 # If the current neighbor is not colored, color it with the
20 # other color and continue the DFS.
21 if colors[neighbor] == 0 and not dfs(neighbor, -color, graph, colors):
22 return False
23 return True

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected