-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_coverage.py
More file actions
339 lines (229 loc) · 8.37 KB
/
graph_coverage.py
File metadata and controls
339 lines (229 loc) · 8.37 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
def parse_graph(input_text: str) -> dict[str, list[str]]:
"""
Create an internal representation of a graph from the given text input.
Supported input formats:
- Each line represents a directed edge between two nodes separated with a space ( `node1 node2` ).
- Each line represents a node and all of its neighbors separated with spaces ( `node1 nb1 nb2 ...` ).
Lines starting with `#` are ignored.
------
Arguments:
input_text: String containing graph information.
------
Returns:
A directed graph in dictionary form where each key is a node and each value is a list of neighbors.
------
Raises:
ValueError: If the input is invalid.
"""
graph = {}
for num, line in enumerate(input_text.strip().split('\n'), start = 1):
line = line.strip()
if not line or line.startswith('#'):
continue
nodes = line.split(' ')
if not nodes:
raise ValueError(f'Invalid input format at line {num}: {line}')
node, neighbors = nodes[0], nodes[1:]
if node not in graph:
graph[node] = []
for neighbor in neighbors:
graph[node].append(neighbor)
if neighbor not in graph:
graph[neighbor] = []
if not graph:
raise ValueError('Invalid input.')
return graph
def format_output(paths: list[list[str]]) -> str | None:
"""
Format the given paths for display on the site.
------
Arguments:
paths: A list of paths.
------
Returns:
A formatted string of the same paths, or None if the given list is empty.
"""
if not paths:
return None
output = f'Num\tLen\tPath\n'
for idx, path in enumerate(paths, start = 1):
pstr = ' → '.join(map(str, path))
output += f'{idx:3}\t{len(path):3}\t{pstr}\n'
return output
def init_computation_state(graph: dict[str, list[str]]) -> dict[str, ...]:
"""
Initialize the computation state for graph coverage.
------
Arguments:
graph: A directed graph, as returned from `parse_graph()`.
------
Returns:
A newly initialized computation state.
"""
return {
'graph' : graph,
'paths' : [],
'iteration' : 0,
'progress' : ''
}
def _step_prime_paths(state: dict[str, ...], max_iters: int = 100) -> dict[str, ...]:
"""
Execute one iteration of the prime paths computation.
------
Arguments:
state: The computation state from the previous step.
max_iters: Maximum number of iterations before raising an error. Set to -1 to remove
the limit. Defaults to 100.
------
Returns:
The updated state.
------
Raises:
RuntimeError: If the maximum number of iterations is reached.
"""
if state['progress'] in ['computed', 'cleaned']:
return state
graph, paths, iteration = state['graph'], state['paths'], state['iteration']
if iteration == 0:
paths = [[node] for node in graph]
iteration += 1
if iteration == max_iters:
raise RuntimeError('Maximum iterations reached.')
made_changes = False
for idx in range(len(paths)):
path = paths[idx]
pcpy = path.copy()
pset = set(path)
if len(pset) != len(path):
continue # Path contains duplicate nodes, skip.
node = path[-1]
neighbors = graph[node]
has_one_neighbor = len(neighbors) == 1
split_path = False
for neighbor in neighbors:
if neighbor in pset and neighbor != path[0]:
continue # path + neighbor creates a cycle, skip.
if has_one_neighbor:
path.append(neighbor)
elif split_path:
paths.append(pcpy + [neighbor])
else:
path.append(neighbor)
split_path = True
made_changes = True
state['paths'], state['iteration'] = paths, iteration
state['progress'] = 'computed' if not made_changes else f'Iteration {iteration}: Found {len(paths)} prime paths.'
return state
def _cleanup_prime_paths(state: dict[str, ...]) -> dict[str, ...]:
"""
Remove all sub-paths from the computed prime paths.
------
Arguments:
state: The state after computing prime paths.
------
Returns:
The final state with prime paths.
"""
if state['progress'] == 'cleaned':
return state
paths = state['paths']
paths = sorted([' '.join(path) for path in paths], key = len)
for i in range(len(paths)):
path = paths[i]
for j in range(i + 1, len(paths)):
if path in paths[j]:
paths[i] = '' # paths[i] is a sub-path of paths[j], remove it.
break
paths = [path.split(' ') for path in paths if path]
paths.sort(key = lambda x: (len(x), x))
state['paths'], state['progress'] = paths, 'cleaned'
return state
def compute_prime_paths(graph: dict[str, list[str]], max_iters: int = 100) -> list[list[str]]:
"""
Compute prime paths for the given directed graph.
------
Arguments:
graph: A dictionary representing a directed graph where each key is a node and each
value is a list of neighbors.
max_iters: Maximum number of iterations before raising an error. Set to -1 to remove
the limit. Defaults to 100.
------
Returns:
A list with all prime paths.
------
Raises:
RuntimeError: If the maximum number of iterations is reached.
"""
state = init_computation_state(graph)
while True:
state = _step_prime_paths(state, max_iters)
if state['progress'] == 'computed':
print('Finished computing potential prime paths.')
break
print(state['progress'])
print('Removing sub-paths...')
state = _cleanup_prime_paths(state)
print('Finished removing sub-paths.')
paths = state['paths']
print(f'\nFound a total of {len(paths)} prime paths:')
print(format_output(paths))
return state['paths']
def _step_edge_pairs(state: dict[str, ...], max_iters: 100):
"""
Execute one iteration of the edge pairs computation.
------
Arguments:
state: The computation state from the previous step.
max_iters: Maximum number of iterations before raising an error. Set to -1 to remove
the limit. Defaults to 100.
------
Returns:
The updated state.
------
Raises:
RuntimeError: If the maximum number of iterations is reached.
"""
if state['progress'] in ['computed', 'cleaned']:
return state
graph, paths, iteration = state['graph'], state['paths'], state['iteration']
if iteration == 0:
state['remaining_nodes'] = list(graph)
iteration += 1
if iteration == max_iters:
raise RuntimeError('Maximum iterations reached.')
node = state['remaining_nodes'][0]
for neighbor1 in graph[node]:
for neighbor2 in graph[neighbor1]:
paths.append([node, neighbor1, neighbor2])
state['remaining_nodes'].pop(0)
state['paths'], state['iteration'] = paths, iteration
state['progress'] = 'computed' if not state['remaining_nodes'] \
else f'Iteration {iteration}: Found {len(paths)} edge pairs.'
return state
def compute_edge_pairs(graph: dict[str, list[str]], max_iters: int = 100) -> list[list[str]]:
"""
Compute edge pairs for the given directed graph.
------
Arguments:
graph: A dictionary representing a directed graph where each key is a node and each
value is a list of neighbors.
max_iters: Maximum number of iterations before raising an error. Set to -1 to remove
the limit. Defaults to 100.
------
Returns:
A list with all edge pairs.
------
Raises:
RuntimeError: If the maximum number of iterations is reached.
"""
state = init_computation_state(graph)
while True:
state = _step_edge_pairs(state, max_iters)
if state['progress'] == 'computed':
print('Finished computing edge pairs.')
break
print(state['progress'])
paths = state['paths']
print(f'\nFound a total of {len(paths)} edge pairs:')
print(format_output(paths))
return state['paths']