-
Notifications
You must be signed in to change notification settings - Fork 0
/
digraph.py
361 lines (285 loc) · 9.27 KB
/
digraph.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
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
350
351
352
353
354
355
356
357
358
359
360
361
"""
Quiz 5. Joseph Michael Blouin - 1290002, Feb 8th 2013
Graph module for undirected graphs.
"""
import random
try:
import display
except:
print("Warning: failed to load display module. Graph drawing will not work.")
class Digraph:
"""
Directed graph. The vertices must be immutable.
To create an empty graph:
>>> G = Digraph()
>>> (G.num_vertices(), G.num_edges())
(0, 0)
To create a circular graph with 3 vertices:
>>> G = Digraph([(1, 2), (2, 3), (3, 1)])
>>> (G.num_vertices(), G.num_edges())
(3, 3)
"""
def __init__(self, edges = None):
self._tosets = {}
self._fromsets = {}
if edges:
for e in edges: self.add_edge(e)
def __repr__(self):
return "Digraph({}, {})".format(self.vertices(), self.edges())
def add_vertex(self, v):
"""
Adds a vertex to the graph. It starts with no edges.
>>> G = Digraph()
>>> G.add_vertex(1)
>>> G.vertices() == {1}
True
"""
if v not in self._tosets:
self._tosets[v] = set()
self._fromsets[v] = set()
def add_edge(self, e):
"""
Adds an edge to graph. If vertices in the edge do not exist, it adds them.
>>> G = Digraph()
>>> G.add_vertex(1)
>>> G.add_vertex(2)
>>> G.add_edge((1, 2))
>>> G.add_edge((2, 1))
>>> G.add_edge((3, 4))
>>> G.add_edge((1, 2))
>>> G.num_edges()
3
>>> G.num_vertices()
4
"""
# Adds the vertices (in case they don't already exist)
for v in e:
self.add_vertex(v)
# Add the edge
self._tosets[e[0]].add(e[1])
self._fromsets[e[1]].add(e[0])
def edges(self):
"""
Returns the set of edges in the graph as ordered tuples.
"""
return { (v, w) for v in self._tosets for w in self._tosets[v] }
def vertices(self):
"""
Returns the set of vertices in the graph.
"""
return set(self._tosets.keys())
def draw(self, filename, attr = {}):
"""
Draws the graph into a dot file.
"""
display.write_dot_desc((self.vertices(), self.edges()), filename, attributes=attr, graphtype='digraph')
def num_edges(self):
m = 0
for v in self._tosets:
m += len(self._tosets[v])
return m
def num_vertices(self):
"""
Returns the number of vertices in the graph.
"""
return len(self._tosets)
def adj_to(self, v):
"""
Returns the set of vertices that contain an edge from v.
>>> G = Digraph()
>>> for v in [1, 2, 3]: G.add_vertex(v)
>>> G.add_edge((1, 3))
>>> G.add_edge((1, 2))
>>> G.adj_to(3) == set()
True
>>> G.adj_to(1) == { 2, 3 }
True
"""
return self._tosets[v]
def adj_from(self, v):
"""
Returns the set of vertices that contain an edge to v.
>>> G = Digraph()
>>> G.add_edge((1, 3))
>>> G.add_edge((2, 3))
>>> G.adj_from(1) == set()
True
>>> G.adj_from(3) == { 1, 2 }
True
"""
return self._fromsets[v]
def is_path(self, path):
"""
Returns True if the list of vertices in the argument path are a
valid path in the graph. Returns False otherwise.
Base Test Case
>>> G = Digraph([(1, 2), (2, 3), (2, 4), (1, 5), (2, 5), (4, 5), (5, 2)])
>>> G.is_path([1, 5, 2, 4, 5])
True
>>> G.is_path([1, 5, 4, 2])
False
Test a path containing multiples of the same edge
>>> G.is_path([5, 2, 5, 2])
False
Test a path that could be incorrectly perceived as having multiples of the same edge
IE test distinguishment between (x, y) and (y, x)
>>> G.is_path([5, 2, 5])
True
Test Invalid first step
>>> G.is_path([4, 2, 5, 2])
False
Test Invalid last step
>>> G.is_path([1, 2, 3, 5])
False
Test bad vertex
>>> G.is_path([10, 2, 4, 5])
False
>>> G.is_path([2, 4, 5, 10])
False
>>> G.is_path([2, 4, 10, 5])
False
Test empty path
>>> G.is_path([])
False
"""
if not len(path): return False
# Get the set of edges in this graph (important that this be a copy)
edges = [v for v in self.edges()]
# Make sure the path can actually be commuted
for i in range(1, len(path)):
# If the edge exists in the edges list, then it has not appeared before
# and an edge exists betweeen the vertices
if (path[i - 1], path[i]) in edges:
# This edge is valid. Remove it from the set of edges so it cannot appear again
edges.remove((path[i - 1], path[i]))
else:
# This edge is invalid either because no edge exists between these vertices or
# it has already been traversed
return False
return True
def random_graph(n, m):
"""
Make a random Digraph with n vertices and m edges.
>>> G = random_graph(10, 5)
>>> G.num_edges()
5
>>> G.num_vertices()
10
>>> G = random_graph(1, 1)
Traceback (most recent call last):
...
ValueError: For 1 vertices, you wanted 1 edges, but can only have a maximum of 0
"""
G = Digraph()
for v in range(n):
G.add_vertex(v)
max_num_edges = n * (n-1)
if m > max_num_edges:
raise ValueError("For {} vertices, you wanted {} edges, but can only have a maximum of {}".format(n, m, max_num_edges))
while G.num_edges() < m:
G.add_edge(random.sample(range(n), 2))
return G
def spanning_tree(G, start):
"""
Runs depth-first-search on G from vertex start to create a spanning tree.
"""
visited = set()
todo = [ (start, None) ]
T = Digraph()
while todo:
(cur, e) = todo.pop()
if cur in visited: continue
visited.add(cur)
if e: T.add_edge(e)
for n in G.adj_to(cur):
if n not in visited:
todo.append((n, (cur, n)))
return T
def shortest_path(G, source, dest):
"""
Returns the shortest path from vertex source to vertex dest or None if there is no such path.
Essentially Dijkstra's Algorithm.
Note: Although it would require only a couple simple modifications to the code I decided
to return null if source == dest although it could be desired to find the shortest path
that leads back to the source without returning a path of zero length if such a path is possible.
>>> G = Digraph([(1, 2), (2, 3), (3, 4), (4, 5), (1, 6), (3, 6), (6, 7)])
>>> path = shortest_path(G, 1, 7)
>>> path
[1, 6, 7]
>>> G.is_path(path)
True
Test a source vertex not in the graph
>>> shortest_path(G, 10, 1) is None
True
Test a destination vertex not in the graph
>>> shortest_path(G, 1, 10) is None
True
Test a path that is not possible
>>> shortest_path(G, 4, 6) is None
True
Test the reverse of a possible path
>>> shortest_path(G, 7, 1) is None
True
Test a path where source == dest
>>> shortest_path(G, 1, 1) is None
True
Test on a graph with zero/one/two vertices
>>> G2 = Digraph()
>>> shortest_path(G2, 1, 2) is None
True
>>> G2.add_vertex(1)
>>> shortest_path(G2, 1, 2) is None
True
>>> G2.add_edge([1,2])
>>> shortest_path(G2, 1, 2)
[1, 2]
>>> shortest_path(G2, 2, 1) is None
True
"""
# Check that source exists within G and that source != dest
if source not in G.vertices() or source == dest: return
# Get the spanning tree from this node
tree = spanning_tree(G, source)
# Make sure that the dest is in the sources' spanning tree
if dest not in tree.vertices(): return
# Begin searching the spanning tree
queue = [ source ]
# List of previous nodes
previous = {}
while queue:
currentElement = queue.pop()
# Are we there yet?!
if dest == currentElement:
# Walk backwards in history until we get to the source node
path = [ dest ]
while path[-1] != source:
# Use path.append for speed and reverse later
path.append( previous[ path[-1] ] )
# The path is now backwards, reverse and return
path.reverse()
return path
# Find the children of this element
# and append to the queue with history
for child in [child for child in tree.edges() if child[0] == currentElement]:
previous[child[1]] = currentElement
queue.append(child[1])
def compress(walk):
"""
Remove cycles from a walk to create a path.
>>> compress([1, 2, 3, 4])
[1, 2, 3, 4]
>>> compress([1, 3, 0, 1, 6, 4, 8, 6, 2])
[1, 6, 2]
"""
lasttime = {}
for (i,v) in enumerate(walk):
lasttime[v] = i
rv = []
i = 0
while (i < len(walk)):
rv.append(walk[i])
i = lasttime[walk[i]]+1
return rv
if __name__ == "__main__":
import doctest
doctest.testmod()