Is Graph Bipartite? LeetCode solution in Python

1
class Solution:
2
def isBipartite(self, graph: List[List[int]]) -> bool:
3
v = len(graph)
4
color = [-1] * v
5
color[0] = 1
6

7
def helper(src, q):
8
q.append(src)
9

10
while q:
11
node = q.pop(0)
12

13
for j in graph[node]:
14
if color[j] == -1:
15
color[j] = 1 - color[node]
16
q.append(j)
17
elif color[node] == color[j]:
18
return False
19
return True
20

21
for i in range(v):
22
if not helper(i, []):
23
return False
24
return True

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0