-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvn_graph.c
1837 lines (1738 loc) · 53.3 KB
/
vn_graph.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// KMB 2006 Dec 05, 13
// - better sizes for graph_t components
// - non-recursive dfs
// - new internals with g->b and g->d
// - neighbour lists are no longer -1-terminated
// - neighbour lists grow as required - can now do bigger graphs
// - g->d[i] gives degree of node i
// KMB 2006 Jul 19; Nov 22
// TODO tidy up debug prints
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/times.h>
#include "vn_graph.h"
static const double RAND_MAXp1=((double)RAND_MAX+1.0);
#define UNIFORM_01 (rand()/RAND_MAXp1) /* uniform[0,1) */
#undef VN_COUNT_RNG_CALLS
#ifdef VN_COUNT_RNG_CALLS
long long vn_rng_calls=0;
#define VN_GRAPH_UNIFORM_01 (vn_rng_calls++,rand()/((double)RAND_MAX)) /* uniform(0,1) */
#else
#define VN_GRAPH_UNIFORM_01 (rand()/((double)RAND_MAX)) /* uniform(0,1) */
#endif
//#define MALLOC_FREE_CHECK
#ifdef MALLOC_FREE_CHECK
static size_t alloc=0;
#define Malloc(sz) malloc((alloc+=(sz),sz))
#define Calloc(n,sz) calloc(n,(alloc+=((n)*(sz)),sz))
#define Realloc(p,oldsz,newsz) realloc(p,(alloc+=((newsz)-(oldsz)),(newsz)))
#define Free(p,sz) free((alloc-=(sz),p))
#define print_alloc(msg) fprintf(stderr,"%s: alloc=%ld\n",msg,(long)alloc);
#define check_alloc(msg) if (alloc) { fprintf(stderr,"%s: alloc=%ld\n",msg,(long)alloc); exit(1); }
#else
#define Malloc(sz) malloc(sz)
#define Calloc(n,sz) calloc(n,sz)
#define Realloc(p,oldsz,newsz) realloc(p,(newsz))
#define Free(p,sz) free(p)
#endif
inline static void graph_add_edge1(graph_t g, double k);
#include "trick.c"
#include "clique.c"
#include "rainbow.c"
#include "geng_reader.c"
const char* graph_get_version(void) {
// FIXME use VERY_NAUTY_VERSION
const char* graph_version="very_nauty-1.1 compiled" " "__DATE__" "__TIME__;
return graph_version;
}
static size_t adj_list_initial_size=8; // initial size of adjacency lists
void set_adj_list_initial_size(size_t sz) {
adj_list_initial_size=sz;
}
static void shuffle(int p[], int n, int m);
//! make a new empty graph with n nodes
graph_t graph_new(unsigned int n) {
size_t sz=adj_list_initial_size;
int i;
graph_t g=Malloc(sizeof(struct _graph));
g->v=Malloc(n*sizeof(char));
g->c=Malloc(n*sizeof(int));
g->l=Malloc(n*sizeof(int));
g->b=Malloc(n*sizeof(size_t));
g->d=Malloc(n*sizeof(node_t));
g->a=Malloc(n*sizeof(node_t*));
if (n<=sz) sz=n; // initial allocation of neighbour lists
for (i=0; i<n; i++) {
g->c[i]=-1;
g->l[i]=-1;
g->v[i]=0;
g->d[i]=0;
g->b[i]=sz;
g->a[i]=Malloc(sz*sizeof(node_t));
}
g->nnodes=n;
g->nedges=0;
return g;
}
//! destroy a graph
void graph_clear(graph_t g) {
int i,n=g->nnodes;
for (i=0; i<n; i++) Free(g->a[i],n*sizeof(node_t*));
Free(g->v,n*sizeof(char));
Free(g->c,n*sizeof(int));
Free(g->l,n*sizeof(int));
Free(g->b,n*sizeof(size_t));
Free(g->d,n*sizeof(node_t));
Free(g->a,n*sizeof(node_t*));
Free(g,sizeof(struct _graph));
}
//! add a node to a graph
void graph_add_node(graph_t g) {
size_t sz=adj_list_initial_size;
unsigned int i,n=g->nnodes,np1=n+1;
if (np1<=sz) sz=np1; // initial allocation of neighbour lists
g->a=Realloc(g->a,n*sizeof(node_t*),np1*sizeof(node_t*));
g->b=Realloc(g->b,n*sizeof(size_t),np1*sizeof(size_t));
g->b[n]=sz;
g->a[n]=Malloc(sz*sizeof(node_t));
g->v=Realloc(g->v,n*sizeof(char),np1*sizeof(char));
g->c=Realloc(g->c,n*sizeof(int),np1*sizeof(int));
g->l=Realloc(g->l,n*sizeof(int),np1*sizeof(int));
g->d=Realloc(g->d,n*sizeof(node_t),np1*sizeof(node_t));
g->d[n]=0;
for (i=0; i<n; i++) {
g->v[i]=0;
g->c[i]=-1;
g->l[i]=-1;
}
g->nnodes=np1;
}
//! delete edge u--v from the graph (if it is present)
// return 1 if edge was found and deleted, else 0
int graph_del_edge(graph_t g, node_t u, node_t v) {
int i,j;
for (i=0; i<g->d[u]; i++) if (g->a[u][i]==v) {
memmove(&g->a[u][i],&g->a[u][i+1],(g->d[u]-1-i)*sizeof(node_t));
g->d[u]--;
for (j=0; j<g->d[v]; j++) if (g->a[v][j]==u) {
memmove(&g->a[v][j],&g->a[v][j+1],(g->d[v]-1-j)*sizeof(node_t));
g->d[v]--;
break;
}
g->nedges--;
return 1;
}
return 0;
}
//! 1 if edge i--j is present in g
int graph_has_edge(graph_t g, node_t i, node_t j) {
int k;
for (k=0; k<g->d[i]; k++) if (g->a[i][k]==j) return 1;
return 0;
}
//! empty a graph
void graph_empty(graph_t g) {
int i;
bzero(g->d,g->nnodes*sizeof(node_t)); // degree 0
bzero(g->v,g->nnodes*sizeof(char)); // not visited
for (i=0; i<g->nnodes; i++) {
g->c[i]=-1; // not colored
g->l[i]=-1; // no cluster
}
g->nedges=0;
}
//! a complete graph
void graph_complete(graph_t g) {
int i,j,n=g->nnodes;
for (i=0; i<n; i++) {
g->a[i]=Realloc(g->a[i],g->b[i]*sizeof(node_t),n*sizeof(node_t));
for (j=0; j<n; j++) if (j!=i) g->a[i][j]=j;
g->d[i]=n-1;
g->b[i]=n;
g->c[i]=-1;
g->l[i]=-1;
g->v[i]=0;
}
g->nedges=n*(n-1)/2;
}
//! add undirected edge u--v to the graph, if it is not already present
void graph_add_edge(graph_t g, node_t u, node_t v) {
int i;
for (i=0; i<g->d[u]; i++) if (g->a[u][i]==v) return;
assert(g->d[u]<=g->b[u]);
if (g->d[u]==g->b[u]) {
g->b[u]+=16;
g->a[u]=Realloc(g->a[u],(g->b[u]-16)*sizeof(node_t),g->b[u]*sizeof(node_t));
}
g->a[u][g->d[u]++]=v;
assert(g->d[v]<=g->b[v]);
if (g->d[v]==g->b[v]) {
g->b[v]+=16;
g->a[v]=Realloc(g->a[v],(g->b[v]-16)*sizeof(node_t),g->b[v]*sizeof(node_t));
}
g->a[v][g->d[v]++]=u;
g->nedges++;
}
//! add undirected edge u--v to the graph, whether present already or not
// warning - can make multigraphs if abused
void graph_append_edge(graph_t g, node_t u, node_t v) {
assert(g->d[u]<=g->b[u]);
if (g->d[u]==g->b[u]) {
g->b[u]+=16;
g->a[u]=Realloc(g->a[u],(g->b[u]-16)*sizeof(node_t),g->b[u]*sizeof(node_t));
}
g->a[u][g->d[u]++]=v;
assert(g->d[v]<=g->b[v]);
if (g->d[v]==g->b[v]) {
g->b[v]+=16;
g->a[v]=Realloc(g->a[v],(g->b[v]-16)*sizeof(node_t),g->b[v]*sizeof(node_t));
}
g->a[v][g->d[v]++]=u;
g->nedges++;
}
//! degree of a node
int graph_node_degree(graph_t g, node_t i) {
return g->d[i];
}
//! minimum degree of a node in g
int graph_min_degree(graph_t g) {
int i,d,k=INT_MAX;
for (i=0; i<g->nnodes; i++) if ((d=graph_node_degree(g,i))<k) k=d;
return k;
}
//! maximum degree of a node in g
int graph_max_degree(graph_t g) {
int i,d,k=-1;
for (i=0; i<g->nnodes; i++) if ((d=graph_node_degree(g,i))>k) k=d;
return k;
}
//! mean degree
double graph_mean_degree(graph_t g) {
return (2.0*g->nedges)/g->nnodes;
}
//! simple listing of graph
void graph_show(graph_t g) {
int i,j;
for (i=0; i<g->nnodes; i++) if (g->d[i]>0) { // i has neighbours
printf("%d: ",i);
for (j=0; j<g->d[i]; j++) printf("%d ",g->a[i][j]);
printf("\n");
}
}
void graph_show_edge_list(graph_t g, int offset) {
int i,j;
printf("%d %d\n",g->nnodes,g->nedges);
for (i=0; i<g->nnodes; i++) if (g->d[i]>0) { // i has neighbours
for (j=0; j<g->nnodes; j++) if (g->d[i]>0 && g->a[i][j]>i) printf("%d %d\n",i+offset,g->a[i][j]+offset);
}
}
void graph_dump_to_file(graph_t g, char* fn) {
// my edge list format
int i,j;
FILE* f=fopen(fn,"w");
fprintf(f,"n=%d m=%d.\n",g->nnodes,g->nedges);
for (i=0; i<g->nnodes; i++) if (g->d[i]>0) { // i has neighbours
for (j=0; j<g->d[i]; j++) if (j>i) fprintf(f,"%d %d\n",i,j);
}
fclose(f);
printf("%s written\n",fn);
}
void graph_to_showg(graph_t g, int n, char* fn) {
// "showg -e -l0" format
int i,j;
FILE* f=fopen(fn,"w");
fprintf(f,"Graph %d, order %d.\n",n,g->nnodes);
fprintf(f,"%d %d\n",n,g->nedges);
for (i=0; i<g->nnodes; i++) if (g->d[i]>0) { // i has neighbours
for (j=0; j<g->d[i]; j++) if (j>i) fprintf(f,"%d %d ",i,j);
}
fprintf(f,"\n");
fclose(f);
printf("%s written:\n",fn);
}
void graph_to_dimacs(graph_t g, char* fn) {
// ftp://dimacs.rutgers.edu/pub/challenge/graph/doc/ccformat.tex
// 1-indexed
int i,j;
FILE* f=fopen(fn,"w");
fprintf(f,"c written by graph_to_dimacs\n");
fprintf(f,"p edge %d %d\n",g->nnodes,g->nedges);
for (i=0; i<g->nnodes; i++) if (g->d[i]>0) { // i has neighbours
for (j=0; j<g->d[i]; j++) if (j>i) fprintf(f,"e %d %d\n",i+1,j+1);
}
fclose(f);
printf("%s written\n",fn);
}
void graph_to_theta(graph_t g, char* fn) { // FIXME
// 1-indexed, complemented, for DSDP Lovasz theta
int i,j,n=g->nnodes;
FILE* f=fopen(fn,"w");
int u[g->nnodes];
fprintf(f,"%d %d\n",n,n*(n-1)/2-g->nedges);
for (i=0; i<n; i++) if (g->d[i]>0) { // i has neighbours
bzero(u,n*sizeof(int));
for (j=0; j<g->d[i]; j++) u[g->a[i][j]]=1;
for (j=i+1; j<n; j++) if (!u[j]) fprintf(f,"%d %d\n",i+1,j+1);
}
fclose(f);
printf("%s written: now do \"theta %s\"\n",fn,fn);
}
graph_t graph_line_graph(graph_t g) {
int i,j,k,m=0,n=g->nedges;
int e0[n],e1[n];
graph_t l=graph_new(n);
for (i=0; i<g->nnodes; i++) {
for (k=0; k<g->d[i]; k++) { // loop over edges of g
j=g->a[i][k];
if (i<j) { // i--j is an edge; do each edge once only
assert(m<n);
if (m>n-1) { fprintf(stderr,"Arghhh!!"); exit(1); }
e0[m]=i;
e1[m]=j;
m++;
}
}
}
assert(m==n);
for (i=0; i<n; i++) { // loop over possible edges of line graph
for (j=0; j<n; j++) if (i!=j) {
if (e0[i]==e0[j] ||
e1[j]==e1[i] ||
e1[i]==e0[j] ||
e0[i]==e1[j]) { // common end-point
graph_add_edge(l,i,j);
}
}
}
return l;
}
void graph_make_dotfile(graph_t g, char* fn) {
int i,j,k;
FILE* f=fopen(fn,"w");
fprintf(f,"graph %s {\n","graph_make_dotfile");
fprintf(f," center=true; unflatten=true;\n ratio=1;\n /* mclimit=5;\n nslimit=500;\n nslimit1=500;\n remincross=true; */\n size=\"8,8\";\n node [shape=\"circle\",size=\"0.1\"]\n node [color=green,fontcolor=red,fontsize=8]\n edge [color=blue];\n graph [concentrate=\"false\"];\n");
for (i=0; i<g->nnodes; i++) if (g->d[i]>0) { // i has neighbours
for (k=0; k<g->d[i]; k++) if ((j=g->a[i][k])>i) fprintf(f," %d--%d;\n",i,j);
}
fprintf(f,"}\n");
fclose(f);
printf("%s written: now do \"neato -Tps %s | gv -\"\n",fn,fn);
}
void rgb2hexstring(double r, double g, double b, char *s) {
double gi=1.0/1.0; // inverse gamma
r=floor(255.9999999*pow(r,gi));
g=floor(255.9999999*pow(g,gi));
b=floor(255.9999999*pow(b,gi));
snprintf(s,7,"%02x%02x%02x",(int)r,(int)g,(int)b);
}
static double logpoisson(int k, double l) {
// log Poisson
// log Pr[x=k]
return -l+k*log(l)-lgamma(k+1);
}
// http://www.graphviz.org/doc/info/colors.html
void graph_make_dotfile_colored(graph_t g, char* fn) {
int i,j,k,m=-1;
char s[7];
double rd,gr,bl,c;
FILE* f=fopen(fn,"w");
fprintf(f,"graph %s {\n","graph_make_dotfile_colored");
for (i=0; i<g->nnodes; i++) if (g->c[i]>m) m=g->c[i];
fprintf(f," center=true; unflatten=true;\n ratio=1;\n /* mclimit=5;\n nslimit=500;\n nslimit1=500;\n remincross=true; */\n size=\"8,10\";\n node [shape=\"circle\",size=\"0.03\"]\n node [fontsize=10,style=filled]\n edge [style=\"setlinewidth(1)\",color=black];\n graph [concentrate=\"false\",label=\"%d colors\"];\n",m);
for (i=0; i<g->nnodes; i++) {
c=g->c[i]/(double)m;
rainbow(c,1.0,1.0,&rd,&gr,&bl);
rgb2hexstring(rd,gr,bl,s);
//fprintf(stderr,"%d %d %f %s\n",i,g->c[i],(g->c[i])/(double)m,s);
fprintf(f," %d [color=\"#%s\",fillcolor=\"#%s\"];\n",i,s,s);
}
for (i=0; i<g->nnodes; i++) if (g->d[i]>0) { // i has neighbours
for (k=0; k<g->d[i]; k++) if ((j=g->a[i][k])>i) fprintf(f," %d--%d;\n",i,j);
}
fprintf(f,"}\n");
fclose(f);
printf("%s written: now do \"neato -Tps %s | gv -\"\n",fn,fn);
}
// non-recursive depth-first search - recommended over graph_dfs...
static void graph_dfs_nonrec(graph_t g, node_t s, node_t l) {
size_t sz=g->nnodes,usz=1024;
node_t i,j,nhbr,*stack=malloc(sz*sizeof(node_t));
int *u,c,top=0,utop,ok;
u=malloc(usz*sizeof(int));
stack[0]=s;
while (top>=0) {
j=stack[top--]; // pop
cluster(g,j)=l; // cluster number
visited(g,j)=1; // we have now visited node i
utop=0;
for (i=0; i<degree(g,j); i++) {
nhbr=neighbour(g,j,i);
if (!visited(g,nhbr)) { // push
if (utop>=usz-1) u=realloc(u,(usz+=1024)*sizeof(int));
u[utop++]=color(g,nhbr);
if (top>=sz-1) stack=realloc(stack,(sz+=1024)*sizeof(node_t));
stack[++top]=nhbr; // push
}
}
// find smallest available color for node j
c=0;
while (1) {
ok=1;
for (i=0; i<utop; i++) if (c==u[i]) { ok=0; break; }
if (ok) {
color(g,j)=c;
break;
}
c++;
}
}
free(u);
free(stack);
}
// recursive depth-first search - may get stack overflow problems...
// OBSOLETE
static void graph_dfs(graph_t g, node_t s, node_t l) {
node_t i;
int m=-1;
node_t *gas=g->a[s];
//fprintf(stderr,"s=%d l=%d kkk=%lu\n",s,l,kkk++);
cluster(g,s)=l; // cluster number
visited(g,s)=1; // we have now visited node s
// find biggest color used by neighbours
for (i=0; i<degree(g,s); i++) {
int j;
if ((j=color(g,gas[i]))>=0) m=j>m?j:m;
}
if (m==-1) // no neighbours are yet colored
color(g,s)=0;
else if (m==0) // one neighbours colored 0
color(g,s)=1;
else { // find colors used by neighbours - they are in the range [0,m]
// find smallest color not used by neighbours
int j;
//char *u=calloc(m+2,sizeof(char)); // extra 0 element as sentinel
char u[m+2]; bzero(u,(m+2)*sizeof(char)); // extra 0 element as sentinel
for (i=0; i<degree(g,s); i++) if ((j=color(g,gas[i]))>=0) u[j]=1;
i=-1; while (u[++i]);
color(g,s)=i;
//free(u);
}
// visit neighbours
for (i=0; i<degree(g,s); i++) if (!visited(g,gas[i])) graph_dfs(g,gas[i],l);
}
// recursive depth-first search - may get stack overflow problems...
// OBSOLETE
static void graph_dfs_stack(graph_t g, node_t s, int l) {
// undocumented - uses stack for temporary arrays (small graphs only)
int i,j,m=-1;
node_t *gas=g->a[s];
g->l[s]=l; // cluster number
g->v[s]=1; // we have just visited node s
// find biggest color used by neighbours
for (i=0; i<g->d[s]; i++) if ((j=color(g,gas[i]))>=0) m=j>m?j:m;
if (m==-1) // no neighbours are yet colored
g->c[s]=0;
else if (m==0) // one neighbours colored 0
g->c[s]=1;
else { // find colors used by neighbours - they are in the range [0,m]
int u[m+2]; bzero(u,(m+2)*sizeof(int)); // extra 0 element as sentinel
for (i=0; i<g->d[s]; i++) if ((j=color(g,gas[i]))>=0) u[j]=1;
// find smallest color not used by neighbours
i=0; while (u[i++]); color(g,s)=i-1;
}
// visit neighbours
for (i=0; i<g->d[s]; i++) if (!visited(g,gas[i])) graph_dfs_stack(g,gas[i],l);
}
int graph_greedy_color(graph_t g, int perm[]) {
// greedy coloring
int i,pi,c=0;
for (i=0; i<g->nnodes; i++) { g->v[i]=0; g->c[i]=-1; } // -1=not colored
for (i=0; i<g->nnodes; i++) {
if (perm) pi=perm[i]; else pi=i;
if (!visited(g,pi)) graph_dfs_nonrec(g,pi,c++);
}
c=-1;
for (i=0; i<g->nnodes; i++) if (color(g,i)>c) c=color(g,i);
return c+1; // colors used
}
int graph_sequential_color(graph_t g, int perm[], int ub) {
// N[i][j] => node i has a neighbour colored j
// colored in order given by perm
// return -1 as soon as more colors than ub used
int i,j=0,k,m=-1,pi,**N=Malloc(g->nnodes*sizeof(int*));
int *Npi;
node_t *gapi;
for (i=0; i<g->nnodes; i++) N[i]=Calloc(g->nnodes,sizeof(int));
for (i=0; i<g->nnodes; i++) {
if (perm) pi=perm[i]; else pi=i;
Npi=N[pi];
gapi=g->a[pi];
j=-1; while (Npi[++j]);
for (k=0; k<g->d[pi]; k++) N[gapi[k]][j]=1;
g->c[pi]=j;
if (j+1>ub) { m=-2; goto quit; };
m=j>m?j:m;
}
quit: for (i=0; i<g->nnodes; i++) Free(N[i],g->nnodes*sizeof(int));
Free(N,g->nnodes*sizeof(int*));
return m+1; // number of colors used
}
//! return an upper bound to chi
int graph_sequential_color_repeat(graph_t g, int n) {
int k,perm[g->nnodes],j,chi_ub=INT_MAX;
for (j=0; j<g->nnodes; j++) perm[j]=j;
for (j=0; j<n; j++) {
k=graph_sequential_color(g,perm,chi_ub);
if (k>0 && k<chi_ub) chi_ub=k; // got a better bound
shuffle(perm,g->nnodes,g->nnodes);
}
return chi_ub;
}
int graph_check_coloring(graph_t g) {
int i,j,k,ci;
for (i=0; i<g->nnodes; i++) {
ci=g->c[i];
j=0;
for (k=0; k<g->d[i]; k++) // for each neighbour
if (ci==g->c[g->a[i][k]]) { // something wrong
//for (j=0; j<g->nnodes; j++) printf("color(%d)=%d\n",j,g->c[j]);
return 0;
}
}
return 1; // ok
}
void graph_gnp_slow(graph_t g, double p) {
int i,j;
graph_empty(g);
for (i=0; i<g->nnodes; i++) for (j=i+1; j<g->nnodes; j++)
if (rand()<p*RAND_MAX) graph_append_edge(g,i,j);
}
double binomial_deviate(double pp, int n) {
int j;
static int nold=-1;
double am,em,g,p,bnl,sq,t,y;
static double pold=-1.0,pc,plog,pclog,en,oldg;
p=(pp<=0.5?pp:1.0-pp);
am=n*p;
if (n<30) {
bnl=0.0;
for (j=1; j<=n; j++) if (UNIFORM_01<p) ++bnl;
} else if (am<1.0) {
g=exp(-am);
for (t=1.0,j=0; j<=n; j++) { t*=UNIFORM_01; if (t<g) break; }
bnl=(j<=n?j:n);
} else {
if (n!=nold) {
en=n; oldg=lgamma(en+1.0); nold=n;
}
if (p!=pold) {
pc=1.0-p; plog=log(p); pclog=log(pc); pold=p;
}
sq=sqrt(2.0*am*pc);
do {
do {
y=tan(M_PI*UNIFORM_01); em=sq*y+am;
} while (em<0.0 || em>=(en+1.0));
em=floor(em);
t=1.2*sq*(1.0+y*y)*exp(oldg-lgamma(em+1.0)-lgamma(en-em+1.0)+em*plog+(en-em)*pclog);
} while (UNIFORM_01>t);
bnl=em;
}
if (p!=pp) return n-bnl;
return bnl;
}
void graph_gnp(graph_t g, double p) {
if (p>0.5) // dense - FIXME what is the best cross-over value??
graph_gnp_slow(g,p);
else { // sparse
int i,k=0,n2=nnodes(g)*(nnodes(g)-1)/2;
double log1mp=log1p(-p);
graph_empty(g);
while (1) {
if ((i=(int)floor(log1p(-UNIFORM_01)/log1mp))) { // geometric deviate
k+=i;
if (k>=n2) return;
graph_add_edge1(g,k);
}
}
}
}
void graph_gnm_slow(graph_t g, unsigned long m) {
/* KMB 2006 Dec 04
OBSOLETE - use graph_gnm
Return the random graph G_{n,m}.
Gives a graph picked uniformly at randomly out of the set of all graphs
with n nodes and m edges.
Parameters:
- m: the number of edges
Algorithm by Keith M. Briggs - python original Mar 31, 2006.
Inspired by Knuth's Algorithm S (Selection sampling technique),
in section 3.4.2 of The Art of Computer Programming by Donald E. Knuth
Volume 2 / Seminumerical algorithms; Third Edition, Addison-Wesley, 1997.
*/
int u=0,v=1,n=nnodes(g);
unsigned long k=0,t=0;
double mmax=(n*(n-1.0))/2; // beware overflow
if (n==1) return;
if (m>=mmax) {
graph_complete(g);
return;
}
graph_empty(g);
while (1) {
if ((mmax-t)*(rand()/(1.0+RAND_MAX))<m-k) {
assert(u<n);
assert(v<n);
graph_append_edge(g,u,v);
if (++k==m) return;
}
t++;
v++;
if (v==n) {
u++;
v=u+1;
}
}
}
// helper functions for Vitter's fast random sampling method
// ACM Trans Math Soft 13, 58 (1987)
inline static void graph_add_edge1(graph_t g, double k) {
// add an edge, counted by k in 0..n*(n-1)/2-1
// we will map k to (i,j) as follows, to get an entry in the lower triangle
// of the adjacency matrix.
// example for nnodes=5, i=row, j=column, k=...
// x x x x x
// 0 x x x x
// 1 2 x x x
// 3 4 5 x x
// 6 7 8 9 x
// k=i*(i-1)/2+j, so the inverse function is...
// i=floor((1+sqrt(1+8*k))/2)
// j=k-i*(i-1)/2
// could use iteration i<-floor((i^2+2*k)/(2*i-1)) instead of sqrt here...
node_t i=(node_t)floor((1.0+sqrt(1.0+8*k))/2.0),j=(node_t)(k-(i*(i-1))/2);
//fprintf(stderr,"%.0f->(%lu,%lu)\n",k,(long unsigned int)i,(long unsigned int)j);
graph_append_edge(g,i,j); // can append - each possible k will occur at most once
}
static void Method_A_double(double nr, double Nr, double offset, graph_t g) {
// ACM Trans Math Soft 13, 58 (1987)
// Method A page 65. For n/N large.
// offset is added to each output
// do everything in double to get 2^53 range
double v,quot,top=Nr-nr,s,k=-1.0;
while (nr>1) {
v=VN_GRAPH_UNIFORM_01;
s=0.0;
quot=top/Nr;
while (quot>v) {
s++;
top--;
Nr--;
quot*=top/Nr;
}
//if (s==0.0) fprintf(stderr,"!! Nr=%g nr=%g k=%g quot=%g v=%g\n",Nr,nr,k,quot,v),exit(1);
k+=s+1; // skip
graph_add_edge1(g,k+offset);
Nr--;
nr--;
}
k+=1+floor(Nr*VN_GRAPH_UNIFORM_01);
graph_add_edge1(g,k+offset);
}
static void Method_D_double(double nr, double Nr, graph_t g) {
// ACM Trans Math Soft 13, 58 (1987)
// Method D page 66. For n/N small.
// do everything in double to get 2^53 range
double ninv=1.0/nr,nmin1inv,u,x,vprime,y1,y2,top,bot,qu1r=Nr-nr+1.0,k=-1.0,s,t,limit,negalphainv=-13.0,threshold=-negalphainv*nr;
vprime=pow(VN_GRAPH_UNIFORM_01,ninv);
while (nr>1 && threshold<Nr) {
nmin1inv=1.0/(nr-1.0);
while (1) {
while (1) {
x=Nr*(1.0-vprime);
s=floor(x);
if (s<qu1r) break;
vprime=pow(VN_GRAPH_UNIFORM_01,ninv);
}
u=VN_GRAPH_UNIFORM_01;
y1=pow(u*Nr/qu1r,nmin1inv);
vprime=y1*(1.0-x/Nr)*(qu1r/(qu1r-s));
if (vprime<=1.0) break;
y2=1.0;
top=Nr-1.0;
if (nr-1>s) {
bot=Nr-nr;
limit=Nr-s;
} else {
bot=Nr-s-1.0;
limit=qu1r;
}
for (t=Nr-1; t>=limit; t--) {
y2*=top/bot;
top--;
bot--;
}
if (Nr/(Nr-x)>=y1*pow(y2,nmin1inv)) {
vprime=pow(VN_GRAPH_UNIFORM_01,nmin1inv);
break;
}
vprime=pow(VN_GRAPH_UNIFORM_01,ninv);
}
k+=s+1; // skip
graph_add_edge1(g,k);
Nr-=s+1.0;
nr--;
ninv=nmin1inv;
qu1r-=s;
threshold+=negalphainv;
}
if (nr>1) {
//fprintf(stderr,"A: Nr=%g k=%g nr=%g offset=%g\n",Nr,k,nr,k);
if (k==-1) Method_A_double(nr,Nr,0,g); // outside threshold
else Method_A_double(nr,Nr-k,k,g);
} else {
s=1+floor(Nr*vprime);
k+=s; // skip
graph_add_edge1(g,k);
}
}
void graph_gnm(graph_t g, unsigned long m) {
/* KMB 2006 Dec 11
Return a random graph from G_{n,m}.
Gives a graph picked uniformly at random out of the set of all graphs
with n nodes and m edges.
Parameters:
- m: the number of edges
*/
graph_empty(g);
double nr=m,Nr=g->nnodes*(g->nnodes-1.0)/2.0;
//Method_A_double(nr,Nr,0,g); // slow
Method_D_double(nr,Nr,g); // fast!
}
void graph_grg(graph_t g, double r) {
// slow
int i,j,n=g->nnodes;
double r2=r*r,dx,dy,x[n],y[n];
graph_empty(g);
for (i=0; i<n; i++) {
x[i]=UNIFORM_01;
y[i]=UNIFORM_01;
}
for (i=0; i<n; i++)
for (j=i+1; j<n; j++) {
dx=x[i]-x[j];
dy=y[i]-y[j];
if (dx*dx+dy*dy<r2) graph_append_edge(g,i,j);
}
}
typedef struct { double x,y; } xy;
static int cmp(const void *a, const void *b) {
xy *aa=(xy*)a;
xy *bb=(xy*)b;
return aa->x<bb->x?-1:(aa->x>bb->x?1:0);
}
static double torus_distance2(double dx, double dy) {
if (dx>0.5) dx=1.0-dx;
if (dy>0.5) dy=1.0-dy;
return dx*dx+dy*dy;
}
void graph_grg_torus(graph_t g, double r) {
int i,j,n=g->nnodes;
double r2=r*r,x[n],y[n];
graph_empty(g);
for (i=0; i<n; i++) {
x[i]=UNIFORM_01;
y[i]=UNIFORM_01;
}
for (i=0; i<n; i++)
for (j=i+1; j<n; j++)
if (torus_distance2(fabs(x[i]-x[j]),fabs(y[i]-y[j]))<r2) graph_append_edge(g,i,j);
}
void graph_lognormal_grg_torus(graph_t g, double r, double alpha) {
// nodes d apart are joined with probability (1-erf(log(d/r)/alpha))/2
// so alpha=0 corresponds to GRG(n,r), but alpha>0 makes the cutoff fuzzier.
// log(d/r)/alpha = log(sqrt(d^2)/r)/alpha = (log(d^2)/2-log(r))/alpha
int i,j,n=g->nnodes;
if (alpha==0.0) {
graph_grg_torus(g,r);
return;
}
double d2,x[n],y[n],c=log(r);
graph_empty(g);
for (i=0; i<n; i++) {
x[i]=UNIFORM_01;
y[i]=UNIFORM_01;
}
for (i=0; i<n; i++)
for (j=i+1; j<n; j++) {
d2=torus_distance2(fabs(x[i]-x[j]),fabs(y[i]-y[j]));
if (UNIFORM_01<0.5*(1.0-erf((log(d2)/2-c)/alpha)))
graph_append_edge(g,i,j);
}
}
void graph_lognormal_grg(graph_t g, double r, double alpha) {
// nodes d apart are joined with probability (1-erf(log(d/r)/alpha))/2
// so alpha=0 corresponds to GRG(n,r), but alpha>0 makes the cutoff fuzzier.
// log(d/r)/alpha = log(sqrt(d^2)/r)/alpha = (log(d^2)/2-log(r))/alpha
int i,j,n=g->nnodes;
if (alpha==0.0) {
graph_grg(g,r);
return;
}
double d2,x[n],y[n],c=log(r);
graph_empty(g);
for (i=0; i<n; i++) {
x[i]=UNIFORM_01;
y[i]=UNIFORM_01;
}
for (i=0; i<n; i++)
for (j=i+1; j<n; j++) {
d2=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
if (UNIFORM_01<0.5*(1.0-erf((log(d2)/2-c)/alpha)))
graph_append_edge(g,i,j);
}
}
void graph_grg_torus_sorted(graph_t g, double r) {
int i,j,m=0,n=g->nnodes;
double r2=r*r;
xy z[n];
for (i=0; i<n; i++) {
z[i].x=UNIFORM_01;
z[i].y=UNIFORM_01;
}
qsort(z,n,sizeof(xy),cmp); // sort on x
graph_empty(g);
for (i=0; i<n; i++) {
for (j=i+1; j<n; j++)
if (torus_distance2(fabs(z[i].x-z[j].x),fabs(z[i].y-z[j].y))<r2) graph_append_edge(g,i,j),m++;
}
}
int graph_grg_torus_iterator(unsigned int n, double r, int f(graph_t,int,void*), void* cd) {
/*
Iterate over the ensemble grg(n,r).
Strategy: divide unit square into nb*nb boxes of width>=r. Two nodes
can only be neighbours if they are in the same or abutting boxes.
We then only need on average to look at
(n/(nb*nb)*(n/(nb*nb)-1)/2)*(5*nb*nb)
pairs of nodes, compared to n*(n-1)/2 the slow way.
This method wins when (n/(nb*nb)*(n/(nb*nb)-1)/2)*(5*nb*nb)>n*(n-1)/2,
which gives nb>sqrt(5) when n is large.
Since nb~1/r, the method checks about 5/2*n^2*r^2 pairs.
f() is called for each graph, and should return a zero value to
terminate the iteration. Client data may be passed to f through cd.
Revised 2007 Feb 02.
*/
const int timing=0;
int i,j,k,l,m,ig=0;
double ave,stdev,*x,*y,r2;
size_t initial_length;
unsigned int nb,***boxes,**count;
size_t **alloc;
/* timing */
struct tms buffer;
int tps;
clock_t current_time,start_time;
// internal functions...
int i0,i1,k0,k1;
double dx,dy;
graph_t g=graph_new(n);
if (r<=0.0) {
graph_empty(g);
do { // iterate over graphs
} while ((l=f(g,ig++,cd)));
return l;
}
inline void check_box(int i, int j) {
// check all pairs in box (i,j)
for (i0=0; i0<count[i][j]; i0++) {
k0=boxes[i][j][i0];
for (i1=0; i1<count[i][j]; i1++) {
k1=boxes[i][j][i1];
if (k0<k1) {
dx=fabs(x[k0]-x[k1]);
dy=fabs(y[k0]-y[k1]);
if (torus_distance2(dx,dy)<r2) graph_append_edge(g,k0,k1);
}
}
}
}
inline void check_pairs(int i, int j, int l, int m) {
// check all pairs, one in box (i,j), one in box (l,m)
// (l,m) will always be one of the boxes above, above right, right,
// or below right of (i,j). Thus we get each potential edge once only.
for (i0=0; i0<count[i][j]; i0++) {
k0=boxes[i][j][i0];
for (i1=0; i1<count[l][m]; i1++) {
k1=boxes[l][m][i1];
if (1) {
dx=fabs(x[k0]-x[k1]);
dy=fabs(y[k0]-y[k1]);
if (torus_distance2(dx,dy)<r2) graph_append_edge(g,k0,k1);
}
}
}
}
nb=(int)floor(1.0/r);
r2=r*r;
x=malloc(n*sizeof(double));
y=malloc(n*sizeof(double));
//> lprint(solve((n/(nb*nb)*(n/(nb*nb)-1)/2)*(5*nb*nb)=n*(n-1)/2,nb));
//1/(4+n)*5^(1/2)*((4+n)*n)^(1/2), -1/(4+n)*5^(1/2)*((4+n)*n)^(1/2)
if (nb<4 || nb<1.0/(4.0+n)*sqrt(5*(4.0+n)*n)) { // use 'slow' method
do { // iterate over graphs
graph_empty(g);
for (k=0; k<n; k++) {
x[k]=UNIFORM_01;
y[k]=UNIFORM_01;
}
for (i=0; i<n; i++)
for (j=i+1; j<n; j++)
if (torus_distance2(fabs(x[i]-x[j]),fabs(y[i]-y[j]))<r2)
graph_append_edge(g,i,j);
} while ((l=f(g,ig++,cd)));
free(x);
free(y);
graph_clear(g);
return l;
}
if (timing) {
tps=sysconf(_SC_CLK_TCK); // clock ticks per second
times(&buffer);
start_time=buffer.tms_utime;
}
boxes=malloc(nb*sizeof(unsigned int**));
count=malloc(nb*sizeof(unsigned int*));
alloc=malloc(nb*sizeof(size_t*));
for (i=0; i<nb; i++) {
boxes[i]=malloc(nb*sizeof(unsigned int*));
count[i]=calloc(nb,sizeof(unsigned int)); // initially zero
alloc[i]=malloc(nb*sizeof(size_t)); // allocated length of list boxes[i][j]
}
ave=n/((double)nb*nb); // expected contents of each box
stdev=1.0/sqrt(ave); // typical standard deviation of box contents
initial_length=(size_t)ceil(ave*(1.0+6.0*stdev)); // hope to never have to reallocate
if (initial_length<8) initial_length=8;
for (i=0; i<nb; i++) for (j=0; j<nb; j++) {
alloc[i][j]=initial_length;
boxes[i][j]=malloc(initial_length*sizeof(unsigned int));
}
if (timing) {
times(&buffer);
current_time=buffer.tms_utime;
printf("setup time=%g\n",(current_time-start_time)/(double)tps);
start_time=buffer.tms_utime;
}
do { // iterate over graphs
graph_empty(g);
for (k=0; k<n; k++) { // generate data and count box contents
x[k]=UNIFORM_01;
y[k]=UNIFORM_01;
i=(int)floor(nb*x[k]);
j=(int)floor(nb*y[k]); // node k is in box (i,j)
if (count[i][j]>=alloc[i][j]) {
alloc[i][j]=4+count[i][j];
boxes[i][j]=realloc(boxes[i][j],alloc[i][j]*sizeof(unsigned int));
}
boxes[i][j][count[i][j]++]=k;
}