-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGraph.py
218 lines (185 loc) · 6.34 KB
/
Graph.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
class Graph(dict):
def __init__(self, vs=[], es=[]):
"""create a new graph. (vs) is a list of vertices;
(es) is a list of edges."""
for v in vs:
self.add_vertex(v)
for e in es:
self.add_edge(e)
def add_vertex(self, v):
"""add (v) to the graph"""
self[v] = {}
def add_edge(self, e):
"""add (e) to the graph by adding an entry in both directions.
If there is already an edge connecting these vertices, the
new edge replaces it.
"""
v, w = e
self[v][w] = e
self[w][v] = e
def get_edge(self, v, w):
"""get the Edge object connecting two vertices."
>>> u, v = Vertex('u'), Vertex('v')
>>> e = Edge(u, v)
>>> g = Graph([u, v], [e])
>>> g.get_edge(u, v)
Edge(Vertex('u'), Vertex('v'))
"""
try:
return self[v][w]
except KeyError:
return None
def remove_edge(self, e):
"""Remove an edge from the graph.
>>> u, v = Vertex('u'), Vertex('v')
>>> e = Edge(u, v)
>>> g = Graph([u, v], [e])
>>> g.remove_edge(e)
>>> g.edges()
[]
"""
v, w = e
del self[v][w]
del self[w][v]
def vertices(self):
"""Return all of the vertices in the graph as a list.
>>> u, v = Vertex('u'), Vertex('v')
>>> e = Edge(u, v)
>>> g = Graph([u, v], [e])
>>> set(g.vertices()) == set([u, v])
True
"""
return list(self.keys())
def edges(self):
"""Return all of the edges in the graph as a list.
>>> u, v = Vertex('u'), Vertex('v')
>>> e = Edge(u, v)
>>> g = Graph([u, v], [e])
>>> g.edges()
[Edge(Vertex('u'), Vertex('v'))]
"""
result = set()
for v in self.keys():
for w in self[v]:
result.add(self[v][w])
return list(result)
def is_connected(self):
"""Return True if the graph is connected.
>>> u, v, w, z = [Vertex(c) for c in "uvwz"]
>>> g = Graph([u, v, w, z], [Edge(u,v), Edge(v, w), Edge(v, z)])
>>> g.is_connected()
True
>>> g2 = Graph([u, v, w, z], [Edge(u,v), Edge(w, z)])
>>> g2.is_connected()
False
>>> g3 = Graph([u, v, w, z], [Edge(u,v), Edge(v, w), Edge(w, z)])
>>> g3.is_connected()
True
>>> g4 = Graph([Vertex(n) for n in range(0, 256)])
>>> g4.add_all_edges()
>>> g4.is_connected()
True
"""
for v in self.vertices():
v.visited = False
queue = [self.vertices()[0]]
while len(queue) > 0:
v = queue.pop()
v.visited = True
for w in self.out_vertices(v):
if not w.visited:
queue.append(w)
return all(u.visited for u in self.vertices())
def out_vertices(self, v):
"""Return all of the vertices connected to a given
vertex.
>>> u, v, w = Vertex('u'), Vertex('v'), Vertex('w')
>>> e, f = Edge(u, v), Edge(u, w)
>>> g = Graph([u, v, w], [e, f])
>>> set(g.out_vertices(u)) == set([v, w])
True
"""
return list(self[v].keys())
def out_edges(self, v):
"""Return all of the edges connected to a given vertex.
>>> u, v, w = Vertex('u'), Vertex('v'), Vertex('w')
>>> e, f = Edge(u, v), Edge(u, w)
>>> g = Graph([u, v, w], [e, f])
>>> set(g.out_edges(u)) == set([e, f])
True
"""
return list(self[v].values())
def add_all_edges(self):
"""Connect every vertex in the graph.
>>> u, v, w = Vertex('u'), Vertex('v'), Vertex('w')
>>> g = Graph([u, v, w])
>>> g.add_all_edges()
>>> set(g.out_vertices(u)) == set([v, w])
True
>>> set(g.out_vertices(v)) == set([u, w])
True
>>> set(g.out_vertices(w)) == set([u, v])
True
"""
for v in self.vertices():
for w in self.vertices():
if v != w:
self.add_edge(Edge(v, w))
def add_regular_edges(self, degree):
"""Connect vertices in such a way that the graph is regular.
>>> u,v,w,x = [Vertex(c) for c in "uvwx"]
>>> g1 = Graph([u,v,w,x])
>>> g1.add_regular_edges(1)
>>> list(len(g1.out_vertices(n)) for n in g1.vertices())
[1, 1, 1, 1]
>>> g2 = Graph([u,v,w,x])
>>> g2.add_regular_edges(2)
>>> list(len(g2.out_vertices(n)) for n in g2.vertices())
[2, 2, 2, 2]
>>> g3 = Graph([u,v,w])
>>> g3.add_regular_edges(2)
>>> list(len(g3.out_vertices(n)) for n in g3.vertices())
[2, 2, 2]
>>> g4 = Graph([u,v,w,x])
>>> g4.add_regular_edges(3)
>>> list(len(g4.out_vertices(n)) for n in g4.vertices())
[3, 3, 3, 3]
>>> g5 = Graph([Vertex(c) for c in "abcdefgh"])
>>> g5.add_regular_edges(6)
>>> all(list(len(g5.out_vertices(n)) == 6 for n in g5.vertices()))
True
>>> g6 = Graph([u, v])
>>> g6.add_regular_edges(4)
Traceback (most recent call last):
ValueError: Invalid graph for degree 4
"""
vs = self.vertices()
size = len(vs)
if size < degree + 1 or size * degree % 2 == 1:
raise ValueError, "Invalid graph for degree %d" % degree
for v in vs:
try:
ws = self.vertices()
ws.remove(v)
for i in range(0, degree - len(self.out_vertices(v))):
w = ws.pop()
while w in self.out_vertices(v) or len(self.out_vertices(w)) >= degree:
w = ws.pop()
self.add_edge(Edge(v, w))
except IndexError:
pass
class Vertex(object):
def __init__(self, label=''):
self.label = label
def __repr__(self):
return 'Vertex(%s)' % repr(self.label)
__str__ = __repr__
class Edge(tuple):
def __new__(cls, e1, e2):
return tuple.__new__(cls, (e1, e2))
def __repr__(self):
return 'Edge(%s, %s)' % (repr(self[0]), repr(self[1]))
__str__ = __repr__
if __name__ == "__main__":
import doctest
doctest.testmod()