-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgutil2.c
566 lines (470 loc) · 11.2 KB
/
gutil2.c
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/* gutil2.c: Some more graph utilities. */
#include "gtools.h"
#include "gutils.h"
/**************************************************************************/
int
loopcount(graph *g, int m, int n)
/* Number of loops */
{
set *gi;
int i,nl;
nl = 0;
for (i = 0, gi = g; i < n; ++i, gi += m)
if (ISELEMENT(gi,i)) ++nl;
return nl;
}
/**************************************************************************/
long
pathcount1(graph *g, int start, setword body, setword last)
/* Number of paths in g starting at start, lying within body and
ending in last. {start} and last should be disjoint subsets of body. */
{
long count;
setword gs,w;
int i;
gs = g[start];
w = gs & last;
count = POPCOUNT(w);
body &= ~bit[start];
w = gs & body;
while (w)
{
TAKEBIT(i,w);
count += pathcount1(g,i,body,last&~bit[i]);
}
return count;
}
/**************************************************************************/
long
cyclecount1(graph *g, int n)
/* The total number of cycles in g (assumed no loops), m=1 only */
{
setword body,nbhd;
long total;
int i,j;
body = ALLMASK(n);
total = 0;
for (i = 0; i < n-2; ++i)
{
body ^= bit[i];
nbhd = g[i] & body;
while (nbhd)
{
TAKEBIT(j,nbhd);
total += pathcount1(g,j,body,nbhd);
}
}
return total;
}
/**************************************************************************/
long
cyclecount(graph *g, int m, int n)
/* The total number of cycles in g (assumed no loops) */
{
if (n == 0) return 0;
if (m == 1) return cyclecount1(g,n);
gt_abort(">E cycle counting is only implemented for n <= WORDSIZE\n");
return 0;
}
/**************************************************************************/
long
indpathcount1(graph *g, int start, setword body, setword last)
/* Number of induced paths in g starting at start, extravertices within
* body and ending in last.
* {start}, body and last should be disjoint. */
{
long count;
setword gs,w;
int i;
gs = g[start];
w = gs & last;
count = POPCOUNT(w);
w = gs & body;
while (w)
{
TAKEBIT(i,w);
count += indpathcount1(g,i,body&~gs,last&~bit[i]&~gs);
}
return count;
}
/**************************************************************************/
long
indcyclecount1(graph *g, int n)
/* The total number of induced cycles in g (assumed no loops), m=1 only */
{
setword body,last,cni;
long total;
int i,j;
body = ALLMASK(n);
total = 0;
for (i = 0; i < n-2; ++i)
{
body ^= bit[i];
last = g[i] & body;
cni = g[i] | bit[i];
while (last)
{
TAKEBIT(j,last);
total += indpathcount1(g,j,body&~cni,last);
}
}
return total;
}
/**************************************************************************/
long
indcyclecount(graph *g, int m, int n)
/* The total number of induced cycles in g (assumed no loops) */
{
if (n == 0) return 0;
if (m == 1) return indcyclecount1(g,n);
gt_abort(
">E induced cycle counting is only implemented for n <= WORDSIZE\n");
return 0;
}
/**************************************************************************/
long
numtriangles1(graph *g, int n)
/* The number of triangles in g; undirected only */
{
int i,j;
setword gi,w;
long total;
total = 0;
for (i = 0; i < n-2; ++i)
{
gi = g[i] & BITMASK(i);
while (gi)
{
TAKEBIT(j,gi);
w = g[j] & gi;
if (w) total += POPCOUNT(w);
}
}
return total;
}
/**************************************************************************/
long
numtriangles(graph *g, int m, int n)
/* The number of triangles in g; undirected only */
{
int i,j,k,kw;
setword *gi,*gj,w;
long total;
if (m == 1) return numtriangles1(g,n);
total = 0;
for (i = 0, gi = g; i < n-2; ++i, gi += m)
for (j = i; (j = nextelement(gi,m,j)) > 0; )
{
gj = GRAPHROW(g,j,m);
kw = SETWD(j);
w = gi[kw] & gj[kw] & BITMASK(SETBT(j));
if (w) total += POPCOUNT(w);
for (k = kw+1; k < m; ++k)
{
w = gi[k] & gj[k];
if (w) total += POPCOUNT(w);
}
}
return total;
}
/**************************************************************************/
long
numdirtriangles(graph *g, int m, int n)
/* The number of directed triangles in g */
{
long total;
int i,j,k;
set *gi,*gj;
total = 0;
for (i = 0, gi = g; i < n-2; ++i, gi += m)
for (j = i; (j = nextelement(gi,m,j)) >= 0;)
{
gj = GRAPHROW(g,j,m);
for (k = i; (k = nextelement(gj,m,k)) >= 0; )
if (k != j && ISELEMENT(GRAPHROW(g,k,m),i)) ++total;
}
return total;
}
/**************************************************************************/
void
commonnbrs(graph *g, int *minadj, int *maxadj, int *minnon, int *maxnon,
int m, int n)
/* Count the common neighbours of pairs of vertices, and give the minimum
and maximum for adjacent and non-adjacent vertices. Undirected only.
Null minimums are n+1 and null maximums are -1.
*/
{
int j,k;
int mina,maxa,minn,maxn;
int cn;
set *gi,*gj;
setword w;
if (n == 0)
{
*minadj = *maxadj = *minnon = *maxnon = 0;
return;
}
mina = minn = n+1;
maxa = maxn = -1;
for (j = 0, gj = g; j < n; ++j, gj += m)
for (gi = g; gi != gj; gi += m)
{
cn = 0;
for (k = 0; k < m; ++k)
{
w = gi[k] & gj[k];
if (w) cn += POPCOUNT(w);
}
if (ISELEMENT(gi,j))
{
if (cn < mina) mina = cn;
if (cn > maxa) maxa = cn;
}
else
{
if (cn < minn) minn = cn;
if (cn > maxn) maxn = cn;
}
}
*minadj = mina;
*maxadj = maxa;
*minnon = minn;
*maxnon = maxn;
}
/**************************************************************************/
void
delete1(graph *g, graph *h, int v, int n)
/* Delete vertex v from g, result in h */
{
setword mask1,mask2,gi;
int i;
mask1 = ALLMASK(v);
mask2 = BITMASK(v);
for (i = 0; i < v; ++i)
{
gi = g[i];
h[i] = (gi & mask1) | ((gi & mask2) << 1);
}
for (i = v; i < n-1; ++i)
{
gi = g[i+1];
h[i] = (gi & mask1) | ((gi & mask2) << 1);
}
}
/**************************************************************************/
void
contract1(graph *g, graph *h, int v, int w, int n)
/* Contract distinct vertices v and w (not necessarily adjacent)
with result in h. No loops are created. */
{
int x,y;
setword bitx,bity,mask1,mask2;
int i;
if (w < v)
{
x = w;
y = v;
}
else
{
x = v;
y = w;
}
bitx = bit[x];
bity = bit[y];
mask1 = ALLMASK(y);
mask2 = BITMASK(y);
for (i = 0; i < n; ++i)
if (g[i] & bity)
h[i] = (g[i] & mask1) | bitx | ((g[i] & mask2) << 1);
else
h[i] = (g[i] & mask1) | ((g[i] & mask2) << 1);
h[x] |= h[y];
for (i = y+1; i < n; ++i) h[i-1] = h[i];
h[x] &= ~bitx;
}
/**************************************************************************/
static TLS_ATTR int knm[18][16]; /* knm[n,m] = conncontent(K_n - m*K_2) */
static TLS_ATTR boolean knm_computed = FALSE;
int
conncontent(graph *g, int m, int n)
/* number of connected spanning subgraphs with an even number
of edges minus the number with an odd number of edges */
{
graph h[WORDSIZE];
setword gj;
int i,j,v1,v2,x,y;
int minv,mindeg,deg,goodv;
long ne;
if (m > 1) ABORT("conncontent only implemented for m=1");
/* First handle tiny graphs */
if (n <= 3)
{
if (n == 1) return 1;
if (n == 2) return (g[0] ? -1 : 0);
if (!g[0] || !g[1] || !g[2]) return 0; /* disconnected */
if (g[0]^g[1]^g[2]) return 1; /* path */
return 2; /* triangle */
}
/* Now compute
ne = number of edges
mindeg = minimum degree
minv = a vertex of minimum degree
goodv = a vertex with a clique neighbourhood (-1 if none)
*/
mindeg = n;
ne = 0;
goodv = -1;
for (j = 0; j < n; ++j)
{
gj = g[j];
deg = POPCOUNT(gj);
ne += deg;
if (deg < mindeg)
{
mindeg = deg;
minv = j;
if (deg == 1) goodv = j;
}
if (deg >= 3 && deg <= 4 && goodv < 0)
{
while (gj)
{
TAKEBIT(i,gj);
if (gj & ~g[i]) break;
}
if (!gj) goodv = j;
}
}
ne /= 2;
/* Cases of isolated vertex or tree */
if (mindeg == 0) return 0;
#if 0
if (mindeg == 1 && ne == n-1)
{
if (isconnected1(g,n)) return ((n&1) ? 1 : -1);
else return 0;
}
#endif
/* Cases of clique and near-clique */
if (mindeg == n-1)
{
j = -1;
for (i = 2; i < n; ++i) j *= -i;
return j;
}
if (mindeg == n-2 && n < 16)
{
if (!knm_computed)
{
knm_computed = TRUE;
knm[1][0] = 1;
for (i = 2; i < 16; ++i)
{
knm[i][0] = -knm[i-1][0] * (i-1);
for (j = 1; j+j <= i; ++j)
knm[i][j] = knm[i][j-1] + knm[i-1][j-1];
}
}
return knm[n][(n*n-n)/2-ne];
}
/* Case of vertex with clique neighbourhood */
if (goodv >= 0)
{
delete1(g,h,goodv,n);
return -POPCOUNT(g[goodv]) * conncontent(h,m,n-1);
}
/* Case of minimum degree 2 */
if (mindeg == 2)
{
x = FIRSTBITNZ(g[minv]);
y = FIRSTBITNZ(g[minv]^bit[x]);
if (x > minv) --x;
if (y > minv) --y;
delete1(g,h,minv,n);
v1 = conncontent(h,m,n-1);
if (h[x] & bit[y]) return -2*v1; /* adjacent neighbours */
h[x] |= bit[y];
h[y] |= bit[x];
v2 = conncontent(h,m,n-1);
return -v1 - v2;
}
/* Case of more than 2/3 dense but not complete */
if (3*ne > n*n-n)
{
j = FIRSTBITNZ(g[minv] ^ bit[minv] ^ ALLMASK(n)); /* non-neighbour */
g[minv] ^= bit[j];
g[j] ^= bit[minv];
v1 = conncontent(g,m,n);
g[minv] ^= bit[j];
g[j] ^= bit[minv];
contract1(g,h,minv,j,n);
v2 = conncontent(h,m,n-1);
return v1 + v2;
}
/* All remaining cases */
j = FIRSTBITNZ(g[minv]); /* neighbour */
g[minv] ^= bit[j];
g[j] ^= bit[minv];
v1 = conncontent(g,m,n);
g[minv] ^= bit[j];
g[j] ^= bit[minv];
contract1(g,h,minv,j,n);
v2 = conncontent(h,m,n-1);
return v1 - v2;
}
boolean
stronglyconnected(graph *g, int m, int n)
/* test if digraph g is strongly connected */
{
int sp,v,vc;
int numvis;
set *gv;
#if MAXN
int num[MAXN],lowlink[MAXN],stack[MAXN];
#else
DYNALLSTAT(int,num,num_sz);
DYNALLSTAT(int,lowlink,lowlink_sz);
DYNALLSTAT(int,stack,stack_sz);
#endif
#if !MAXN
DYNALLOC1(int,num,num_sz,n,"stronglyconnected");
DYNALLOC1(int,lowlink,lowlink_sz,n,"stronglyconnected");
DYNALLOC1(int,stack,stack_sz,n,"stronglyconnected");
#endif
if (n == 0) return FALSE;
num[0] = 0;
for (v = 1; v < n; ++v) num[v] = -1;
lowlink[0] = 0;
numvis = 1;
sp = 0;
v = 0;
vc = -1;
gv = (set*)g;
for (;;)
{
vc = nextelement(gv,m,vc);
if (vc < 0)
{
if (sp == 0) break;
if (lowlink[v] == num[v]) return FALSE;
vc = v;
v = stack[--sp];
gv = GRAPHROW(g,v,m);
if (lowlink[vc] < lowlink[v]) lowlink[v] = lowlink[vc];
}
else if (num[vc] < 0)
{
stack[++sp] = vc;
v = vc;
gv = GRAPHROW(g,v,m);
vc = -1;
lowlink[v] = num[v] = numvis++;
}
else if (vc != v)
{
if (num[vc] < lowlink[v]) lowlink[v] = num[vc];
}
}
return numvis == n;
}