785. Is Graph Bipartite?
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 58% Topics: Depth-First Search, Breadth-First Search, Union Find, Graph
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(|V| + |E|)
# Space: O(|V|)
class Solution(object):
def isBipartite(self, graph):
"""
:type graph: List[List[int]]
:rtype: bool
"""
color = {}
for node in xrange(len(graph)):
if node in color:
continue
stack = [node]
color[node] = 0
while stack:
curr = stack.pop()
for neighbor in graph[curr]:
if neighbor not in color:
stack.append(neighbor)
color[neighbor] = color[curr] ^ 1
elif color[neighbor] == color[curr]:
return False
return True
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions