-
Notifications
You must be signed in to change notification settings - Fork 0
/
207.py
60 lines (43 loc) · 1.32 KB
/
207.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
"""
Problem:
Given an undirected graph G, check whether it is bipartite. Recall that a graph is
bipartite if its vertices can be divided into two independent sets, U and V, such that
no edge connects vertices of the same set.
"""
from DataStructures.Graph import GraphUndirectedUnweighted
def is_bipartite(graph: GraphUndirectedUnweighted) -> bool:
set_1, set_2 = set(), set()
sorted_nodes = sorted(
graph.connections.items(), key=lambda x: len(x[1]), reverse=True
)
for node, _ in sorted_nodes:
if node in set_2:
continue
set_1.add(node)
for other_node in graph.connections[node]:
set_2.add(other_node)
for node in set_2:
for other_node in graph.connections[node]:
if other_node in set_2:
return False
return True
if __name__ == "__main__":
graph1 = GraphUndirectedUnweighted()
graph1.add_edge(1, 2)
graph1.add_edge(2, 3)
graph1.add_edge(1, 4)
print(is_bipartite(graph1))
graph1.add_edge(1, 3)
print(is_bipartite(graph1))
graph2 = GraphUndirectedUnweighted()
graph2.add_edge(1, 2)
graph2.add_edge(2, 3)
graph2.add_edge(3, 4)
graph2.add_edge(4, 1)
print(is_bipartite(graph2))
"""
SPECS:
TIME COMPLEXITY: O(v + e)
SPACE COMPLEXITY: O(v)
[e = edges, v = vertices]
"""