-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathback-warnsdorf.w
More file actions
192 lines (171 loc) · 5.36 KB
/
back-warnsdorf.w
File metadata and controls
192 lines (171 loc) · 5.36 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
@i gb_types.w
\def\given{\!\mid\!}
@*Intro. Given a graph $G$ on the vertex set $V$, and a vertex $s\in V$,
this program finds all possible simple paths from $s$ that could be
traversed by Warnsdorf's (nondeterministic) rule.
It reports their lengths and their final vertices.
Warndorf's rule is simply this: At a given stage, we have covered
a set of vertices~$U$, with a path from $s$ to some $v\in U$.
If $v$ has no neighbors $\notin U$, stop. Otherwise
cover a neighbor $u\notin U$ whose degree in $G\given(V-U)$ is minimum,
and set $v\gets u$, and repeat.
An optional extension to that rule is also implemented: One or more
vertices can be mentioned as preferred candidates for the end of the path.
(For example, we might want to mention a neighbor of $s$, because
that would imply a Hamiltonian cycle.)
This implementation has an interesting artifact: If the same vertex is
mentioned more than once as a target, it essentially disappears from the
graph. Therefore we could, say, try for a Hamiltonian path that
ends with $a$, $b$, $c$ by listing $a$, $b$, $b$, $c$, $c$ on the
command line.
@d maxn 1000
@c
#include <stdio.h>
#include <stdlib.h>
#include "gb_graph.h"
#include "gb_save.h"
unsigned long long nodes,lnodes[maxn];
unsigned long long count,hcount,lcount[maxn],vcount[maxn];
double pr[maxn],vpr[maxn],lpr[maxn],totalpr;
Vertex *start,*vstack[maxn];
int sptr,bestl;
int mv[maxn],ht[maxn];
@<Subroutines@>@;
main(int argc, char*argv[]) {
register int d,k,l,x;
register Arc *a;
register Graph *g;
register Vertex *u,*v;
register double p;
@<Process the command line@>;
@<Backtrack through the Warnsdorf tree@>;
@<Output the results@>;
}
@ @<Process the command line@>=
if (argc<3) {
fprintf(stderr,"Usage: %s foo.gb start [dest]*\n",argv[0]);
exit(-1);
}
g=restore_graph(argv[1]);
if (!g) {
fprintf(stderr,"I couldn't reconstruct graph %s!\n",argv[1]);
exit(-2);
}
if (g->n>=maxn) {
fprintf(stderr,"Recompile me --- I can't deal with more than %d vertices!\n",
maxn-1);
exit(-6);
}
start=findvert(g,argv[2]);
@ @<Sub...@>=
Vertex *findvert(Graph *g, char*str) {
register Vertex *v;
for (v=g->vertices;v<g->vertices+g->n;v++)
if (strcmp(v->name,str)==0) break;
if (v<g->vertices+g->n) return v;
fprintf(stderr,"Vertex %s isn't in the graph!\n",
str);
exit(-3);
}
@ Again I follow ye olde structure of Algorithm 7.2.2B.
@d deg u.I
@d covered v.I
@<Backtrack through the Warnsdorf tree@>=
b1:@+for (v=g->vertices;v<g->vertices+g->n;v++) {
for (d=0,a=v->arcs;a;a=a->next) d++;
v->deg=d,v->covered=0;
}
for (k=3;argv[k];k++)
findvert(g,argv[k])->deg+=g->n; /* make this vertex undesirable */
l=0,v=start,ht[0]=0;
vstack[0]=v,sptr=1,x=0;
p=1.0;
b2: nodes++, lnodes[l]++; /* at this point |v=vstack[x]| */
mv[l]=x, x=ht[++l]=sptr;
p=p/(double)(ht[l]-ht[l-1]);
pr[l]=p; /* the probability that a random tree walk would get here */
v->covered=1;
for (d=g->n+g->n,a=v->arcs;a;a=a->next) {
u=a->tip;
if (u->covered) continue;
k=u->deg-1, u->deg=k;
if (k>d) continue;
if (k<d) d=k,sptr=x;
vstack[sptr++]=u;
}
if (x==sptr) @<Report end of path and |goto b5|@>;
b3: v=vstack[x];
goto b2;
b4:@+if (++x<sptr) goto b3;
b5:@+if (--l) {
x=mv[l],p=pr[l],v=vstack[x],sptr=ht[l+1];
v->covered=0;
for (a=v->arcs;a;a=a->next) {
u=a->tip;
if (u->covered) continue;
u->deg++;
}
goto b4;
}
@ @<Report end of path and |goto b5|@>=
{
count++, lcount[l]++, lpr[l]+=p, totalpr+=p;
nodes++, lnodes[l]++; /* count the solution nodes with the branch nodes */
if (l>=bestl) {
bestl=l;
@<Publish the current path@>;
if (l==g->n) hcount++,vcount[v-g->vertices]++,vpr[v-g->vertices]+=p;
}
goto b5;
}
@ @<Publish the current path@>=
{
for (k=0;k<l;k++) printf("%s ",
vstack[mv[k]]->name);
printf("(%d,%g) #%llu, %g\n",
l,p,count,totalpr); @q)@>;
}
@ @<Output the results@>=
fprintf(stderr,"Altogether %llu nodes, %llu paths, %llu hpaths.\n",
nodes,count,hcount);
for (l=0;l<=bestl;l++) {
fprintf(stderr," Level %d: %12llu node%s",
l,lnodes[l],lnodes[l]==1?"":"s");
if (lcount[l])
fprintf(stderr,", %llu paths (%g)",
lcount[l],lpr[l]/totalpr);
fprintf(stderr,"\n");
}
for (k=0;k<g->n;k++) if (vcount[k])
fprintf(stderr," hamiltonian to %s: %12llu, %g\n",
(g->vertices+k)->name,vcount[k],vpr[k]/totalpr);
@ Here's a routine that I might use while debugging.
@<Sub...@>=
void print_vert(Vertex *v) {
fprintf(stderr," %s %s%ld%s\n",
v->name,v->covered?"(":"",v->deg,v->covered?")":"");
}
@ And another, to display the state of {\it all\/} the vertices.
@<Sub...@>=
void print_state(Graph *g) {
register int k;
for (k=0;k<g->n;k++) print_vert(g->vertices+k);
}
@ Yet another, to show the neighbors of a given vertex and their
current degrees.
@<Sub...@>=
void print_arcs(Vertex *v) {
register Arc *a;
register Vertex *u;
fprintf(stderr,"%s ->",
v->name);
for (a=v->arcs;a;a=a->next) {
u=a->tip;
if (u->covered) fprintf(stderr," (%s)",
u->name)@q)@>;
else fprintf(stderr," %s(%ld)",
u->name,u->deg);
}
fprintf(stderr,"\n");
}
@*Index.