Skip to content

Commit 3d1956d

Browse files
geon0325goswami-rahul
authored andcommitted
Created check_bipartite.py in algorithms/graph + test cases (keon#347)
* Update __init__.py * Update test_graph.py * Create check_bipartite.py * Update README.md * Update README_CN.md * Update README_GE.md * Update README_JP.md * Update README_KR.md * Update check_bipartite.py * Update check_bipartite.py
1 parent 5982b36 commit 3d1956d

File tree

8 files changed

+60
-0
lines changed

8 files changed

+60
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ If you want to uninstall algorithms, it is as simple as:
139139
- [word_break](algorithms/dp/word_break.py)
140140
- [fibonacci](algorithms/dp/fib.py)
141141
- [graph](algorithms/graph)
142+
- [check_bipartite](algorithms/graph/check_bipartite.py)
142143
- [strongly_connected](algorithms/graph/checkDiGraphStronglyConnected.py)
143144
- [clone_graph](algorithms/graph/clone_graph.py)
144145
- [cycle_detection](algorithms/graph/cycle_detection.py)

README_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pip3 uninstall -y algorithms
142142
- [regex_matching:正则匹配](algorithms/dp/regex_matching.py)
143143
- [word_break:单词分割](algorithms/dp/word_break.py)
144144
- [graph:图](graph)
145+
- [check_bipartite](algorithms/graph/check_bipartite.py)
145146
- [2-sat:2-sat](algorithms/graph/satisfiability.py)
146147
- [clone_graph:克隆图](algorithms/graph/clone_graph.py)
147148
- [cycle_detection:判断圈算法](algorithms/graph/cycle_detection.py)

README_GE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
146146
- [word_break](algorithms/dp/word_break.py)
147147
- [fibonacci](algorithms/dp/fib.py)
148148
- [graph](algorithms/graph)
149+
- [check_bipartite](algorithms/graph/check_bipartite.py)
149150
- [strongly_connected](algorithms/graph/checkDiGraphStronglyConnected.py)
150151
- [clone_graph](algorithms/graph/clone_graph.py)
151152
- [cycle_detection](algorithms/graph/cycle_detection.py)

README_JP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ if __name__ == "__main__":
140140
- [word_break](algorithms/dp/word_break.py)
141141
- [fibonacci](algorithms/dp/fib.py)
142142
- [graph : グラフ](algorithms/graph)
143+
- [check_bipartite](algorithms/graph/check_bipartite.py)
143144
- [strongly_connected](algorithms/graph/checkDiGraphStronglyConnected.py)
144145
- [clone_graph](algorithms/graph/clone_graph.py)
145146
- [cycle_detection](algorithms/graph/cycle_detection.py)

README_KR.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ if __name__ == "__main__":
137137
- [word_break](algorithms/dp/word_break.py)
138138
- [fibonacci](algorithms/dp/fib.py)
139139
- [graph : 그래프](algorithms/graph)
140+
- [check_bipartite](algorithms/graph/check_bipartite.py)
140141
- [strongly_connected](algorithms/graph/checkDiGraphStronglyConnected.py)
141142
- [clone_graph](algorithms/graph/clone_graph.py)
142143
- [cycle_detection](algorithms/graph/cycle_detection.py)

algorithms/graph/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from .tarjan import *
2+
from .check_bipartite import *

algorithms/graph/check_bipartite.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
3+
Bipartite graph is a graph whose vertices can be divided into two disjoint and independent sets.
4+
(https://en.wikipedia.org/wiki/Bipartite_graph)
5+
6+
Time complexity is O(|E|)
7+
Space complexity is O(|V|)
8+
9+
"""
10+
11+
def check_bipartite(adj_list):
12+
13+
V = len(adj_list)
14+
15+
# Divide vertexes in the graph into set_type 1 and 2
16+
# Initialize all set_types as -1
17+
set_type = [-1 for v in range(V)]
18+
set_type[0] = 0
19+
20+
q = [0]
21+
22+
while q:
23+
v = q.pop(0)
24+
25+
# If there is a self-loop, it cannot be bipartite
26+
if adj_list[v][v]:
27+
return False
28+
29+
for u in range(V):
30+
if adj_list[v][u]:
31+
if set_type[u] == set_type[v]:
32+
return False
33+
elif set_type[u] == -1:
34+
# set type of u opposite of v
35+
set_type[u] = 1 - set_type[v]
36+
q.append(u)
37+
38+
return True
39+

tests/test_graph.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from algorithms.graph import Tarjan
2+
from algorithms.graph import check_bipartite
23

34
import unittest
45

@@ -42,3 +43,17 @@ def test_tarjan_example_2(self):
4243

4344
g = Tarjan(example)
4445
self.assertEqual(g.sccs, [['A', 'B', 'E'], ['C', 'D'], ['F', 'G'], ['H']])
46+
47+
48+
class TestCheckBipartite(unittest.TestCase):
49+
50+
def test_check_bipartite(self):
51+
52+
adj_list_1 = [[0, 0, 1], [0, 0, 1], [1, 1, 0]]
53+
self.assertEqual(True, check_bipartite(adj_list_1))
54+
55+
adj_list_2 = [[0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0]]
56+
self.assertEqual(True, check_bipartite(adj_list_2))
57+
58+
adj_list_3 = [[0, 1, 0, 0], [1, 0, 1, 1], [0, 1, 0, 1], [0, 1, 1, 0]]
59+
self.assertEqual(False, check_bipartite(adj_list_3))

0 commit comments

Comments
 (0)