MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / depth_first_search

Function depth_first_search

graphs/check_bipatrite.py:56–75  ·  view source on GitHub ↗

Perform Depth-First Search (DFS) on the graph starting from a node. Args: node: The current node being visited. color: The color assigned to the current node. Returns: True if the graph is bipartite starting from the current node,

(node: int, color: int)

Source from the content-addressed store, hash-verified

54 """
55
56 def depth_first_search(node: int, color: int) -> bool:
57 """
58 Perform Depth-First Search (DFS) on the graph starting from a node.
59
60 Args:
61 node: The current node being visited.
62 color: The color assigned to the current node.
63
64 Returns:
65 True if the graph is bipartite starting from the current node,
66 False otherwise.
67 """
68 if visited[node] == -1:
69 visited[node] = color
70 if node not in graph:
71 return True
72 for neighbor in graph[node]:
73 if not depth_first_search(neighbor, 1 - color):
74 return False
75 return visited[node] == color
76
77 visited: defaultdict[int, int] = defaultdict(lambda: -1)
78 for node in graph:

Callers 1

is_bipartite_dfsFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected