Skip to content
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

Fixes #FIXME comment in the program #12535

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 13 additions & 8 deletions graphs/check_bipatrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,19 @@ def is_bipartite_dfs(graph: defaultdict[int, list[int]]) -> bool:
>>> is_bipartite_dfs({0.9: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]})
Traceback (most recent call last):
...
KeyError: 0

>>> # FIXME: This test should fails with
>>> # TypeError: list indices must be integers or...
TypeError: Nodes must be integers
>>> is_bipartite_dfs({0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]})
True
Traceback (most recent call last):
...
TypeError: Nodes must be integers
>>> is_bipartite_dfs({"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]})
Traceback (most recent call last):
...
KeyError: 1
TypeError: Nodes must be integers
>>> is_bipartite_dfs({0: ["b", "d"], 1: ["a", "c"], 2: ["b", "d"], 3: ["a", "c"]})
Traceback (most recent call last):
...
KeyError: 'b'
TypeError: Nodes must be integers
"""

def depth_first_search(node: int, color: int) -> bool:
Expand All @@ -85,6 +84,12 @@ def depth_first_search(node: int, color: int) -> bool:
return False
return visited[node] == color

for node, neighbours in graph.items():
if not isinstance(node, int):
raise TypeError("Nodes must be integers")
for neighbour in neighbours:
if not isinstance(neighbour, int):
raise TypeError("Nodes must be integers")
visited: defaultdict[int, int] = defaultdict(lambda: -1)
for node in graph:
if visited[node] == -1 and not depth_first_search(node, 0):
Expand Down Expand Up @@ -173,7 +178,7 @@ def is_bipartite_bfs(graph: defaultdict[int, list[int]]) -> bool:
return True


if __name__ == "__main":
if __name__ == "__main__":
import doctest

result = doctest.testmod()
Expand Down
Loading