| 1 |
\datethis |
| 2 |
@s try x |
| 3 |
@i gb_types.w |
| 4 |
|
| 5 |
@*Hamiltonian cycles. This program finds all Hamiltonian cycles of |
| 6 |
an undirected graph. [It's a slight revision of the program published |
| 7 |
in my paper ``Mini-indexes for literate programs,'' {\sl Software---Concepts |
| 8 |
and Tools\/ \bf15} (1994), 2--11.] The input graph should be in |
| 9 |
Stanford GraphBase format, and should be named on the command line |
| 10 |
as, for example, \.{foo.gb}. An optional second command-line parameter |
| 11 |
is a modulus |m|, which causes every $m$th solution to be printed. |
| 12 |
|
| 13 |
We use a utility field to record the vertex degrees. |
| 14 |
|
| 15 |
@d deg u.I |
| 16 |
|
| 17 |
@c |
| 18 |
#include "gb_graph.h" /* the GraphBase data structures */ |
| 19 |
#include "gb_save.h" /* |restore_graph| */ |
| 20 |
|
| 21 |
main(int argc,char *argv[]) |
| 22 |
{ |
| 23 |
Graph *g; |
| 24 |
Vertex *x,*y,*z, *tmax; |
| 25 |
register Vertex *t,*u,*v; |
| 26 |
register Arc *a,*aa; |
| 27 |
register int d; |
| 28 |
Arc *b,*bb; |
| 29 |
int count=0; |
| 30 |
int dmin,modulus; |
| 31 |
@<Process the command line, inputting the graph@>; |
| 32 |
@<Prepare |g| for backtracking, and find a vertex |x| of minimum degree@>; |
| 33 |
for (v=g->vertices;v<g->vertices+g->n;v++) printf(" %d",v->deg); |
| 34 |
printf("\n"); /* TEMPORARY CHECK */ |
| 35 |
if (x->deg<2) { |
| 36 |
printf("The minimum degree is %d (vertex %s)!\n",x->deg,x->name); |
| 37 |
return -1; |
| 38 |
} |
| 39 |
for (b=x->arcs;b->next;b=b->next) for (bb=b->next;bb;bb=bb->next) { |
| 40 |
v=b->tip; |
| 41 |
z=bb->tip; |
| 42 |
@<Find all simple paths of length |g->n-2| from |v| to |z|, |
| 43 |
avoiding |x|@>; |
| 44 |
} |
| 45 |
printf("Altogether %d solutions.\n",count); |
| 46 |
for (v=g->vertices;v<g->vertices+g->n;v++) printf(" %d",v->deg); |
| 47 |
printf("\n"); /* TEMPORARY CHECK, SHOULD AGREE WITH FORMER VALUES */ |
| 48 |
} |
| 49 |
|
| 50 |
@ @<Process the command line, inputting the graph@>= |
| 51 |
if (argc>1) g=restore_graph(argv[1]);@+ else g=NULL; |
| 52 |
if (argc<3 || sscanf(argv[2],"%d",&modulus)!=1) modulus=1000000000; |
| 53 |
if (!g || modulus<=0) { |
| 54 |
fprintf(stderr,"Usage: %s foo.gb [modulus]\n",argv[0]); |
| 55 |
exit(-1); |
| 56 |
} |
| 57 |
|
| 58 |
@ Vertices that have already appeared in the path are ``taken,'' and |
| 59 |
their |taken| field is nonzero. Initially we make all those fields zero. |
| 60 |
|
| 61 |
@d taken v.I |
| 62 |
|
| 63 |
@<Prepare |g| for backtracking, and find a vertex |x| of minimum degree@>= |
| 64 |
dmin=g->n; |
| 65 |
for (v=g->vertices;v<g->vertices+g->n;v++) { |
| 66 |
v->taken=0; |
| 67 |
d=0; |
| 68 |
for (a=v->arcs;a;a=a->next) d++; |
| 69 |
v->deg=d; |
| 70 |
if (d<dmin) dmin=d,x=v; |
| 71 |
} |
| 72 |
|
| 73 |
@*The data structures. I use one simple rule to cut off unproductive |
| 74 |
branches of the search tree: If one of the vertices we could move to next |
| 75 |
is adjacent to only one other unused vertex, we must move to it now. |
| 76 |
|
| 77 |
The moves will be recorded in the vertex array of |g|. More precisely, the |
| 78 |
|k|th vertex of the path will be |t->vert| when |t| is the |k|th vertex of |
| 79 |
the graph. If the move was not forced, |t->ark| will point to the Arc |
| 80 |
record representing the edge from |t->vert| to |(t+1)->vert|; otherwise |
| 81 |
|t->ark| will be |NULL|. |
| 82 |
|
| 83 |
This program is a typical backtrack program. I am more comfortable doing |
| 84 |
it with labels and goto statements than with while loops, but some day |
| 85 |
I may learn my lesson. |
| 86 |
|
| 87 |
@d vert w.V |
| 88 |
@d ark x.A |
| 89 |
|
| 90 |
@<Find all simple paths of length |g->n-2|...@>= |
| 91 |
t=g->vertices;@+tmax=t+g->n-1; |
| 92 |
x->taken=1;@+t->vert=x; |
| 93 |
t->ark=NULL; |
| 94 |
advance: @<Increase |t| and update the data structures to show that |
| 95 |
vertex |v| is now taken; |goto backtrack| if no further |
| 96 |
moves are possible@>; |
| 97 |
try: @<Look at edge |a| and its successors, advancing if it is a valid move@>; |
| 98 |
restore: @<Downdate the data structures to the state they were in when |
| 99 |
level |t| was entered@>; |
| 100 |
backtrack: @<Decrease |t|, if possible, and try the next possibility; |
| 101 |
or |goto done|@>; |
| 102 |
done: |
| 103 |
|
| 104 |
@ @<Increase |t| and update the data structures...@>= |
| 105 |
t++; |
| 106 |
t->vert=v; |
| 107 |
v->taken=1; |
| 108 |
if (v==z) { |
| 109 |
if (t==tmax) @<Record a solution@>; |
| 110 |
goto backtrack; |
| 111 |
} |
| 112 |
for (aa=v->arcs,y=NULL;aa;aa=aa->next) { |
| 113 |
u=aa->tip; |
| 114 |
d=u->deg-1; |
| 115 |
if (d==1 && u->taken==0) { |
| 116 |
if (y) goto restore; /* restoration will stop at |aa| */ |
| 117 |
y=u; |
| 118 |
} |
| 119 |
u->deg=d; |
| 120 |
} |
| 121 |
if (y) { |
| 122 |
t->ark=NULL; |
| 123 |
v=y; |
| 124 |
goto advance; |
| 125 |
} |
| 126 |
a=v->arcs; |
| 127 |
|
| 128 |
@ @<Downdate the data structures to the state they were in when |
| 129 |
level |t| was entered@>= |
| 130 |
for (a=t->vert->arcs;a!=aa;a=a->next) a->tip->deg++; |
| 131 |
|
| 132 |
@ @<Look at edge |a| and its successors, advancing if it is a valid move@>= |
| 133 |
while (a) { |
| 134 |
v=a->tip; |
| 135 |
if (v->taken==0) { |
| 136 |
t->ark=a; |
| 137 |
goto advance; |
| 138 |
} |
| 139 |
a=a->next; |
| 140 |
} |
| 141 |
restore_all: aa=NULL; /* all moves tried; we fall through to |restore| */ |
| 142 |
|
| 143 |
@ @<Decrease |t|, if possible...@>= |
| 144 |
t->vert->taken=0; |
| 145 |
t--; |
| 146 |
if (t->ark) { |
| 147 |
a=t->ark->next; |
| 148 |
goto try; |
| 149 |
} |
| 150 |
if (t==g->vertices) goto done; |
| 151 |
goto restore_all; /* the move was forced */ |
| 152 |
|
| 153 |
@ @<Record a solution@>= |
| 154 |
{ |
| 155 |
count++; |
| 156 |
if (count%modulus==0) { |
| 157 |
printf("%d: ",count); |
| 158 |
for (u=g->vertices;u<=tmax;u++) printf("%s ",u->vert->name); |
| 159 |
printf("\n"); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
@*Index. |