-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc.py
155 lines (119 loc) · 3.8 KB
/
cc.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
#!/usr/bin/env python3
"""
The task is to use depth-first search to compute the number of
connected components in a given undirected graph.
"""
import os
_FILE = "input/rosalind_cc.txt"
if os.path.isfile(_FILE):
with open(_FILE, "r", encoding="utf-8") as _f:
INPUT_DATA = _f.read().strip()
else:
INPUT_DATA = str()
INPUT_DATA = (
INPUT_DATA
or """
12 13
1 2
1 5
5 9
5 10
9 10
3 4
3 7
3 8
4 8
7 11
8 11
11 12
8 12
"""
)
class Vertex:
"""Create a lone vertex"""
def __init__(self, id):
self.id = id
self.edges = []
def __str__(self):
return f"{self.__class__.__name__}(id={self.id}, edges={self.edges})"
class GraphUnDirected:
"""Create a new graph with specified # of vertexes"""
def __init__(self, n_vertexes, n_edges):
self.expected_edges_num = n_edges * 2 # double for undirected graphs
self.vertexes = [Vertex(x) for x in range(n_vertexes)]
def __str__(self):
return (
f"{self.__class__.__name__}"
+ f"(n_vertexes={len(self.vertexes)}, "
+ f"n_edges={sum(len(x.edges) for x in self.vertexes)})"
)
def add_edge(self, id_vertex_from, id_vertex_to):
"""Add edge: "incoming --> outbound" vertex"""
self.vertexes[id_vertex_from].edges.append(id_vertex_to)
# undirected graph, so add both ways
self.vertexes[id_vertex_to].edges.append(id_vertex_from)
def validate_edge_number(self):
"""Check the edge number meets expected/given value"""
expected = self.expected_edges_num
actual = sum(len(x.edges) for x in self.vertexes)
if actual != expected:
raise ValueError(
f"Wrong number of edges: expected {expected}, actual {actual}"
)
def connected_components(self, current_node: int):
"""The main algorithm"""
queue = [current_node]
explored = set(queue)
while queue:
print(f"Current Node: {queue[0]}")
vertex = self.vertexes[queue[0]]
for edge in vertex.edges:
if edge not in explored:
queue.append(edge)
explored.add(edge)
queue.pop(0)
current_node = max(explored)
return explored
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Read input (into graph)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def main():
"""The main program"""
for i, line in enumerate(INPUT_DATA.strip().split("\n")):
# Initialize graph with n_vertexes
if i == 0:
n_vertexes, n_edges = (int(x) for x in line.split())
graph = GraphUnDirected(n_vertexes, n_edges)
print(graph)
# First line is metadata, continue
continue
# Store the edges on the graph
id_vertex_from, id_vertex_to = (int(x) - 1 for x in line.split())
# print(f"{id_vertex_from} -> {id_vertex_to}")
graph.add_edge(id_vertex_from, id_vertex_to)
# Validate edge number
graph.validate_edge_number()
# Print graph info
for vertex in graph.vertexes:
print(vertex)
print(graph)
# Run algorithm
print()
print("Running main algorithm...")
print()
segments = []
explored = set()
target = set(x.id for x in graph.vertexes)
while target.difference(explored):
current_node = min(target.difference(explored))
segment = graph.connected_components(current_node=current_node)
print(f"segment: {segment}")
segments.append(segment)
explored = explored.union(segment)
print(f"explored: {explored}")
print()
# Print number of segments, only output for this algorithm / problem
print(f"Answer: {len(segments)}")
if __name__ == "__main__":
# Run the main program
main()