forked from jwasham/practice-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirected_graph_list.py
More file actions
349 lines (273 loc) · 9.06 KB
/
directed_graph_list.py
File metadata and controls
349 lines (273 loc) · 9.06 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
340
341
342
343
344
345
346
347
348
349
import queue
import copy
class DirectedGraph(object):
"""
Directed Graph, with graph represented as an adjacency list
"""
def __init__(self):
self.adjacency_list = {}
def add_edge(self, source, destination):
"""
Adds an edge defined by vertices source and destination
:param source:
:param destination:
:return:
"""
if source not in self.adjacency_list:
self.adjacency_list[source] = set()
self.adjacency_list[source].add(destination)
def get_vertex(self):
"""
Generator for returning the next vertex from the adjacency list
:return:
"""
for v in self.adjacency_list:
yield v
def get_neighbor(self, vertex):
"""
Generator for returning the next vertex adjacent to the given vertex
:param vertex:
:return:
"""
if vertex in self.adjacency_list:
for u in self.adjacency_list[vertex]:
yield u
def get_reverse_neighbor(self, vertex):
"""
Generator for returning the reversed edge neighbor to the given vertex (parent)
:param vertex:
:return:
"""
reversed_list = {}
for v, u in self.adjacency_list.items():
for w in u:
if w not in reversed_list:
reversed_list[w] = set()
reversed_list[w].add(v)
if vertex in reversed_list:
for u in reversed_list[vertex]:
yield u
def dfs(self):
"""
Computes the initial source vertices for each connected component
and the parents for each vertex as determined through depth-first-search
:return: initial source vertices for each connected component, parents for each vertex
:rtype: set, dict
"""
parents = {}
components = set()
to_visit = []
for vertex in self.get_vertex():
if vertex not in parents:
components.add(vertex)
else:
continue
to_visit.append(vertex)
while to_visit:
v = to_visit.pop()
for neighbor in self.get_neighbor(v):
if neighbor not in parents:
parents[neighbor] = v
to_visit.append(neighbor)
return components, parents
def bfs(self):
"""
Computes the the parents for each vertex as determined through breadth-first search
:return: parents for each vertex
:rtype: dict
"""
parents = {}
to_visit = queue.Queue()
for vertex in self.get_vertex():
to_visit.put(vertex)
while not to_visit.empty():
v = to_visit.get()
for neighbor in self.get_neighbor(v):
if neighbor not in parents:
parents[neighbor] = v
to_visit.put(neighbor)
return parents
def contains_cycle(self):
"""
Determines if one of the connected components contains a cycle
:return: true if one of the connected components contains a cycle
:rtype: bool
"""
contains_cycle = False
STATUS_STARTED = 1
STATUS_FINISHED = 2
for vertex in self.get_vertex():
statuses = {}
to_visit = [vertex]
while to_visit and not contains_cycle:
v = to_visit.pop()
if v in statuses:
if statuses[v] == STATUS_STARTED:
statuses[v] = STATUS_FINISHED
else:
statuses[v] = STATUS_STARTED
to_visit.append(v) # add to stack again to signal vertex has finished DFS
for u in self.get_neighbor(v):
if u in statuses:
if statuses[u] == STATUS_STARTED:
contains_cycle = True
break
else:
to_visit.append(u)
if contains_cycle:
break
return contains_cycle
def topological_sort(self):
"""
Determines the priority of vertices to be visited.
:return:
"""
STATUS_STARTED = 1
STATUS_FINISHED = 2
order = []
statuses = {}
assert (not self.contains_cycle())
for vertex in self.get_vertex():
to_visit = [vertex]
while to_visit:
v = to_visit.pop()
if v in statuses:
if statuses[v] == STATUS_STARTED:
statuses[v] = STATUS_FINISHED
order.append(v)
else:
statuses[v] = STATUS_STARTED
to_visit.append(v) # add to stack again to signal vertex has finished DFS
for u in self.get_neighbor(v):
if u not in statuses:
to_visit.append(u)
order.reverse()
return order
def strongly_connected_components(self):
"""
Compute the vertices in the strongly connected components
:return list of lists, one for each component's vertices:
"""
stack = self.scc_dfs_forward_pass()
components = self.scc_dfs_reverse_pass(stack)
return components
def scc_dfs_forward_pass(self):
stack = []
visited = set()
for v in self.get_vertex():
self.dfs_forward(v, stack, visited)
return stack
def dfs_forward(self, vertex, stack, visited):
if vertex not in visited:
visited.add(vertex)
for u in self.get_neighbor(vertex):
self.dfs_forward(u, stack, visited)
stack.append(vertex)
def scc_dfs_reverse_pass(self, stack):
components = []
visited = set()
while stack:
v = stack.pop()
if v not in visited:
component = []
self.dfs_reverse(v, component, visited)
component.reverse()
components.append(component)
return components
def dfs_reverse(self, vertex, component, visited):
if vertex not in visited:
visited.add(vertex)
component.append(vertex)
for u in self.get_reverse_neighbor(vertex):
self.dfs_reverse(u, component, visited)
def get_test_graph_1():
dg = DirectedGraph()
dg.add_edge(0, 1)
dg.add_edge(0, 5)
dg.add_edge(1, 2)
dg.add_edge(2, 4)
dg.add_edge(2, 6)
dg.add_edge(3, 2)
dg.add_edge(5, 8)
dg.add_edge(6, 5)
dg.add_edge(7, 5)
dg.add_edge(7, 5)
return dg
def get_test_graph_2():
dg_small = DirectedGraph()
dg_small.add_edge(2, 1)
dg_small.add_edge(4, 5)
dg_small.add_edge(0, 1)
dg_small.add_edge(1, 4)
dg_small.add_edge(1, 3)
return dg_small
def get_test_graph_3():
dg_other = DirectedGraph()
dg_other.add_edge(3, 11)
dg_other.add_edge(5, 2)
dg_other.add_edge(2, 4)
dg_other.add_edge(2, 7)
dg_other.add_edge(8, 11)
dg_other.add_edge(4, 7)
dg_other.add_edge(7, 8)
return dg_other
def get_test_graph_4():
"""
Returns graph containing a cycle
:return:
"""
dg = copy.copy(get_test_graph_1())
dg.add_edge(8, 0) # creates cycle
return dg
def get_test_graph_5():
"""
Returns a graph with 3 cycles and 5 strongly connected components
:return:
"""
dg = DirectedGraph()
dg.add_edge(0, 2)
dg.add_edge(1, 3)
dg.add_edge(3, 2)
dg.add_edge(2, 1)
dg.add_edge(4, 5)
dg.add_edge(5, 6)
dg.add_edge(6, 4)
dg.add_edge(3, 5)
dg.add_edge(7, 5)
dg.add_edge(8, 10)
dg.add_edge(10, 11)
dg.add_edge(11, 9)
dg.add_edge(9, 8)
return dg
def test_dfs():
dg1 = get_test_graph_1()
c1, p1 = dg1.dfs()
assert (c1 == {0, 3, 7})
assert (p1 == {1: 0, 2: 1, 4: 2, 5: 0, 6: 2, 8: 5})
def test_bfs():
dg1 = get_test_graph_1()
p1 = dg1.bfs()
assert (p1 == {1: 0, 2: 1, 4: 2, 5: 0, 6: 2, 8: 5})
def test_contains_cycle():
assert (get_test_graph_1().contains_cycle() == False)
assert (get_test_graph_2().contains_cycle() == False)
assert (get_test_graph_3().contains_cycle() == False)
assert (get_test_graph_4().contains_cycle() == True)
def test_topological_sort():
assert (get_test_graph_1().topological_sort() == [7, 3, 0, 1, 2, 4, 6, 5, 8])
assert (get_test_graph_2().topological_sort() == [2, 0, 1, 3, 4, 5])
assert (get_test_graph_3().topological_sort() == [5, 3, 2, 4, 7, 8, 11])
def test_strongly_connected_components():
dg = get_test_graph_5()
assert (dg.contains_cycle())
components = dg.strongly_connected_components()
assert (components == [[10, 11, 9, 8], [7], [0], [1, 3, 2], [6, 4, 5]])
def main():
test_dfs()
test_bfs()
test_contains_cycle()
test_topological_sort()
test_strongly_connected_components()
print("Tests complete.")
if __name__ == "__main__":
main()