forked from prabhupant/python-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbipartite_graph.py
More file actions
46 lines (32 loc) · 1.14 KB
/
Copy pathbipartite_graph.py
File metadata and controls
46 lines (32 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Check for bipartite graph
# Do a BFS and make source of red color and its neighbour blue
# Keep on doing this.
# If self loop, return false
# If current color == neighbour color, false, else true
# Reference - https://www.geeksforgeeks.org/bipartite-graph/
from collections import defaultdict
class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = defaultdict(list)
def is_bipartite(self, s):
# 0 -> color 0
# 1 -> color 1
# -1 -> no color assigned
colors = [-1] * self.V
colors[s] = 1
queue = []
queue.append(s)
while queue:
u = queue.pop()
if self.graph[u][v] == 1: # Check for self loop
return False
# An edge u to v exists and destination is not
# colored
for v in range(self.V):
if self.graph[u][v] == 1 and colors[v] == -1:
colors[v] = 1 - colors[u]
queue.append(v)
elif self.graph[u][v] == 1 and colors[v] == colors[u]:
return False
return True