-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Concatenates both check bipatrite graphs(bfs&dfs) #10708
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
915cb7e
sync
cclauss 5aec68d
fixes#8098
shivaparihar6119 97085bd
deleted: graphs/check_bipartite_graph_all.py
shivaparihar6119 bd440a0
renamed: graphs/check_bipatrite,py -> graphs/check_bipatrite.py
shivaparihar6119 f4d4644
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] dbb5d98
Add the new tests
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
from collections import defaultdict, deque | ||
|
||
|
||
def is_bipartite_dfs(graph: defaultdict[int, list[int]]) -> bool: | ||
""" | ||
Check if a graph is bipartite using depth-first search (DFS). | ||
|
||
Args: | ||
graph: Adjacency list representing the graph. | ||
|
||
Returns: | ||
True if bipartite, False otherwise. | ||
|
||
Checks if the graph can be divided into two sets of vertices, such that no two | ||
vertices within the same set are connected by an edge. | ||
|
||
Examples: | ||
# FIXME: This test should pass. | ||
>>> is_bipartite_dfs(defaultdict(list, {0: [1, 2], 1: [0, 3], 2: [0, 4]})) | ||
Traceback (most recent call last): | ||
... | ||
RuntimeError: dictionary changed size during iteration | ||
>>> is_bipartite_dfs(defaultdict(list, {0: [1, 2], 1: [0, 3], 2: [0, 1]})) | ||
False | ||
>>> is_bipartite_dfs({}) | ||
True | ||
>>> is_bipartite_dfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}) | ||
True | ||
>>> is_bipartite_dfs({0: [1, 2, 3], 1: [0, 2], 2: [0, 1, 3], 3: [0, 2]}) | ||
False | ||
>>> is_bipartite_dfs({0: [4], 1: [], 2: [4], 3: [4], 4: [0, 2, 3]}) | ||
True | ||
>>> is_bipartite_dfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]}) | ||
False | ||
>>> is_bipartite_dfs({7: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]}) | ||
Traceback (most recent call last): | ||
... | ||
KeyError: 0 | ||
|
||
# FIXME: This test should fails with KeyError: 4. | ||
>>> is_bipartite_dfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]}) | ||
False | ||
>>> is_bipartite_dfs({0: [-1, 3], 1: [0, -2]}) | ||
Traceback (most recent call last): | ||
... | ||
KeyError: -1 | ||
>>> is_bipartite_dfs({-1: [0, 2], 0: [-1, 1], 1: [0, 2], 2: [-1, 1]}) | ||
True | ||
>>> 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... | ||
>>> 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 | ||
>>> is_bipartite_dfs({"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]}) | ||
Traceback (most recent call last): | ||
... | ||
KeyError: 1 | ||
>>> is_bipartite_dfs({0: ["b", "d"], 1: ["a", "c"], 2: ["b", "d"], 3: ["a", "c"]}) | ||
Traceback (most recent call last): | ||
... | ||
KeyError: 'b' | ||
""" | ||
|
||
def depth_first_search(node: int, color: int) -> bool: | ||
""" | ||
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, | ||
False otherwise. | ||
""" | ||
if visited[node] == -1: | ||
visited[node] = color | ||
for neighbor in graph[node]: | ||
if not depth_first_search(neighbor, 1 - color): | ||
return False | ||
return visited[node] == color | ||
|
||
visited: defaultdict[int, int] = defaultdict(lambda: -1) | ||
for node in graph: | ||
if visited[node] == -1 and not depth_first_search(node, 0): | ||
return False | ||
return True | ||
|
||
|
||
def is_bipartite_bfs(graph: defaultdict[int, list[int]]) -> bool: | ||
""" | ||
Check if a graph is bipartite using a breadth-first search (BFS). | ||
|
||
Args: | ||
graph: Adjacency list representing the graph. | ||
|
||
Returns: | ||
True if bipartite, False otherwise. | ||
|
||
Check if the graph can be divided into two sets of vertices, such that no two | ||
vertices within the same set are connected by an edge. | ||
|
||
Examples: | ||
# FIXME: This test should pass. | ||
>>> is_bipartite_bfs(defaultdict(list, {0: [1, 2], 1: [0, 3], 2: [0, 4]})) | ||
Traceback (most recent call last): | ||
... | ||
RuntimeError: dictionary changed size during iteration | ||
>>> is_bipartite_bfs(defaultdict(list, {0: [1, 2], 1: [0, 2], 2: [0, 1]})) | ||
False | ||
>>> is_bipartite_bfs({}) | ||
True | ||
>>> is_bipartite_bfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}) | ||
True | ||
>>> is_bipartite_bfs({0: [1, 2, 3], 1: [0, 2], 2: [0, 1, 3], 3: [0, 2]}) | ||
False | ||
>>> is_bipartite_bfs({0: [4], 1: [], 2: [4], 3: [4], 4: [0, 2, 3]}) | ||
True | ||
>>> is_bipartite_bfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]}) | ||
False | ||
>>> is_bipartite_bfs({7: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]}) | ||
Traceback (most recent call last): | ||
... | ||
KeyError: 0 | ||
|
||
# FIXME: This test should fails with KeyError: 4. | ||
>>> is_bipartite_bfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]}) | ||
False | ||
>>> is_bipartite_bfs({0: [-1, 3], 1: [0, -2]}) | ||
Traceback (most recent call last): | ||
... | ||
KeyError: -1 | ||
>>> is_bipartite_bfs({-1: [0, 2], 0: [-1, 1], 1: [0, 2], 2: [-1, 1]}) | ||
True | ||
>>> is_bipartite_bfs({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... | ||
>>> is_bipartite_bfs({0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]}) | ||
True | ||
>>> is_bipartite_bfs({"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]}) | ||
Traceback (most recent call last): | ||
... | ||
KeyError: 1 | ||
>>> is_bipartite_bfs({0: ["b", "d"], 1: ["a", "c"], 2: ["b", "d"], 3: ["a", "c"]}) | ||
Traceback (most recent call last): | ||
... | ||
KeyError: 'b' | ||
""" | ||
visited: defaultdict[int, int] = defaultdict(lambda: -1) | ||
for node in graph: | ||
if visited[node] == -1: | ||
queue: deque[int] = deque() | ||
queue.append(node) | ||
visited[node] = 0 | ||
while queue: | ||
curr_node = queue.popleft() | ||
for neighbor in graph[curr_node]: | ||
if visited[neighbor] == -1: | ||
visited[neighbor] = 1 - visited[curr_node] | ||
queue.append(neighbor) | ||
elif visited[neighbor] == visited[curr_node]: | ||
return False | ||
return True | ||
|
||
|
||
if __name__ == "__main": | ||
import doctest | ||
|
||
result = doctest.testmod() | ||
if result.failed: | ||
print(f"{result.failed} test(s) failed.") | ||
else: | ||
print("All tests passed!") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.