-
Notifications
You must be signed in to change notification settings - Fork 43
/
is-graph-bipartite.py
187 lines (164 loc) · 5.67 KB
/
is-graph-bipartite.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""
785. Is Graph Bipartite?
Medium
There is an undirected graph with n nodes, where each node is numbered between 0 and n - 1. You are given a 2D array graph, where graph[u] is an array of nodes that node u is adjacent to. More formally, for each v in graph[u], there is an undirected edge between node u and node v. The graph has the following properties:
There are no self-edges (graph[u] does not contain u).
There are no parallel edges (graph[u] does not contain duplicate values).
If v is in graph[u], then u is in graph[v] (the graph is undirected).
The graph may not be connected, meaning there may be two nodes u and v such that there is no path between them.
A graph is bipartite if the nodes can be partitioned into two independent sets A and B such that every edge in the graph connects a node in set A and a node in set B.
Return true if and only if it is bipartite.
Example 1:
Input: graph = [[1,2,3],[0,2],[0,1,3],[0,2]]
Output: false
Explanation: There is no way to partition the nodes into two independent sets such that every edge connects a node in one and a node in the other.
Example 2:
Input: graph = [[1,3],[0,2],[1,3],[0,2]]
Output: true
Explanation: We can partition the nodes into two sets: {0, 2} and {1, 3}.
Constraints:
graph.length == n
1 <= n <= 100
0 <= graph[u].length < n
0 <= graph[u][i] <= n - 1
graph[u] does not contain u.
All the values of graph[u] are unique.
If graph[u] contains v, then graph[v] contains u.
"""
# V0
# IDEA : GRAPH + DFS
class Solution:
def isBipartite(self, graph):
n = len(graph)
self.color = [0] * n
for i in range(n):
# if grpah i's color = 0, and check if it's "Bipartite" via self.colored(i, graph, 1)
if self.color[i] == 0 and not self.colored(i, graph, 1):
return False
return True
def colored(self, now, graph, c):
self.color[now] = c
for nxt in graph[now]:
# case 1) now color = c, next color = 0, but next's neighbor color != -c => this case is not going to be "Bipartite"
if self.color[nxt] == 0 and not self.colored(nxt, graph, -c):
return False
# case 2) now color == next color (next already colored)
elif self.color[nxt] == self.color[now]:
return False
return True
# V0'
# IDEA : DFS
# CONTINUE
# In [5]: for i in range(10):
# ...: if i%2 ==0:
# ...: continue
# ...: #pass
# ...: print (i)
# ...:
# 1
# 3
# 5
# 7
# 9
import collections
class Solution(object):
def isBipartite(self, graph):
def dfs(v, color):
ocolor = 1 - color
for p in edges[v]:
if p not in colors:
colors[p] = ocolor
if not dfs(p, ocolor):
return False
elif colors[p] != ocolor:
return False
return True
"""
:type graph: List[List[int]]
:rtype: bool
"""
edges = collections.defaultdict(set)
for idx, points in enumerate(graph):
for p in points: edges[idx].add(p)
colors = {}
for k in edges:
if k in colors: continue
colors[k] = 0
if not dfs(k, 0): return False
return True
# V1
# https://www.jiuzhang.com/solution/is-graph-bipartite/#tag-highlight-lang-python
# IDEA : GRAPH + DFS OR BFS
class Solution:
"""
@param graph: the given undirected graph
@return: return true if and only if it is bipartite
"""
def isBipartite(self, graph):
n = len(graph)
self.color = [0] * n
for i in range(n):
if self.color[i] == 0 and not self.colored(i, graph, 1):
return False
return True
def colored(self, now, graph, c):
self.color[now] = c
for nxt in graph[now]:
if self.color[nxt] == 0 and not self.colored(nxt, graph, -c):
return False
elif self.color[nxt] == self.color[now]:
return False
return True
# V1'
# http://bookshadow.com/weblog/2018/02/18/leetcode-is-graph-bipartite/
# IDEA : DFS + GRAPH
import collections
class Solution(object):
def isBipartite(self, graph):
"""
:type graph: List[List[int]]
:rtype: bool
"""
edges = collections.defaultdict(set)
for idx, points in enumerate(graph):
for p in points: edges[idx].add(p)
colors = {}
def dfs(v, color):
ocolor = 1 - color
for p in edges[v]:
if p not in colors:
colors[p] = ocolor
if not dfs(p, ocolor):
return False
elif colors[p] != ocolor:
return False
return True
for k in edges:
if k in colors: continue
colors[k] = 0
if not dfs(k, 0): return False
return True
# V2
# 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 range(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