Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. class Solution:
  2. def __init__(self):
  3. self.res_outcome = True
  4.  
  5. def dfs(self, visited, graph, i, color):
  6. # if visited[i] == color:
  7. # return False
  8. visited[i] = color
  9. # print('v: {} visited: {}'.format(i, visited))
  10.  
  11. # print('graph: ',graph[i], 'i: ', i, 'visited[i]: ', visited[i])
  12. for each in graph[i]:
  13. # print('each: ', each, 'visited[each]: ', visited[each])
  14. if visited[each] == 0:
  15. if not self.dfs(visited, graph, each, 3-color):
  16. return False
  17. if visited[each] == color:
  18. return False
  19. return True
  20.  
  21.  
  22. def isBipartite(self, graph: List[List[int]]) -> bool:
  23. visited = [0]*len(graph)
  24. for i in range(len(graph)):
  25. if visited[i] == 0:
  26. if not self.dfs(visited, graph, i, 1): # == False
  27. return False
  28. # print(visited)
  29. return True
  30. # return self.res_outcome
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement