Debian Codegraph @

texk/web2c/cwebdir/tests/ham-sorted.tex

raw
1 \input ctwimac
2 \datethis
3 \def\title{HAM}
4
5
6
7
8 \N{1}{1}
9 Hamiltonian cycles. This program finds all Hamiltonian cycles of
10 an undirected graph. [It's a slight revision of the program published
11 in my paper ``Mini-indexes for literate programs,'' {\sl Software---Concepts
12 and Tools\/ \bf15} (1994), 2--11.] The input graph should be in
13 Stanford GraphBase format, and should be named on the command line
14 as, for example, \.{foo.gb}. An optional second command-line parameter
15 is a modulus~$m$, which causes every $m$th solution to be printed.
16
17 We use a utility field to record the vertex degrees.
18
19 \Y\B\4\D$\\{deg}$ \5
20 $\|u.{}$\|I\par
21 \Y\B\8\#\&{include} \.{"gb\_graph.h"}\C{ the GraphBase data structures }\6
22 \8\#\&{include} \.{"gb\_save.h"}\C{ \PB{\\{restore\_graph}} }\7
23 \1\1\&{int} \\{main}(\&{int} \\{argc}${},\39{}$\&{char} ${}{*}\\{argv}[\,])\2%
24 \2{}$\6
25 ${}\{{}$\1\6
26 \&{Graph} ${}{*}\|g;{}$\6
27 \&{Vertex} ${}{*}\|x,\39{*}\|y,\39{*}\|z,\39{*}\\{tmax};{}$\6
28 \&{register} \&{Vertex} ${}{*}\|t,\39{*}\|u,\39{*}\|v;{}$\6
29 \&{register} \&{Arc} ${}{*}\|a,\39{*}\\{aa};{}$\6
30 \&{register} \&{int} \|d;\6
31 \&{Arc} ${}{*}\|b,\39{*}\\{bb};{}$\6
32 \&{int} \\{count}${}\K\T{0};{}$\6
33 \&{int} \\{dmin}${},\39\\{modulus};{}$\7
34 \X2:Process the command line, inputting the graph\X;\6
35 \X3:Prepare \PB{\|g} for backtracking, and find a vertex \PB{\|x} of minimum
36 degree\X;\6
37 \&{for} ${}(\|v\K\|g\MG\\{vertices};{}$ ${}\|v<\|g\MG\\{vertices}+\|g\MG\|n;{}$
38 ${}\|v\PP){}$\1\5
39 ${}\\{printf}(\.{"\ \%ld"},\39\|v\MG\\{deg});{}$\2\6
40 \\{printf}(\.{"\\n"});\C{ TEMPORARY CHECK }\6
41 \&{if} ${}(\|x\MG\\{deg}<\T{2}){}$\5
42 ${}\{{}$\1\6
43 ${}\\{printf}(\.{"The\ minimum\ degree\ }\)\.{is\ \%ld\ (vertex\ \%s)!\\}\)%
44 \.{n"},\39\|x\MG\\{deg},\39\|x\MG\\{name});{}$\6
45 \&{return} ${}{-}\T{1};{}$\6
46 \4${}\}{}$\2\6
47 \&{for} ${}(\|b\K\|x\MG\\{arcs};{}$ ${}\|b\MG\\{next};{}$ ${}\|b\K\|b\MG%
48 \\{next}){}$\1\6
49 \&{for} ${}(\\{bb}\K\|b\MG\\{next};{}$ \\{bb}; ${}\\{bb}\K\\{bb}\MG\\{next}){}$%
50 \5
51 ${}\{{}$\1\6
52 ${}\|v\K\|b\MG\\{tip};{}$\6
53 ${}\|z\K\\{bb}\MG\\{tip};{}$\6
54 \X4:Find all simple paths of length \PB{$\|g\MG\|n-\T{2}$} from \PB{\|v} to %
55 \PB{\|z}, avoiding \PB{\|x}\X;\6
56 \4${}\}{}$\2\2\6
57 ${}\\{printf}(\.{"Altogether\ \%d\ solut}\)\.{ions.\\n"},\39\\{count});{}$\6
58 \&{for} ${}(\|v\K\|g\MG\\{vertices};{}$ ${}\|v<\|g\MG\\{vertices}+\|g\MG\|n;{}$
59 ${}\|v\PP){}$\1\5
60 ${}\\{printf}(\.{"\ \%ld"},\39\|v\MG\\{deg});{}$\2\6
61 \\{printf}(\.{"\\n"});\C{ TEMPORARY CHECK, SHOULD AGREE WITH FORMER VALUES }\6
62 \4${}\}{}$\2\par
63 \mini
64 \]{GB\_\,GRAPH}10 \&{Arc} =\&{struct}
65 \]{GB\_\,GRAPH}9 \\{arcs} \&{Arc} ${}{*}{}$
66 \]{GB\_\,GRAPH}20 \&{Graph} =\&{struct}
67 \]{GB\_\,GRAPH}8 \|{I} \&{long}
68 \]{GB\_\,GRAPH}20 \|{n} \&{long}
69 \]{GB\_\,GRAPH}9 \\{name} \&{char} ${}{*}{}$
70 \]{GB\_\,GRAPH}10 \\{next} \&{Arc} ${}{*}{}$
71 \]"<stdio.h>" \\{printf} \zip
72 \]{GB\_\,SAVE}4 \\{restore\_graph} \&{Graph} ${}{*}(\,){}$
73 \]{GB\_\,GRAPH}10 \\{tip} \&{Vertex} ${}{*}{}$
74 \]{GB\_\,GRAPH}9 \|{u} \&{util}
75 \]{GB\_\,GRAPH}9 \&{Vertex} =\&{struct}
76 \]{GB\_\,GRAPH}20 \\{vertices} \&{Vertex} ${}{*}{}$
77 }\FI
78
79 \shortpage
80 \M{2}
81 \B\X2:Process the command line, inputting the graph\X${}\E{}$\6
82 \&{if} ${}(\\{argc}>\T{1}){}$\1\5
83 ${}\|g\K\\{restore\_graph}(\\{argv}[\T{1}]){}$;\5
84 \2\&{else}\1\5
85 ${}\|g\K\NULL;{}$\2\6
86 \&{if} ${}(\\{argc}<\T{3}\V\\{sscanf}(\\{argv}[\T{2}],\39\.{"\%d"},\39{\AND}%
87 \\{modulus})\I\T{1}){}$\1\5
88 ${}\\{modulus}\K\T{1000000000};{}$\2\6
89 \&{if} ${}(\R\|g\V\\{modulus}\Z\T{0}){}$\5
90 ${}\{{}$\1\6
91 ${}\\{fprintf}(\\{stderr},\39\.{"Usage:\ \%s\ foo.gb\ [m}\)\.{odulus]\\n"},\39%
92 \\{argv}[\T{0}]);{}$\6
93 ${}\\{exit}({-}\T{1});{}$\6
94 \4${}\}{}$\2\par
95 \U1.
96 \mini
97 \[1 \\{argc} \&{int}
98 \[1 \\{argv} \&{char} ${}{*}[\,]{}$
99 \]"<stdlib.h>" \\{exit} \zip
100 \]"<stdio.h>" \\{fprintf} \zip
101 \[1 \|{g} \&{Graph} ${}{*}{}$
102 \[1 \\{modulus} \&{int}
103 \]{GB\_\,SAVE}4 \\{restore\_graph} \&{Graph} ${}{*}(\,){}$
104 \]"<stdio.h>" \\{sscanf} \zip
105 \]"<stdio.h>" \\{stderr} \zip
106 }\FI
107
108 \M{3}
109 Vertices that have already appeared in the path are ``taken,'' and
110 their \PB{\\{taken}} field is nonzero. Initially we make all those fields zero.
111
112 \Y\B\4\D$\\{taken}$ \5
113 $\|v.{}$\|I\par
114 \Y\B\4\X3:Prepare \PB{\|g} for backtracking, and find a vertex \PB{\|x} of
115 minimum degree\X${}\E{}$\6
116 $\\{dmin}\K\|g\MG\|n;{}$\6
117 \&{for} ${}(\|v\K\|g\MG\\{vertices};{}$ ${}\|v<\|g\MG\\{vertices}+\|g\MG\|n;{}$
118 ${}\|v\PP){}$\5
119 ${}\{{}$\1\6
120 ${}\|v\MG\\{taken}\K\T{0};{}$\6
121 ${}\|d\K\T{0};{}$\6
122 \&{for} ${}(\|a\K\|v\MG\\{arcs};{}$ \|a; ${}\|a\K\|a\MG\\{next}){}$\1\5
123 ${}\|d\PP;{}$\2\6
124 ${}\|v\MG\\{deg}\K\|d;{}$\6
125 \&{if} ${}(\|d<\\{dmin}){}$\1\5
126 ${}\\{dmin}\K\|d,\39\|x\K\|v;{}$\2\6
127 \4${}\}{}$\2\par
128 \U1.
129 \mini
130 \[1 \|{a} \&{register} \&{Arc} ${}{*}{}$
131 \]{GB\_\,GRAPH}9 \\{arcs} \&{Arc} ${}{*}{}$
132 \[1 \|{d} \&{register} \&{int}
133 \[1 \\{deg} =\|u.\|I
134 \[1 \\{dmin} \&{int}
135 \[1 \|{g} \&{Graph} ${}{*}{}$
136 \]{GB\_\,GRAPH}8 \|{I} \&{long}
137 \]{GB\_\,GRAPH}20 \|{n} \&{long}
138 \]{GB\_\,GRAPH}10 \\{next} \&{Arc} ${}{*}{}$
139 \[1 \|{v} \&{register} \&{Vertex} ${}{*}{}$
140 \]{GB\_\,GRAPH}9 \|{v} \&{util}
141 \]{GB\_\,GRAPH}20 \\{vertices} \&{Vertex} ${}{*}{}$
142 \[1 \|{x} \&{Vertex} ${}{*}{}$
143 }\FI
144
145 \N{1}{4}
146 The data structures. I use one simple rule to cut off unproductive
147 branches of the search tree: If one of the vertices we could move to next
148 is adjacent to only one other unused vertex, we must move to it now.
149
150 The moves will be recorded in the vertex array of \PB{\|g}. More precisely, the
151 \PB{\|k}th vertex of the path will be \PB{$\|t\MG\\{vert}$} when \PB{\|t} is
152 the \PB{\|k}th vertex of
153 the graph. If the move was not forced, \PB{$\|t\MG\\{ark}$} will point to the
154 Arc
155 record representing the edge from \PB{$\|t\MG\\{vert}$} to \PB{$(\|t+\T{1})\MG%
156 \\{vert}$}; otherwise
157 \PB{$\|t\MG\\{ark}$} will be \PB{$\NULL$}.
158
159 This program is a typical backtrack program. I am more comfortable doing
160 it with labels and goto statements than with while loops, but some day
161 I may learn my lesson.
162
163 \Y\B\4\D$\\{vert}$ \5
164 $\|w.{}$\|V\par
165 \B\4\D$\\{ark}$ \5
166 $\|x.{}$\|A\par
167 \Y\B\4\X4:Find all simple paths of length \PB{$\|g\MG\|n-\T{2}$} from \PB{\|v}
168 to \PB{\|z}, avoiding \PB{\|x}\X${}\E{}$\6
169 $\|t\K\|g\MG\\{vertices}{}$;\5
170 ${}\\{tmax}\K\|t+\|g\MG\|n-\T{1};{}$\6
171 ${}\|x\MG\\{taken}\K\T{1}{}$;\5
172 ${}\|t\MG\\{vert}\K\|x{}$;\5
173 ${}\|t\MG\\{ark}\K\NULL;{}$\6
174 \4\\{advance}:\5
175 \X5:Increase \PB{\|t} and update the data structures to show that vertex \PB{%
176 \|v} is now taken; \PB{\&{goto} \\{backtrack}} if no further moves are possible%
177 \X;\6
178 \4\\{try\_next}:\5
179 \X7:Look at edge \PB{\|a} and its successors, advancing if it is a valid move%
180 \X;\6
181 \4\\{restore}:\5
182 \X6:Downdate the data structures to the state they were in when level \PB{\|t}
183 was entered\X;\6
184 \4\\{backtrack}:\5
185 \X8:Decrease \PB{\|t}, if possible, and try the next possibility; or \PB{%
186 \&{goto} \\{done}}\X;\6
187 \\{done}:\par
188 \U1.
189 \mini
190 \[1 \|{a} \&{register} \&{Arc} ${}{*}{}$
191 \]{GB\_\,GRAPH}8 \|{A} \&{Arc} ${}{*}{}$
192 \[1 \|{g} \&{Graph} ${}{*}{}$
193 \]{GB\_\,GRAPH}20 \|{n} \&{long}
194 \[1 \|{t} \&{register} \&{Vertex} ${}{*}{}$
195 \[3 \\{taken} =\|v.\|I
196 \[1 \\{tmax} \&{Vertex} ${}{*}{}$
197 \[1 \|{v} \&{register} \&{Vertex} ${}{*}{}$
198 \]{GB\_\,GRAPH}8 \|{V} \&{Vertex} ${}{*}{}$
199 \]{GB\_\,GRAPH}20 \\{vertices} \&{Vertex} ${}{*}{}$
200 \]{GB\_\,GRAPH}9 \|{w} \&{util}
201 \[1 \|{x} \&{register} \&{Vertex} ${}{*}{}$
202 \]{GB\_\,GRAPH}9 \|{x} \&{util}
203 \[1 \|{z} \&{Vertex} ${}{*}{}$
204 }\FI
205
206 \M{5}
207 \B\X5:Increase \PB{\|t} and update the data structures to show that vertex \PB{%
208 \|v} is now taken; \PB{\&{goto} \\{backtrack}} if no further moves are possible%
209 \X${}\E{}$\6
210 $\|t\PP;{}$\6
211 ${}\|t\MG\\{vert}\K\|v;{}$\6
212 ${}\|v\MG\\{taken}\K\T{1};{}$\6
213 \&{if} ${}(\|v\E\|z){}$\5
214 ${}\{{}$\1\6
215 \&{if} ${}(\|t\E\\{tmax}){}$\1\5
216 \X9:Record a solution\X;\2\6
217 \&{goto} \\{backtrack};\6
218 \4${}\}{}$\2\6
219 \&{for} ${}(\\{aa}\K\|v\MG\\{arcs},\39\|y\K\NULL;{}$ \\{aa}; ${}\\{aa}\K\\{aa}%
220 \MG\\{next}){}$\5
221 ${}\{{}$\1\6
222 ${}\|u\K\\{aa}\MG\\{tip};{}$\6
223 ${}\|d\K\|u\MG\\{deg}-\T{1};{}$\6
224 \&{if} ${}(\|d\E\T{1}\W\|u\MG\\{taken}\E\T{0}){}$\5
225 ${}\{{}$\1\6
226 \&{if} (\|y)\1\5
227 \&{goto} \\{restore};\C{ restoration will stop at \PB{\\{aa}} }\2\6
228 ${}\|y\K\|u;{}$\6
229 \4${}\}{}$\2\6
230 ${}\|u\MG\\{deg}\K\|d;{}$\6
231 \4${}\}{}$\2\6
232 \&{if} (\|y)\5
233 ${}\{{}$\1\6
234 ${}\|t\MG\\{ark}\K\NULL;{}$\6
235 ${}\|v\K\|y;{}$\6
236 \&{goto} \\{advance};\6
237 \4${}\}{}$\2\6
238 ${}\|a\K\|v\MG\\{arcs}{}$;\par
239 \U4.
240 \mini
241 \[1 \|{a} \&{register} \&{Arc} ${}{*}{}$
242 \[1 \\{aa} \&{register} \&{Arc} ${}{*}{}$
243 \[4 \\{advance} label
244 \]{GB\_\,GRAPH}9 \\{arcs} \&{Arc} ${}{*}{}$
245 \[4 \\{ark} =\|x.\|A
246 \[4 \\{backtrack} label
247 \[1 \|{d} \&{register} \&{int}
248 \[1 \\{deg} =\|u.\|I
249 \]{GB\_\,GRAPH}10 \\{next} \&{Arc} ${}{*}{}$
250 \[4 \\{restore} label
251 \[1 \|{t} \&{register} \&{Vertex} ${}{*}{}$
252 \[3 \\{taken} =\|v.\|I
253 \]{GB\_\,GRAPH}10 \\{tip} \&{Vertex} ${}{*}{}$
254 \[1 \\{tmax} \&{Vertex} ${}{*}{}$
255 \[1 \|{u} \&{register} \&{Vertex} ${}{*}{}$
256 \[1 \|{v} \&{register} \&{Vertex} ${}{*}{}$
257 \[4 \\{vert} =\|w.\|V
258 \[1 \|{y} \&{Vertex} ${}{*}{}$
259 \[1 \|{z} \&{Vertex} ${}{*}{}$
260 }\FI
261
262 \M{6}
263 \B\X6:Downdate the data structures to the state they were in when level \PB{%
264 \|t} was entered\X${}\E{}$\6
265 \&{for} ${}(\|a\K\|t\MG\\{vert}\MG\\{arcs};{}$ ${}\|a\I\\{aa};{}$ ${}\|a\K\|a%
266 \MG\\{next}){}$\1\5
267 ${}\|a\MG\\{tip}\MG\\{deg}\PP{}$;\2\par
268 \U4.
269 \mini
270 \[1 \|{a} \&{register} \&{Arc} ${}{*}{}$
271 \[1 \\{aa} \&{register} \&{Arc} ${}{*}{}$
272 \]{GB\_\,GRAPH}9 \\{arcs} \&{Arc} ${}{*}{}$
273 \[1 \\{deg} =\|u.\|I
274 \]{GB\_\,GRAPH}10 \\{next} \&{Arc} ${}{*}{}$
275 \[1 \|{t} \&{register} \&{Vertex} ${}{*}{}$
276 \]{GB\_\,GRAPH}10 \\{tip} \&{Vertex} ${}{*}{}$
277 \[4 \\{vert} =\|w.\|V
278 }\FI
279
280 \M{7}
281 \B\X7:Look at edge \PB{\|a} and its successors, advancing if it is a valid move%
282 \X${}\E{}$\6
283 \&{while} (\|a)\5
284 ${}\{{}$\1\6
285 ${}\|v\K\|a\MG\\{tip};{}$\6
286 \&{if} ${}(\|v\MG\\{taken}\E\T{0}){}$\5
287 ${}\{{}$\1\6
288 ${}\|t\MG\\{ark}\K\|a;{}$\6
289 \&{goto} \\{advance};\6
290 \4${}\}{}$\2\6
291 ${}\|a\K\|a\MG\\{next};{}$\6
292 \4${}\}{}$\2\6
293 \4\\{restore\_all}:\5
294 ${}\\{aa}\K\NULL{}$;\C{ all moves tried; we fall through to \PB{\\{restore}} }%
295 \par
296 \U4.
297 \mini
298 \[1 \|{a} \&{register} \&{Arc} ${}{*}{}$
299 \[1 \\{aa} \&{register} \&{Arc} ${}{*}{}$
300 \[4 \\{advance} label
301 \[4 \\{ark} =\|x.\|A
302 \]{GB\_\,GRAPH}10 \\{next} \&{Arc} ${}{*}{}$
303 \[4 \\{restore} label
304 \[1 \|{t} \&{register} \&{Vertex} ${}{*}{}$
305 \[3 \\{taken} =\|v.\|I
306 \]{GB\_\,GRAPH}10 \\{tip} \&{Vertex} ${}{*}{}$
307 \[1 \|{v} \&{register} \&{Vertex} ${}{*}{}$
308 }\FI
309
310 \M{8}
311 \B\X8:Decrease \PB{\|t}, if possible, and try the next possibility; or \PB{%
312 \&{goto} \\{done}}\X${}\E{}$\6
313 $\|t\MG\\{vert}\MG\\{taken}\K\T{0};{}$\6
314 ${}\|t\MM;{}$\6
315 \&{if} ${}(\|t\MG\\{ark}){}$\5
316 ${}\{{}$\1\6
317 ${}\|a\K\|t\MG\\{ark}\MG\\{next};{}$\6
318 \&{goto} \\{try\_next};\6
319 \4${}\}{}$\2\6
320 \&{if} ${}(\|t\E\|g\MG\\{vertices}){}$\1\5
321 \&{goto} \\{done};\2\6
322 \&{goto} \\{restore\_all};\C{ the move was forced }\par
323 \U4.
324 \mini
325 \[1 \|{a} \&{register} \&{Arc} ${}{*}{}$
326 \[4 \\{ark} =\|x.\|A
327 \[4 \\{done} label
328 \[1 \|{g} \&{Graph} ${}{*}{}$
329 \]{GB\_\,GRAPH}10 \\{next} \&{Arc} ${}{*}{}$
330 \[7 \\{restore\_all} label
331 \[1 \|{t} \&{register} \&{Vertex} ${}{*}{}$
332 \[3 \\{taken} =\|v.\|I
333 \[4 \\{try\_next} label
334 \[4 \\{vert} =\|w.\|V
335 \]{GB\_\,GRAPH}20 \\{vertices} \&{Vertex} ${}{*}{}$
336 }\FI
337
338 \M{9}
339 \B\X9:Record a solution\X${}\E{}$\6
340 ${}\{{}$\1\6
341 ${}\\{count}\PP;{}$\6
342 \&{if} ${}(\\{count}\MOD\\{modulus}\E\T{0}){}$\5
343 ${}\{{}$\1\6
344 ${}\\{printf}(\.{"\%d:\ "},\39\\{count});{}$\6
345 \&{for} ${}(\|u\K\|g\MG\\{vertices};{}$ ${}\|u\Z\\{tmax};{}$ ${}\|u\PP){}$\1\5
346 ${}\\{printf}(\.{"\%s\ "},\39\|u\MG\\{vert}\MG\\{name});{}$\2\6
347 \\{printf}(\.{"\\n"});\6
348 \4${}\}{}$\2\6
349 \4${}\}{}$\2\par
350 \U5.
351 \mini
352 \[1 \\{count} \&{int}
353 \[1 \|{g} \&{Graph} ${}{*}{}$
354 \[1 \\{modulus} \&{int}
355 \]{GB\_\,GRAPH}9 \\{name} \&{char} ${}{*}{}$
356 \]"<stdio.h>" \\{printf} \zip
357 \[1 \\{tmax} \&{Vertex} ${}{*}{}$
358 \[1 \|{u} \&{register} \&{Vertex} ${}{*}{}$
359 \[4 \\{vert} =\|w.\|V
360 \]{GB\_\,GRAPH}20 \\{vertices} \&{Vertex} ${}{*}{}$
361 }\FI
362
363 \inx
364 \fin
365 \con