Skip to content

Updated check_bipartite_graph_dfs.py #9525

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 26 commits into from
Oct 5, 2023
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ffa6738
Create dijkstra_algorithm.py
debnath003 Oct 2, 2023
6a04fce
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 2, 2023
8faba24
Update dijkstra_algorithm.py
debnath003 Oct 2, 2023
8521a6b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 2, 2023
e0da2c3
Update dijkstra_algorithm.py
debnath003 Oct 2, 2023
934272e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 2, 2023
c2e4771
Update dijkstra_algorithm.py
debnath003 Oct 2, 2023
df722eb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 2, 2023
2888c8b
Delete greedy_methods/dijkstra_algorithm.py
debnath003 Oct 2, 2023
1de4cb5
Update check_bipartite_graph_dfs.py
debnath003 Oct 2, 2023
5857640
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 2, 2023
4d4bb3e
Update check_bipartite_graph_dfs.py
debnath003 Oct 2, 2023
307388b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 2, 2023
b7522ba
Update graphs/check_bipartite_graph_dfs.py
debnath003 Oct 4, 2023
8f46b7f
Update graphs/check_bipartite_graph_dfs.py
debnath003 Oct 4, 2023
81d6e00
Update check_bipartite_graph_dfs.py
debnath003 Oct 4, 2023
2f62202
Update check_bipartite_graph_dfs.py
debnath003 Oct 4, 2023
55a384e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2023
74b0d5b
Update check_bipartite_graph_dfs.py
debnath003 Oct 4, 2023
10a5d0e
Update check_bipartite_graph_dfs.py
debnath003 Oct 4, 2023
afd7c07
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2023
9b949a0
Update check_bipartite_graph_dfs.py
debnath003 Oct 4, 2023
779ccf0
Update check_bipartite_graph_dfs.py
debnath003 Oct 4, 2023
6d5ed2a
Update check_bipartite_graph_dfs.py
debnath003 Oct 4, 2023
272d924
Let's use self-documenting variable names
cclauss Oct 5, 2023
498438b
Update check_bipartite_graph_dfs.py
cclauss Oct 5, 2023
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
Prev Previous commit
Next Next commit
Update check_bipartite_graph_dfs.py
  • Loading branch information
debnath003 authored Oct 2, 2023
commit 4d4bb3e85a2f6218d5498affa8710b6bcda55e45
7 changes: 2 additions & 5 deletions graphs/check_bipartite_graph_dfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,27 @@ def is_bipartite(graph: defaultdict[int, list[int]]) -> bool:
bool: True if the graph is Bipartite, False otherwise.

Examples:
>>> graph1=defaultdict(list,{0:[1, 3],1:[0, 2],2:[1, 3],3:[0,2], 4:[]})
>>> graph1=defaultdict(list,{0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []})
>>> is_bipartite(graph1)
True

>>> graph2 = defaultdict(list, {0: [1, 2], 1: [0, 2], 2: [0, 1]})
>>> is_bipartite(graph2)
False
"""

def dfs(node, color):
vis[node] = color
for nbor in graph[node]:
if vis[nbor] == color or (vis[nbor] == -1 and not dfs(nbor, 1 - color)):
return False
return True

vis = defaultdict(lambda: -1)
vis: defaultdict[int, int] = defaultdict(lambda: -1)

return all(not (vis[node] == -1 and not dfs(node, 0)) for node in graph)


if __name__ == "__main__":
import doctest

result = doctest.testmod()

if result.failed == 0:
Expand Down