-
Notifications
You must be signed in to change notification settings - Fork 0
/
sig.c
710 lines (613 loc) · 18.3 KB
/
sig.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define MAXNODES 100
#define MAXCOLORS 100
#define MAXEDGES 100*(100-1)
/* SIG Simplified Iterated Greedy graph coloring algorithm C.C. McGeoch 2011
Usage: sig <filename
filename: input file containing algorithm parameters. See
getinput.c for details.
*/
//#define REPORTSTATS 0
//#define REPORTCOLORS 0
//#define DEBUG 0
#define ASSERT(cond, msg) if (! (cond)) {printf("msg \n"); exit(1);} ;
int numnodes=0; /* number of nodes in the graph */
int numcolors=0; /* initialized to equal numnodes, then decreases */
int numedges=0; /* total edges */
/*-------------------------------------------------------------*/
/* Graph and coloring representation */
/* The graph is an adjacency list in two arrays */
/*-------------------------------------------------------------*/
/* vertices named/indexed 1..n; colors named/indexed 1..n */
int edgex[MAXNODES+1]; /* vertex v: index to neighbor array */
int ecount[MAXNODES+1]; /* vertex v: number of neighbors */
int neighbor[MAXEDGES+1]; /* list of adjacent nodes */
int colorof[MAXNODES+1]; /* vertex v: current color */
int curcolors; /* number distinct colors in coloring*/
int curcolorscore; /* sum of current colors in coloring + (curcolors*numnodes) */
int colorcount[MAXNODES+1]; /* color c: number of vertices this color */
int bestcoloring[MAXNODES+1]; /* save best coloring: i is col(v_i) */
int bestcolors;
int bestcolorscore = MAXNODES+1;
int vperm[MAXNODES+1]; /* vertex permutation: pos i is name of v_i */
int cperm[MAXNODES+1]; /* color permutation: pos i is name of c_i */
/*Hand defined to allow for compilation*/
/*Defined by command line*/
FILE *fp = NULL;
int initpolicy = 1;
/*Hard coded for testing*/
int maxiter = 200;
int trials = 1;
int revert = 20;
int target = 1;
float ccutoffs[6]; /* ccutoffs is a 1 by 6 normalized probability vector */
float vcutoffs[4]; /* vcutoffs is a 1 by 4 normalized probability vector */
int initcolors;
int initcolorscore;
/*-------------------------------------------------------*/
/* algorithm statistics */
int checkcount = 0;
/*-----------------------------------------------------------*/
/* Utility functions */
/*print usage----------------------------------------------------*/
void usage() {
printf("sig graph_file initpolicy [options]\n");
printf("--maxiter <int> Max number of iterations, default 1000\n");
printf("--trials <int> Max trials, default 100\n");
printf("--revert <int> Max number of trials before revert, default 100\n");
printf("--target <int> Expected number of colors, default 100\n");
printf("--vweight <int> <int> <int> <int>\n");
printf("--cweight <int> <int> <int> <int> <int> <int>\n");
printf("-h, --help Print this message\n");
}
void parseinput(int argc, char** argv) {
fp = fopen(argv[1], "r");
if (NULL == fp){
printf("Error opening file\n");
exit(1);
}
initpolicy = atoi(argv[2]);
for(int x = 0; x < 6; ++x) {
ccutoffs[x] = 1.0/6.0;
}
for(int x = 0; x < 4; ++x) {
vcutoffs[x] = 1.0/4.0;
}
/*parse flags*/
for(int i = 3; i < argc; ++i) {
if(!strncmp("--maxiter", argv[i], 9)) {
++i;
maxiter = atoi(argv[i]);
} else if (!strncmp("--trials", argv[i], 8)) {
++i;
trials = atoi(argv[i]);
} else if (!strncmp("--revert", argv[i], 8)) {
++i;
revert = atoi(argv[i]);
} else if (!strncmp("--target", argv[i], 8)) {
++i;
target = atoi(argv[i]);
} else if (!strncmp("-h", argv[i], 2) || !strncmp("--help", argv[i], 6)) {
usage();
exit(0);
} else if (!strncmp("--cweight", argv[i], 9)) {
++i;
ccutoffs[0] = atof(argv[i]);
++i;
ccutoffs[1] = atof(argv[i]);
++i;
ccutoffs[2] = atof(argv[i]);
++i;
ccutoffs[3] = atof(argv[i]);
++i;
ccutoffs[4] = atof(argv[i]);
++i;
ccutoffs[5] = atof(argv[i]);
} else if (!strncmp("--vweight", argv[i], 9)) {
++i;
vcutoffs[0] = atof(argv[i]);
++i;
vcutoffs[1] = atof(argv[i]);
++i;
vcutoffs[2] = atof(argv[i]);
++i;
vcutoffs[3] = atof(argv[i]);
} else {
printf("Unknown flag %s \n", argv[i]);
exit(1);
}
}
}
/*---printcolors---------------------------------------------*/
void printcolors(int which)
{
int v;
if (which==0) {// print best
printf("best coloring:\t");
for (v =1; v<= numnodes; v++) {
printf("%d %d\t", v, bestcoloring[v]);
if (v % 10 == 0) printf("\n");
}
printf("\n best %d %d \n", bestcolors, bestcolorscore);
}
else { // print current
printf("current coloring:\t");
for (v =1; v<= numnodes; v++) {
printf("%d %d\t", v, colorof[v]);
if (v % 10 == 0) printf("\n");
}
printf("\n curcolors,score %d %d \n", curcolors, curcolorscore);
}
}
/*---printgraph----------------------------------------------*/
void printgraph()
{
int src;
int ex;
int i;
for (src = 1; src <= numnodes; src++) {
ex = edgex[src];
printf("\n v %d: ", src);
for (i=1; i <= ecount[src]; i++) {
printf("%d \t", neighbor[ex++]);
}
}
printf("\n");
}
/*--------------------------------------------------------------*/
/* getgraph: Read input file and build graph. */
/* Input Format:
First line is integer number of nodes.
Then (nodes are named 1..n):
neighbors of v1, list ending with 0
neighbors of v2, list ending with 0
...
neighbors of vn, list ending with 0
Lines after n+1 inputs are ignored as comments.
*/
void getgraph()
{
int i;
int edgeindex = 1;
int edgecount;
int nodeval;
fscanf(fp,"%d", &numnodes); /* number of nodes in graph */
numcolors = numnodes;
if (numnodes >MAXNODES) {
printf("too many nodes: recompile and try again\n");
exit(1);
}
for (i=1; i <= numnodes; i++) {
edgex[i] = edgeindex;
fscanf(fp, "%d", &nodeval);
edgecount=0;
while (nodeval != 0) {
numedges++; /* total for accounting */
edgecount++; /* total neighbors this node */
neighbor[edgeindex++] = nodeval;
fscanf(fp, "%d", &nodeval);
} // for each neighbor
ecount[i] = edgecount;
} // for each vertex
} //getgraph
/*--------------------------------------------------------------*/
/* Greedy coloring */
/*--checkcolorok----------------------------------------------*/
int checkcolorok(int thenode, int thecolor)
{
int ex = edgex[thenode]; /* index to neighbor list */
int edges = ecount[thenode]; /* number of neighbors */
int e=1;
while (e++ <= edges){
++checkcount;
if (colorof [neighbor[ex++] ] == thecolor) return 0; /* false */
}
return 1;
}
/*---colorit---------------------------------------------------*/
void colorit (int thenode, int thecolor)
{
colorof[thenode] = thecolor;
colorcount[thecolor]++;
}
/*---greedycolorgraph-----------------------------------------------------------*/
/* recolors the graph respecting vperm, cperm, and current colors */
void greedycolorgraph()
{
int v , vertex;
int k, c;
for (k = 1; k <= numnodes; k++) colorcount[k] = 0; // initialize
curcolors=0;
curcolorscore=0;
#ifdef DEBUG
printf("\nbefore greedy color: ");
printcolors(1);
printf("\n vperm is: \t");
for (v = 1; v <= numnodes; v++) printf("%d \t", vperm[v]);
printf("\n cperm is: \t");
for (c = 1; c <= numcolors; c++) printf("%d \t", cperm[c]);
#endif
for (v = 1; v <= numnodes; v++) { // for each vertex
vertex = vperm[v]; // respecting vperm
for (k = 1; k <= numcolors; k++ ) { // for each color
int color = cperm[k] ; // respecting cperm
if (checkcolorok(vertex, color)) { // if ok
colorit(vertex, color); // set colorof
curcolorscore += color; // sum of colors assigned in coloring
if (curcolors < k) curcolors=k; // number of distinct colors assigned
break;
}//if
}//for k
}//for v
curcolorscore += curcolors*numnodes; //finish curcolorscore
if (curcolors < numcolors) numcolors = curcolors; // reduce size of color set
#ifdef DEBUG
printf("\nafter greedy color: ");
printcolors(1);
printf("curcolors %d curcolorscore %d numcolors %d\n", curcolors, curcolorscore, numcolors);
#endif
} // greedycolorgraph
/*---saveAsBest---------------------------------------------------*/
void saveAsBest() {
int v;
for (v=1; v<=numnodes; v++) bestcoloring[v] = colorof[v];
bestcolors=curcolors; // should equal numcolors
bestcolorscore=curcolorscore;
#ifdef DEBUG
printf("save best\n");
#endif
}
/*---revertToBest-------------------------------------------------*/
void revertToBest() {
int v;
for (v=1; v<= numnodes; v++) colorof[v] = bestcoloring[v];
curcolors=bestcolors;
curcolorscore = bestcolorscore;
#ifdef DEBUG
printf("revert \n");
#endif
}
/*--pickRandomColorRule------------------------------------------------*/
int pickRandomColorRule() {
double pr = drand48();
int i;
for (i=0; i < 6; ++i) {
if (pr <= 0) {
break;
}
pr -= ccutoffs[i];
}
return i;
}
/*--pickRandomVertexRule-----------------------------------------------*/
int pickRandomVertexRule() {
double pr = drand48();
int i;
for (i=0; i < 4; ++i){
if(pr <= 0) {
break;
}
pr -= vcutoffs[i];
}
return i;
}
/*---vertexreorder--------------------------------------*/
void vertexreorder(int vr) {
/* vertex re-order puts vertices in groups according to color, by rule:
0: 1..k colors going up
1: k..1 colors going down
2: random color order
3: by decreasing total degree in color set
builds vperm[1..n] vperm[i] names the vertex to be considered ith in turn
*/
int i;
int colorhead[numnodes+1]; // start of each color list
int next[numnodes+1]; // next vertex in color list
int order[numnodes+1]; // temp for ordering colors
int c, k, cc, r;
int vpermx=0;
int vx;
int tmp;
int max;
int v;
// gather vertices according to color
for (k=1; k <= numnodes; k++) { //numnodes = max colors
colorhead[k] = 0;
next[k] = 0;
}
for(v=1; v<= numnodes;v++) {
c = colorof[v];
next[v] = colorhead[c];
colorhead[c] = v;
}
#ifdef DEBUG
printf("vertex rule %d: ", vr);
switch(vr) {
case 0: printf("1..k\n"); break;
case 1: printf("k..1\n"); break;
case 2: printf("random\n"); break;
case 3: printf("decreasing vcount in colorset\n"); break;
}
#endif
//copy into vperm by color group and rule
vpermx = 1; // index into vperm
switch(vr) {
case 0: // vertices by increasing colors (skipping unused colors)
for (c = 1; c <= numnodes; c++) {
vx = colorhead[c];
while (vx != 0) {
vperm[vpermx++] = vx;
vx = next[vx];
}
}// for
break;
case 1: // vertices by decreasing colors (skipping unused colors)
for (c = numnodes; c>= 1; c--) {
vx = colorhead[c];
while (vx != 0) {
vperm[vpermx++] = vx;
vx = next[vx];
}
}//for
break;
case 2: //vertices by random color gropus
for (i = 1; i <= numnodes; i++) order[i] = i; // initialize order
for (i = numnodes; i >= 2; i--){ // random permute
r = (int) (drand48() * i) +1 ; //1..i inclusive
tmp= order[i];
order[i] = order[r];
order[r]=tmp;
}
for (c = 1; c <= numnodes; c++) {//build vperm
cc = order[c];
vx = colorhead[cc];
while (vx != 0){
vperm[vpermx++] = vx;
vx = next[vx];
}
}//for
break;
case 3: //vertices by decreasing vcount in colorset
for (c = 1; c <= numnodes; c++) order[c] = colorcount[c]; // copy it
while ( TRUE ) { // at most maxnodes passes, usually fewer
max = -1;
for (c = 1; c <= numnodes; c++) if (max < order[c]) max = order[c];
for (c = 1; c <= numnodes; c++) { // copy all groups with max colors
if (max == order[c] ) {
vx = colorhead[c];
while (vx != 0) {
vperm[vpermx++] = vx;
vx=next[vx];
}
order[c] = -1 ; //remove it from copy
}//if max
}// c passes
if (max==0) break; // last colorcount was 0, done
}// while true
break;
}//switch
}//vertexreorder
/*-----------------------------------------------------------------*/
/* colorreroder sets the colors in order to be considered */
void colorreorder (int cr) {
/*re-order colors in cperm[1..numcolors] by color rule
0: 1..k up
1: largest color set first
2: smallest color set first
3: random colors
4: down k..1
5: reverse current order
result is array cperm[1..numcolors] (just the front): cperm[i] names the ith color
*/
int i, c, cx;
int r;
int tmp;
int order[numnodes+1];
int coloruse[numnodes+1] ; // which colors are in the mix?
int newcperm[numnodes+1];
int newx=1;
int max;
int min;
for (c = 1; c <= numnodes; c++) coloruse[c] = FALSE;
for (c = 1; c <= numcolors; c++) coloruse[ cperm[c] ] = TRUE; //use this color
#ifdef DEBUG
printf("color reorde rules %d number colors is %d: ", cr, numcolors);
switch(cr) {
case 0: printf("1..k\n"); break;
case 1: printf("largest first\n"); break;
case 2: printf("smallest first\n"); break;
case 3: printf("random\n"); break;
case 4: printf("k..1\n"); break;
case 5: printf("reverse current\n"); break;
}
printf("use these colors: ");
for(c =1; c <= numcolors; c++) printf(" %d\t" , cperm[c]);
printf("\n");
#endif
switch (cr) {
case 0: // 1..k up
cx = 1;
for (c = 1; c<= numnodes; c++) // for all colors in up order
if (coloruse[c] == TRUE) cperm[cx++] = c; // if in use, put it next in cperm
break;
case 1: // largest color set first among those in front of cperm
for (c = 1; c <= numcolors; c++) {
order[ cperm[c] ] = colorcount[ cperm[c] ]; // copy
}
newx=1;
while (TRUE) {
max = -1;
for (c = 1; c <= numcolors; c++) {
if (order[cperm[c] ] > max) max = order[cperm[c] ];
}
if (max<=0) break; // ignore unused colors
for (c = 1; c <= numcolors; c++) {
if (order[cperm[c]] == max) { // copy all colors with max count
newcperm[newx++] = cperm[c];
order[cperm[c] ] = -1; // reset
}
}
}//while
for (c = 1; c <= numcolors; c++) cperm[c] = newcperm[c];
break;
case 2: // smallest colorset first, not including 0 counts
for (c = 1; c <= numcolors; c++) {
order[ cperm[c] ] = colorcount[ cperm[c] ]; // copy `
}
newx=1;
while (TRUE) {
min = numnodes+1 ;
for (c = 1; c <= numcolors; c++)
if (order[cperm[c] ] < min ) min = order[cperm[c] ];
if (min==numnodes+1) break; // done finding colors to copy
for (c = 1; c <= numcolors; c++) {
if (order[cperm[c]] == min) {
newcperm[newx++] = cperm[c];
order[cperm[c] ] = numnodes+2; // reset to higher number
}
}
}// while
for (c = 1; c <= numcolors; c++) cperm[c] = newcperm[c];
break;
case 3: // random order among front colors
for (i = numcolors; i >= 2; i--){
r = (int) (drand48() * i) +1 ; //1..i inclusive
tmp= cperm[i];
cperm[i] = cperm[r];
cperm[r]=tmp;
}
break;
case 4: // down k..1 among front of cperm
cx = 1;
for (c = numnodes; c>= 1; c--) { // for all colors in down order
if (coloruse[c] == 1) cperm[cx++] = c; // if in use, put it next in cperm
}
break;
case 5: // reverse current order :
cx = 1;
for (c = numcolors; c>= 1; c--) newcperm[cx++] = cperm[c];
for (c = 1 ; c <= numcolors; c++) cperm[c] = newcperm[c];
break;
}//switch
/* now cperm[1..numcolors] has the new color permutation */
} // reordercolor
/*---initialcoloring()--------------------------------------------------*/
void initialcoloring()
{
int v,i,r, c, tmp;
for (i = 1; i<= numnodes; i++) vperm[i] = i; //initialize vertex permutation
for (i = 1; i<= numcolors; i++) cperm[i] = i; //initialize color permutation
switch (initpolicy) {
case 1: // n distinct colors
for (v=1; v<=numnodes; v++) {
colorof[v] = v;
colorcount[v]=1;
curcolorscore += v;
}
curcolors=numnodes;
curcolorscore += curcolors * numnodes;
numcolors=curcolors;
break;
case 2: // greedy with random vertex order, color order increasing
for (i = numnodes; i >= 2; i--){
r = (int) (drand48() * i) +1 ; //1..i inclusive
tmp= vperm[i];
vperm[i] = vperm[r];
vperm[r]=tmp;
}
for (v=1; v<= numnodes; v++) {
colorof[v] = 0; //initialize vertices to not colored
}
greedycolorgraph();
break;
case 3: //greedy fixed vertex order
greedycolorgraph();
break;
}//switch
initcolors = curcolors;
initcolorscore = curcolorscore;
}//initialcoloring
/*-----------------------------------------------------------*/
/* SIG: simple iterated greedy coloring */
void sigcolor() {
initialcoloring();
saveAsBest();
#ifdef DEBUG
printf("after initialize\n");
printcolors(0); //best
printcolors(1); //current
#endif
int t = 0; // total iterations
int b = 0; // iterations since last save (new best)
int r = 0; // iterations since most recent (savebest, revert)
int cr=0;
int vr=1; //rules
while ((b < maxiter) && (bestcolors > target)) {
t++; b++; r++;
/* replaces pickRandomColorRule and pickRandomVertexRule */
//vr = (vr+1) % 4;
//cr = (cr+1) % 6;
cr = pickRandomColorRule();
vr = pickRandomVertexRule();
#ifdef DEBUG
printf("\niteration %d color rule %d vertex rule %d\n", t, cr, vr);
#endif
vertexreorder(vr);
colorreorder(cr);
greedycolorgraph(); // recolor, respecting old colors
if (curcolors < bestcolors) {// if this is a new min color count
saveAsBest();
b = 0;
r = 0; // new improvement
}
else if (curcolorscore < bestcolorscore) { // if this is a new min color score
saveAsBest();
b = 0;
r = 0; // new improvement
}
if (r > revert) {
revertToBest();
r=0;
}
}//while
}//sigcolor
/*--main---------------------------------------------------------*/
int main(int argc, char* argv[])
{
if (argc < 3){
usage();
return 1;
} else {
parseinput(argc, argv);
}
// transinput(); // command file/parameters from standard in
getgraph(); // input graph (from file named in command file)
#ifdef DEBUG
printf("the graph:\n");
printgraph();
#endif
int i;
for (i = 0; i < trials; i++) {
checkcount=0;
bestcolors=0;
bestcolorscore=0;
// what else to initialize: maxcolor=0; colorcount=0; checkcou
sigcolor() ;
#ifdef REPORTSTATS
printf("i n m it compares maxcolor colorcount \n");
printf("%d %d %d %d %d %d %d \n",
i, numnodes, numedges, trials,
checkcount, bestcolors, bestcolorscore);
#endif
#ifdef REPORTCOLORS
// printcolors(0);
// printcolors(1);
#endif
printf("%d,%d,%d,%d\n", bestcolorscore, bestcolors, initcolorscore, initcolors);
}//for trials
return(0);
} /* end main */