-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Changes from 16 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
ffa6738
Create dijkstra_algorithm.py
debnath003 6a04fce
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8faba24
Update dijkstra_algorithm.py
debnath003 8521a6b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e0da2c3
Update dijkstra_algorithm.py
debnath003 934272e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c2e4771
Update dijkstra_algorithm.py
debnath003 df722eb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2888c8b
Delete greedy_methods/dijkstra_algorithm.py
debnath003 1de4cb5
Update check_bipartite_graph_dfs.py
debnath003 5857640
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4d4bb3e
Update check_bipartite_graph_dfs.py
debnath003 307388b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b7522ba
Update graphs/check_bipartite_graph_dfs.py
debnath003 8f46b7f
Update graphs/check_bipartite_graph_dfs.py
debnath003 81d6e00
Update check_bipartite_graph_dfs.py
debnath003 2f62202
Update check_bipartite_graph_dfs.py
debnath003 55a384e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 74b0d5b
Update check_bipartite_graph_dfs.py
debnath003 10a5d0e
Update check_bipartite_graph_dfs.py
debnath003 afd7c07
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9b949a0
Update check_bipartite_graph_dfs.py
debnath003 779ccf0
Update check_bipartite_graph_dfs.py
debnath003 6d5ed2a
Update check_bipartite_graph_dfs.py
debnath003 272d924
Let's use self-documenting variable names
cclauss 498438b
Update check_bipartite_graph_dfs.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,50 @@ | ||
# Check whether Graph is Bipartite or Not using DFS | ||
|
||
|
||
# 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 dfs(v, c): | ||
visited[v] = True | ||
color[v] = c | ||
for u in graph[v]: | ||
if not visited[u]: | ||
dfs(u, 1 - c) | ||
|
||
for i in range(len(graph)): | ||
if not visited[i]: | ||
dfs(i, 0) | ||
|
||
for i in range(len(graph)): | ||
for j in graph[i]: | ||
if color[i] == color[j]: | ||
from collections import defaultdict | ||
|
||
|
||
def is_bipartite(graph: defaultdict[int, list[int]]) -> bool: | ||
""" | ||
Check whether a graph is Bipartite or not using Depth-First Search (DFS). | ||
|
||
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. | ||
|
||
Args: | ||
graph: An adjacency list representing the graph. | ||
|
||
Returns: | ||
True if there's no edge that connects vertices of the same set, False otherwise. | ||
|
||
Examples: | ||
>>> 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 | ||
debnath003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
vis: defaultdict[int, int] = defaultdict(lambda: -1) | ||
|
||
return all(not (vis[node] == -1 and not dfs(node, 0)) for node in graph) | ||
|
||
|
||
return True | ||
if __name__ == "__main__": | ||
import doctest | ||
|
||
result = doctest.testmod() | ||
|
||
# Adjacency list of graph | ||
graph = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []} | ||
print(check_bipartite_dfs(graph)) | ||
if result.failed == 0: | ||
print("All tests passed!") | ||
else: | ||
print(f"{result.failed} test(s) failed.") |
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.