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 all commits
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
73 changes: 47 additions & 26 deletions graphs/check_bipartite_graph_dfs.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,55 @@
# Check whether Graph is Bipartite or Not using DFS
from collections import defaultdict


# A Bipartite Graph is a graph whose vertices can be divided into two independent sets,
# U and V such that every edge (u, v) either connects a vertex from U to V or a vertex
# from V to U. In other words, for every edge (u, v), either u belongs to U and v to V,
# or u belongs to V and v to U. We can also say that there is no edge that connects
# vertices of same set.
def check_bipartite_dfs(graph):
visited = [False] * len(graph)
color = [-1] * len(graph)
def is_bipartite(graph: defaultdict[int, list[int]]) -> bool:
"""
Check whether a graph is Bipartite or not using Depth-First Search (DFS).
def dfs(v, c):
visited[v] = True
color[v] = c
for u in graph[v]:
if not visited[u]:
dfs(u, 1 - c)
A Bipartite Graph is a graph whose vertices can be divided into two independent
sets, U and V such that every edge (u, v) either connects a vertex from
U to V or a vertex from V to U. In other words, for every edge (u, v),
either u belongs to U and v to V, or u belongs to V and v to U. There is
no edge that connects vertices of the same set.
for i in range(len(graph)):
if not visited[i]:
dfs(i, 0)
Args:
graph: An adjacency list representing the graph.
for i in range(len(graph)):
for j in graph[i]:
if color[i] == color[j]:
return False
Returns:
True if there's no edge that connects vertices of the same set, False otherwise.
return True
Examples:
>>> is_bipartite(
... defaultdict(list, {0: [1, 2], 1: [0, 3], 2: [0, 4], 3: [1], 4: [2]})
... )
False
>>> is_bipartite(defaultdict(list, {0: [1, 2], 1: [0, 2], 2: [0, 1]}))
True
"""

def depth_first_search(node: int, color: int) -> bool:
visited[node] = color
return any(
visited[neighbour] == color
or (
visited[neighbour] == -1
and not depth_first_search(neighbour, 1 - color)
)
for neighbour in graph[node]
)

# Adjacency list of graph
graph = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []}
print(check_bipartite_dfs(graph))
visited: defaultdict[int, int] = defaultdict(lambda: -1)

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


if __name__ == "__main__":
import doctest

result = doctest.testmod()

if result.failed:
print(f"{result.failed} test(s) failed.")
else:
print("All tests passed!")