-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCubicHam.py
More file actions
334 lines (287 loc) · 11.9 KB
/
CubicHam.py
File metadata and controls
334 lines (287 loc) · 11.9 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
"""CubicHam.py
Generate all Hamiltonian cycles in graphs of maximum degree three.
D. Eppstein, April 2004.
"""
import unittest
from Graphs import *
from Biconnectivity import isBiconnected
from CardinalityMatching import matching
from Util import arbitrary_item, map_to_constant
def HamiltonianCycles(G):
"""
Generate a sequence of all Hamiltonian cycles in graph G.
G should be represented in such a way that "for v in G" loops through
the vertices, and "G[v]" produces a collection of neighbors of v; for
instance, G may be a dictionary mapping vertices to lists of neighbors.
Each cycle is returned as a graph in a similar representation, and
should not be modified by the caller. Running time is O(2^{3n/8}) as
analyzed in my paper, The Traveling Salesman Problem for Cubic Graphs.
"""
# Check input and copy it so we can modify the copy.
# In the copied graph G, G[v][w] is True when vw is an original edge
# of the input, and False when it was produced by a contraction.
if not G or not isUndirected(G) or maxDegree(G) > 3:
raise ValueError("HamiltonianCycles input must be undirected degree three graph")
if minDegree(G) < 2:
return
G = copyGraph(G,map_to_constant(True))
# Subgraph of forced edges in the input
forced_in_input = {v:{} for v in G}
# Subgraph of forced edges in current G
forced_in_current = {v:{} for v in G}
# List of vertices with degree two
degree_two = [v for v in G if len(G[v]) == 2]
# Collection of vertices with forced edges
forced_vertices = {}
# Reuse results of call to matching() to speed up subsequent calls
previous_matching = {}
# The overall backtracking algorithm is implemented by means of
# a stack of actions. At each step we pop the most recent action
# off the stack and call it. Each stacked function should return None or False
# normally, or True to signal that we have found a Hamiltonian cycle.
# Whenever we modify the graph, we push an action undoing that modification.
# Below are definitions of actions and action-related functions.
def remove(v,w):
"""Remove edge v,w from edges of G."""
was_original = G[v][w]
del G[v][w],G[w][v]
was_forced = w in forced_in_current[v]
if was_forced:
del forced_in_current[v][w],forced_in_current[w][v]
def unremove():
G[v][w] = G[w][v] = was_original
if was_forced:
forced_in_current[v][w] = forced_in_current[w][v] = True
actions.append(unremove)
def now_degree_two(v):
"""Discover that changing G has caused v's degree to become two."""
degree_two.append(v)
def not_degree_two():
top = degree_two.pop()
assert v == top
actions.append(not_degree_two)
def safely_remove(v,w):
"""
Remove edge v,w and update degree two data structures.
Returns True if successful, False if found a contradiction.
"""
assert w in G[v]
if w in forced_in_current[v] or len(G[v]) < 3 or len(G[w]) < 3:
return False
remove(v,w)
now_degree_two(v)
now_degree_two(w)
return True
def remove_third_leg(v):
"""
Check if v has two forced edges and if so remove unforced one.
Returns True if successful, False if found a contradiction.
"""
if len(G[v]) != 3 or len(forced_in_current[v]) != 2:
return True
w = [x for x in G[v] if x not in forced_in_current[v]][0]
if len(G[w]) <= 2:
return False
return safely_remove(v,w)
def force(v,w):
"""
Add edge v,w to forced edges.
Returns True if successful, False if found a contradiction.
"""
if w in forced_in_current[v]:
return True # Already forced, nothing to do
if len(forced_in_current[v]) > 2 or len(forced_in_current[w]) > 2:
return False # Three incident forced => no cycle exists
if w not in G[v] or v not in G[w]:
return False # Removed from G after we decided to force it?
forced_in_current[v][w] = forced_in_current[w][v] = True
not_previously_forced = [x for x in (v,w) if x not in forced_vertices]
for x in not_previously_forced:
forced_vertices[x] = True
was_original = G[v][w]
if was_original:
forced_in_input[v][w] = forced_in_input[w][v] = True
def unforce():
"""Undo call to force."""
for x in not_previously_forced:
del forced_vertices[x]
del forced_in_current[v][w],forced_in_current[w][v]
if was_original:
del forced_in_input[v][w],forced_in_input[w][v]
actions.append(unforce)
return remove_third_leg(v) and remove_third_leg(w) and \
force_into_triangle(v,w) and force_into_triangle(w,v) and \
force_from_triangle(v,w)
def force_into_triangle(v,w):
"""
After v,w has been added to forced edges, check if w
belongs to a triangle, and if so force the opposite edge.
Returns True if successful, False if found a contradiction.
"""
if len(G[w]) != 3:
return True
x,y = [z for z in G[w] if z != v]
if y not in G[x]:
return True
return force(x,y)
def force_from_triangle(v,w):
"""
After v,w has been added to forced edges, check whether it
belongs to a triangle, and if so force the opposite edge.
Returns True if successful, False if found a contradiction.
"""
for u in list(G[v]): # Use list to avoid dict changes
if u in G[w]:
if len(G[u]) < 3:
return len(G) == 3 # deg=2 only ok if 3 verts left
x = [y for y in G[u] if y != v and y != w][0]
if not force(u,x):
return False
return True
def contract(v):
"""
Contract out degree two vertex.
Returns True if cycle should be reported, False or None otherwise.
Appends recursive search of contracted graph to action stack.
"""
assert len(G[v]) == 2
u,w = G[v]
if w in G[u]: # About to create parallel edge?
if len(G) == 3: # Graph is a triangle?
return force(u,v) and force(v,w) and force(u,w)
if not safely_remove(u,w):
return None # Unable to remove uw, no cycles exist
if not force(u,v) or not force(v,w):
return None # Forcing the edges led to a contradiction
remove(u,v)
remove(v,w)
G[u][w] = G[w][u] = False
forced_in_current[u][w] = forced_in_current[w][u] = True
del G[v],forced_vertices[v]
def uncontract():
del G[u][w],G[w][u]
del forced_in_current[u][w],forced_in_current[w][u]
forced_vertices[v] = True
G[v] = {}
actions.append(uncontract)
if force_from_triangle(u,w): # Contraction may have made a triangle
actions.append(main) # Search contracted graph recursively
def handle_degree_two():
"""
Handle case that the graph has a degree two vertex.
Returns True if cycle should be reported, False or None otherwise.
Appends recursive search of contracted graph to action stack.
"""
v = degree_two.pop()
def unpop():
degree_two.append(v)
actions.append(unpop)
return contract(v)
def main():
"""
Main event dispatcher.
Returns True if cycle should be reported, False or None otherwise.
Appends recursive search of contracted graph to action stack.
"""
if degree_two:
return handle_degree_two()
# At this point, we are going to branch, and if the time is really
# exponential in the size of the remaining graph we can afford some
# more expensive pruning steps first.
# If graph is Hamiltonian it must be biconnected
if not isBiconnected(G):
return None
# If graph is Hamiltonian, unforced edges must have a perfect matching.
# We jump-start the matching algorithm with our previously computed
# matching (or as much of it as fits the current graph) since that is
# likely to be near-perfect.
unforced = {v:{} for v in G}
for v in G:
for w in G[v]:
if w not in forced_in_current[v]:
unforced[v][w] = True
for v in list(previous_matching.keys()):
if v not in unforced or previous_matching[v] not in unforced[v]:
del previous_matching[v]
M = matching(unforced, previous_matching)
previous_matching.clear()
previous_matching.update(M)
if len(M) != len(G):
return None
# Here with a degree three graph in which the forced edges
# form a matching. Pick an unforced edge adjacent to a
# forced one, if possible, else pick any unforced edge,
# and branch on the chosen edge.
if forced_vertices:
v = arbitrary_item(forced_vertices)
else:
v = arbitrary_item(G)
w = [x for x in G[v] if x not in forced_in_current[v]][0]
def continuation():
"""Here after searching first recursive subgraph."""
if force(v,w):
actions.append(main)
actions.append(continuation)
if safely_remove(v,w):
actions.append(main)
# The main backtracking loop
actions = [main]
while actions:
if actions.pop()():
yield forced_in_input
# If run as "python CubicHam.py", run tests on various small graphs
# and check that the correct number of cycles is generated.
class CubicHamTest(unittest.TestCase):
def check(self,G,N):
"""Make sure G has N Hamiltonian cycles."""
count = 0
for C in HamiltonianCycles(G):
# Count the cycle.
count += 1
# Check that it's a degree-two undirected subgraph.
for v in C:
self.assertEqual(len(C[v]),2)
for w in C[v]:
assert v in G and w in G[v] and v in C[w]
# Check that it connects all vertices.
nreached = 0
x = arbitrary_item(G)
a,b = x,x
while True:
nreached += 1
a,b = b,[z for z in C[b] if z != a][0]
if b == x:
break
self.assertEqual(nreached,len(G))
# Did we find enough cycles?
self.assertEqual(count,N)
def testCube(self):
"""Cube has six Hamiltonian cycles."""
cube = {i:(i^1,i^2,i^4) for i in range(8)}
self.check(cube,6)
def twistedLadder(self,n):
"""Connect opposite vertices on an even length cycle."""
return {i:((i+1)%n,(i-1)%n,(i+n//2)%n) for i in range(n)}
def testEvenTwistedLadders(self):
"""twistedLadder(4n) has 2n+1 Hamiltonian cycles."""
for n in range(4,50,4):
self.check(self.twistedLadder(n),n//2+1)
def testOddTwistedLadders(self):
"""twistedLadder(4n+2) has 2n+4 Hamiltonian cycles."""
for n in range(6,50,4):
self.check(self.twistedLadder(n),n//2+3)
def truncate(self,G):
"""Replace each vertex of G by a triangle and return the result."""
return {(v,w):{(v,u) for u in G[v] if u != w} | {(w,v)}
for v in G for w in G[v]}
def testSierpinski(self):
"""
Sierpinski triangle like graphs formed by repeated truncation
of K_4 should all have exactly three Hamiltonian cycles.
"""
G = self.twistedLadder(4) # Complete graph on four vertices
for i in range(3):
G = self.truncate(G)
self.check(G,3)
if __name__ == "__main__":
unittest.main()