We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0d22f4b commit 69470c8Copy full SHA for 69470c8
Leetcode/is_graph_bipartite.py
@@ -0,0 +1,20 @@
1
+# 785. Is Graph Bipartite?
2
+
3
+from collections import deque
4
+class Solution:
5
+ def isBipartite(self, graph: List[List[int]]) -> bool:
6
+ n = len(graph)
7
+ self.nodes = [-1]*n
8
+ def bfs(s):
9
+ self.nodes[s] = 0
10
+ q = deque([s])
11
+ while q:
12
+ cur = q.popleft()
13
+ for t in graph[cur]:
14
+ if self.nodes[t] == -1:
15
+ self.nodes[t] = self.nodes[cur] + 1
16
+ q.append(t)
17
+ elif self.nodes[t] == self.nodes[cur]:
18
+ return False
19
+ return True
20
+ return all(bfs(v) for v in range(n) if self.nodes[v] == -1)
0 commit comments