-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnautycliquer.c
2669 lines (2312 loc) · 62.8 KB
/
nautycliquer.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
/* This file is a concatenation of the files cliquer.c, graph.c
and reorder.c from cliquer-1.21 except the routines for
reading and writing dimacs files.
Also some timing code is commented out because it is not used
by nauty and causes portability problem on non-Unix systems
(thanks to Isuru Fernando).
Some procedures which call cliquer with nauty-format graph
are added. Apart from removing DIMACS, the cliquer procedures
have not been changed except to include nautycliquer.h in
place of the previously included files.
cliquer was kindly provided by Sampo Nisjkanen and Patric Ostergard.
Brendan McKay. Aug 27, 2016
*/
#include "nautycliquer.h"
/*
* This file contains the clique searching routines.
*
* Copyright (C) 2002 Sampo Niskanen, Patric Östergård.
* Licensed under the GNU GPL, read the file LICENSE for details.
* This version covered by nauty&Traces licence, see file COPYRIGHT.
*/
/*
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/times.h>
#include "cliquer.h"
*/
/* Default cliquer options */
static clique_options clique_default_options_struct = {
reorder_by_default, NULL, clique_print_time, NULL, NULL, NULL, NULL, 0
};
clique_options *clique_default_options=&clique_default_options_struct;
/* Calculate d/q, rounding result upwards/downwards. */
#define DIV_UP(d,q) (((d)+(q)-1)/(q))
#define DIV_DOWN(d,q) ((int)((d)/(q)))
/* Global variables used: */
/* These must be saved and restored in re-entrance. */
static int *clique_size; /* c[i] == max. clique size in {0,1,...,i-1} */
static set_t current_clique; /* Current clique being searched. */
static set_t best_clique; /* Largest/heaviest clique found so far. */
#if 0
static struct tms cputimer; /* Timer for opts->time_function() */
static struct timeval realtimer; /* Timer for opts->time_function() */
#endif
static int clique_list_count=0; /* No. of cliques in opts->clique_list[] */
static int weight_multiplier=1; /* Weights multiplied by this when passing
* to time_function(). */
/* List cache (contains memory blocks of size g->n * sizeof(int)) */
static int **temp_list=NULL;
static int temp_count=0;
/*
* Macros for re-entrance. ENTRANCE_SAVE() must be called immediately
* after variable definitions, ENTRANCE_RESTORE() restores global
* variables to original values. entrance_level should be increased
* and decreased accordingly.
*/
static int entrance_level=0; /* How many levels for entrance have occurred? */
#define ENTRANCE_SAVE() \
int *old_clique_size = clique_size; \
set_t old_current_clique = current_clique; \
set_t old_best_clique = best_clique; \
int old_clique_list_count = clique_list_count; \
int old_weight_multiplier = weight_multiplier; \
int **old_temp_list = temp_list; \
int old_temp_count = temp_count;
/*
struct tms old_cputimer; \
struct timeval old_realtimer; \
memcpy(&old_cputimer,&cputimer,sizeof(struct tms)); \
memcpy(&old_realtimer,&realtimer,sizeof(struct timeval))
*/
#define ENTRANCE_RESTORE() \
clique_size = old_clique_size; \
current_clique = old_current_clique; \
best_clique = old_best_clique; \
clique_list_count = old_clique_list_count; \
weight_multiplier = old_weight_multiplier; \
temp_list = old_temp_list;
/*
temp_count = old_temp_count; \
memcpy(&cputimer,&old_cputimer,sizeof(struct tms)); \
memcpy(&realtimer,&old_realtimer,sizeof(struct timeval));
temp_count = old_temp_count;
*/
/* Number of clock ticks per second (as returned by sysconf(_SC_CLK_TCK)) */
static int clocks_per_sec=0;
/* Recursion and helper functions */
static boolean sub_unweighted_single(int *table, int size, int min_size,
graph_t *g);
static int sub_unweighted_all(int *table, int size, int min_size, int max_size,
boolean maximal, graph_t *g,
clique_options *opts);
static int sub_weighted_all(int *table, int size, int weight,
int current_weight, int prune_low, int prune_high,
int min_weight, int max_weight, boolean maximal,
graph_t *g, clique_options *opts);
static boolean store_clique(set_t clique, graph_t *g, clique_options *opts);
static boolean is_maximal(set_t clique, graph_t *g);
static boolean false_function(set_t clique,graph_t *g,clique_options *opts);
/***** Unweighted searches *****/
/*
* Unweighted searches are done separately from weighted searches because
* some effective pruning methods can be used when the vertex weights
* are all 1. Single and all clique finding routines are separated,
* because the single clique finding routine can store the found clique
* while it is returning from the recursion, thus requiring no implicit
* storing of the current clique. When searching for all cliques the
* current clique must be stored.
*/
/*
* unweighted_clique_search_single()
*
* Searches for a single clique of size min_size. Stores maximum clique
* sizes into clique_size[].
*
* table - the order of the vertices in g to use
* min_size - minimum size of clique to search for. If min_size==0,
* searches for a maximum clique.
* g - the graph
* opts - time printing options
*
* opts->time_function is called after each base-level recursion, if
* non-NULL. (NOT IN THIS VERSION)
*
* Returns the size of the clique found, or 0 if min_size>0 and a clique
* of that size was not found (or if time_function aborted the search).
* The largest clique found is stored in current_clique.
*
* Note: Does NOT use opts->user_function of opts->clique_list.
*/
static int unweighted_clique_search_single(int *table, int min_size,
graph_t *g, clique_options *opts) {
#if 0
struct tms tms;
struct timeval timeval;
#endif
int i,j;
int v,w;
int *newtable;
int newsize;
v=table[0];
clique_size[v]=1;
set_empty(current_clique);
SET_ADD_ELEMENT(current_clique,v);
if (min_size==1)
return 1;
if (temp_count) {
temp_count--;
newtable=temp_list[temp_count];
} else {
newtable=malloc(g->n * sizeof(int));
}
for (i=1; i < g->n; i++) {
w=v;
v=table[i];
newsize=0;
for (j=0; j<i; j++) {
if (GRAPH_IS_EDGE(g, v, table[j])) {
newtable[newsize]=table[j];
newsize++;
}
}
if (sub_unweighted_single(newtable,newsize,clique_size[w],g)) {
SET_ADD_ELEMENT(current_clique,v);
clique_size[v]=clique_size[w]+1;
} else {
clique_size[v]=clique_size[w];
}
#if 0
if (opts && opts->time_function) {
gettimeofday(&timeval,NULL);
times(&tms);
if (!opts->time_function(entrance_level,
i+1,g->n,clique_size[v] *
weight_multiplier,
(double)(tms.tms_utime-
cputimer.tms_utime)/
clocks_per_sec,
timeval.tv_sec-
realtimer.tv_sec+
(double)(timeval.tv_usec-
realtimer.tv_usec)/
1000000,opts)) {
temp_list[temp_count++]=newtable;
return 0;
}
}
#endif
if (min_size) {
if (clique_size[v]>=min_size) {
temp_list[temp_count++]=newtable;
return clique_size[v];
}
if (clique_size[v]+g->n-i-1 < min_size) {
temp_list[temp_count++]=newtable;
return 0;
}
}
}
temp_list[temp_count++]=newtable;
if (min_size)
return 0;
return clique_size[v];
}
/*
* sub_unweighted_single()
*
* Recursion function for searching for a single clique of size min_size.
*
* table - subset of the vertices in graph
* size - size of table
* min_size - size of clique to look for within the subgraph
* (decreased with every recursion)
* g - the graph
*
* Returns TRUE if a clique of size min_size is found, FALSE otherwise.
* If a clique of size min_size is found, it is stored in current_clique.
*
* clique_size[] for all values in table must be defined and correct,
* otherwise inaccurate results may occur.
*/
static boolean sub_unweighted_single(int *table, int size, int min_size,
graph_t *g) {
int i;
int v;
int *newtable;
int *p1, *p2;
/* Zero or one vertices needed anymore. */
if (min_size <= 1) {
if (size>0 && min_size==1) {
set_empty(current_clique);
SET_ADD_ELEMENT(current_clique,table[0]);
return TRUE;
}
if (min_size==0) {
set_empty(current_clique);
return TRUE;
}
return FALSE;
}
if (size < min_size)
return FALSE;
/* Dynamic memory allocation with cache */
if (temp_count) {
temp_count--;
newtable=temp_list[temp_count];
} else {
newtable=malloc(g->n * sizeof(int));
}
for (i = size-1; i >= 0; i--) {
v = table[i];
if (clique_size[v] < min_size)
break;
/* This is faster when compiling with gcc than placing
* this in the for-loop condition. */
if (i+1 < min_size)
break;
/* Very ugly code, but works faster than "for (i=...)" */
p1 = newtable;
for (p2=table; p2 < table+i; p2++) {
int w = *p2;
if (GRAPH_IS_EDGE(g, v, w)) {
*p1 = w;
p1++;
}
}
/* Avoid unneccessary loops (next size == p1-newtable) */
if (p1-newtable < min_size-1)
continue;
/* Now p1-newtable >= min_size-1 >= 2-1 == 1, so we can use
* p1-newtable-1 safely. */
if (clique_size[newtable[p1-newtable-1]] < min_size-1)
continue;
if (sub_unweighted_single(newtable,p1-newtable,
min_size-1,g)) {
/* Clique found. */
SET_ADD_ELEMENT(current_clique,v);
temp_list[temp_count++]=newtable;
return TRUE;
}
}
temp_list[temp_count++]=newtable;
return FALSE;
}
/*
* unweighted_clique_search_all()
*
* Searches for all cliques with size at least min_size and at most
* max_size. Stores the cliques as opts declares.
*
* table - the order of the vertices in g to search
* start - first index where the subgraph table[0], ..., table[start]
* might include a requested kind of clique
* min_size - minimum size of clique to search for. min_size > 0 !
* max_size - maximum size of clique to search for. If no upper limit
* is desired, use eg. INT_MAX
* maximal - requires cliques to be maximal
* g - the graph
* opts - time printing and clique storage options
*
* Cliques found are stored as defined by opts->user_function and
* opts->clique_list. opts->time_function is called after each
* base-level recursion, if non-NULL.
*
* clique_size[] must be defined and correct for all values of
* table[0], ..., table[start-1].
*
* Returns the number of cliques stored (not neccessarily number of cliques
* in graph, if user/time_function aborts).
*/
static int unweighted_clique_search_all(int *table, int start,
int min_size, int max_size,
boolean maximal, graph_t *g,
clique_options *opts) {
#if 0
struct timeval timeval;
struct tms tms;
#endif
int i,j;
int v;
int *newtable;
int newsize;
int count=0;
if (temp_count) {
temp_count--;
newtable=temp_list[temp_count];
} else {
newtable=malloc(g->n * sizeof(int));
}
clique_list_count=0;
set_empty(current_clique);
for (i=start; i < g->n; i++) {
v=table[i];
clique_size[v]=min_size; /* Do not prune here. */
newsize=0;
for (j=0; j<i; j++) {
if (GRAPH_IS_EDGE(g,v,table[j])) {
newtable[newsize]=table[j];
newsize++;
}
}
SET_ADD_ELEMENT(current_clique,v);
j=sub_unweighted_all(newtable,newsize,min_size-1,max_size-1,
maximal,g,opts);
SET_DEL_ELEMENT(current_clique,v);
if (j<0) {
/* Abort. */
count-=j;
break;
}
count+=j;
#if 0
if (opts->time_function) {
gettimeofday(&timeval,NULL);
times(&tms);
if (!opts->time_function(entrance_level,
i+1,g->n,min_size *
weight_multiplier,
(double)(tms.tms_utime-
cputimer.tms_utime)/
clocks_per_sec,
timeval.tv_sec-
realtimer.tv_sec+
(double)(timeval.tv_usec-
realtimer.tv_usec)/
1000000,opts)) {
/* Abort. */
break;
}
}
#endif
}
temp_list[temp_count++]=newtable;
return count;
}
/*
* sub_unweighted_all()
*
* Recursion function for searching for all cliques of given size.
*
* table - subset of vertices of graph g
* size - size of table
* min_size - minimum size of cliques to search for (decreased with
* every recursion)
* max_size - maximum size of cliques to search for (decreased with
* every recursion). If no upper limit is desired, use
* eg. INT_MAX
* maximal - require cliques to be maximal (passed through)
* g - the graph
* opts - storage options
*
* All cliques of suitable size found are stored according to opts.
*
* Returns the number of cliques found. If user_function returns FALSE,
* then the number of cliques is returned negative.
*
* Uses current_clique to store the currently-being-searched clique.
* clique_size[] for all values in table must be defined and correct,
* otherwise inaccurate results may occur.
*/
static int sub_unweighted_all(int *table, int size, int min_size, int max_size,
boolean maximal, graph_t *g,
clique_options *opts) {
int i;
int v;
int n;
int *newtable;
int *p1, *p2;
int count=0; /* Amount of cliques found */
if (min_size <= 0) {
if ((!maximal) || is_maximal(current_clique,g)) {
/* We've found one. Store it. */
count++;
if (!store_clique(current_clique,g,opts)) {
return -count;
}
}
if (max_size <= 0) {
/* If we add another element, size will be too big. */
return count;
}
}
if (size < min_size) {
return count;
}
/* Dynamic memory allocation with cache */
if (temp_count) {
temp_count--;
newtable=temp_list[temp_count];
} else {
newtable=malloc(g->n * sizeof(int));
}
for (i=size-1; i>=0; i--) {
v = table[i];
if (clique_size[v] < min_size) {
break;
}
if (i+1 < min_size) {
break;
}
/* Very ugly code, but works faster than "for (i=...)" */
p1 = newtable;
for (p2=table; p2 < table+i; p2++) {
int w = *p2;
if (GRAPH_IS_EDGE(g, v, w)) {
*p1 = w;
p1++;
}
}
/* Avoid unneccessary loops (next size == p1-newtable) */
if (p1-newtable < min_size-1) {
continue;
}
SET_ADD_ELEMENT(current_clique,v);
n=sub_unweighted_all(newtable,p1-newtable,
min_size-1,max_size-1,maximal,g,opts);
SET_DEL_ELEMENT(current_clique,v);
if (n < 0) {
/* Abort. */
count -= n;
count = -count;
break;
}
count+=n;
}
temp_list[temp_count++]=newtable;
return count;
}
/***** Weighted clique searches *****/
/*
* Weighted clique searches can use the same recursive routine, because
* in both cases (single/all) they have to search through all potential
* permutations searching for heavier cliques.
*/
/*
* weighted_clique_search_single()
*
* Searches for a single clique of weight at least min_weight, and at
* most max_weight. Stores maximum clique sizes into clique_size[]
* (or min_weight-1, whichever is smaller).
*
* table - the order of the vertices in g to use
* min_weight - minimum weight of clique to search for. If min_weight==0,
* then searches for a maximum weight clique
* max_weight - maximum weight of clique to search for. If no upper limit
* is desired, use eg. INT_MAX
* g - the graph
* opts - time printing options
*
* opts->time_function is called after each base-level recursion, if
* non-NULL.
*
* Returns 0 if a clique of requested weight was not found (also if
* time_function requested an abort), otherwise returns >= 1.
* If min_weight==0 (search for maximum-weight clique), then the return
* value is the weight of the clique found. The found clique is stored
* in best_clique.
*
* Note: Does NOT use opts->user_function of opts->clique_list.
*/
static int weighted_clique_search_single(int *table, int min_weight,
int max_weight, graph_t *g,
clique_options *opts) {
#if 0
struct timeval timeval;
struct tms tms;
#endif
int i,j;
int v;
int *newtable;
int newsize;
int newweight;
int search_weight;
int min_w;
clique_options localopts;
if (min_weight==0)
min_w=INT_MAX;
else
min_w=min_weight;
if (min_weight==1) {
/* min_weight==1 may cause trouble in the routine, and
* it's trivial to check as it's own case.
* We write nothing to clique_size[]. */
for (i=0; i < g->n; i++) {
if (g->weights[table[i]] <= max_weight) {
set_empty(best_clique);
SET_ADD_ELEMENT(best_clique,table[i]);
return g->weights[table[i]];
}
}
return 0;
}
localopts.time_function=NULL;
localopts.reorder_function=NULL;
localopts.reorder_map=NULL;
localopts.user_function=false_function;
localopts.user_data=NULL;
localopts.clique_list=&best_clique;
localopts.clique_list_length=1;
clique_list_count=0;
v=table[0];
set_empty(best_clique);
SET_ADD_ELEMENT(best_clique,v);
search_weight=g->weights[v];
if (min_weight && (search_weight >= min_weight)) {
if (search_weight <= max_weight) {
/* Found suitable clique. */
return search_weight;
}
search_weight=min_weight-1;
}
clique_size[v]=search_weight;
set_empty(current_clique);
if (temp_count) {
temp_count--;
newtable=temp_list[temp_count];
} else {
newtable=malloc(g->n * sizeof(int));
}
for (i = 1; i < g->n; i++) {
v=table[i];
newsize=0;
newweight=0;
for (j=0; j<i; j++) {
if (GRAPH_IS_EDGE(g,v,table[j])) {
newweight += g->weights[table[j]];
newtable[newsize]=table[j];
newsize++;
}
}
SET_ADD_ELEMENT(current_clique,v);
search_weight=sub_weighted_all(newtable,newsize,newweight,
g->weights[v],search_weight,
clique_size[table[i-1]] +
g->weights[v],
min_w,max_weight,FALSE,
g,&localopts);
SET_DEL_ELEMENT(current_clique,v);
if (search_weight < 0) {
break;
}
clique_size[v]=search_weight;
#if 0
if (opts->time_function) {
gettimeofday(&timeval,NULL);
times(&tms);
if (!opts->time_function(entrance_level,
i+1,g->n,clique_size[v] *
weight_multiplier,
(double)(tms.tms_utime-
cputimer.tms_utime)/
clocks_per_sec,
timeval.tv_sec-
realtimer.tv_sec+
(double)(timeval.tv_usec-
realtimer.tv_usec)/
1000000,opts)) {
set_free(current_clique);
current_clique=NULL;
break;
}
}
#endif
}
temp_list[temp_count++]=newtable;
if (min_weight && (search_weight > 0)) {
/* Requested clique has not been found. */
return 0;
}
return clique_size[table[i-1]];
}
/*
* weighted_clique_search_all()
*
* Searches for all cliques with weight at least min_weight and at most
* max_weight. Stores the cliques as opts declares.
*
* table - the order of the vertices in g to search
* start - first index where the subgraph table[0], ..., table[start]
* might include a requested kind of clique
* min_weight - minimum weight of clique to search for. min_weight > 0 !
* max_weight - maximum weight of clique to search for. If no upper limit
* is desired, use eg. INT_MAX
* maximal - search only for maximal cliques
* g - the graph
* opts - time printing and clique storage options
*
* Cliques found are stored as defined by opts->user_function and
* opts->clique_list. opts->time_function is called after each
* base-level recursion, if non-NULL.
*
* clique_size[] must be defined and correct for all values of
* table[0], ..., table[start-1].
*
* Returns the number of cliques stored (not neccessarily number of cliques
* in graph, if user/time_function aborts).
*/
static int weighted_clique_search_all(int *table, int start,
int min_weight, int max_weight,
boolean maximal, graph_t *g,
clique_options *opts) {
#if 0
struct timeval timeval;
struct tms tms;
#endif
int i,j;
int v;
int *newtable;
int newsize;
int newweight;
if (temp_count) {
temp_count--;
newtable=temp_list[temp_count];
} else {
newtable=malloc(g->n * sizeof(int));
}
clique_list_count=0;
set_empty(current_clique);
for (i=start; i < g->n; i++) {
v=table[i];
clique_size[v]=min_weight; /* Do not prune here. */
newsize=0;
newweight=0;
for (j=0; j<i; j++) {
if (GRAPH_IS_EDGE(g,v,table[j])) {
newtable[newsize]=table[j];
newweight+=g->weights[table[j]];
newsize++;
}
}
SET_ADD_ELEMENT(current_clique,v);
j=sub_weighted_all(newtable,newsize,newweight,
g->weights[v],min_weight-1,INT_MAX,
min_weight,max_weight,maximal,g,opts);
SET_DEL_ELEMENT(current_clique,v);
if (j<0) {
/* Abort. */
break;
}
#if 0
if (opts->time_function) {
gettimeofday(&timeval,NULL);
times(&tms);
if (!opts->time_function(entrance_level,
i+1,g->n,clique_size[v] *
weight_multiplier,
(double)(tms.tms_utime-
cputimer.tms_utime)/
clocks_per_sec,
timeval.tv_sec-
realtimer.tv_sec+
(double)(timeval.tv_usec-
realtimer.tv_usec)/
1000000,opts)) {
set_free(current_clique);
current_clique=NULL;
break;
}
}
#endif
}
temp_list[temp_count++]=newtable;
return clique_list_count;
}
/*
* sub_weighted_all()
*
* Recursion function for searching for all cliques of given weight.
*
* table - subset of vertices of graph g
* size - size of table
* weight - total weight of vertices in table
* current_weight - weight of clique found so far
* prune_low - ignore all cliques with weight less or equal to this value
* (often heaviest clique found so far) (passed through)
* prune_high - maximum weight possible for clique in this subgraph
* (passed through)
* min_size - minimum weight of cliques to search for (passed through)
* Must be greater than 0.
* max_size - maximum weight of cliques to search for (passed through)
* If no upper limit is desired, use eg. INT_MAX
* maximal - search only for maximal cliques
* g - the graph
* opts - storage options
*
* All cliques of suitable weight found are stored according to opts.
*
* Returns weight of heaviest clique found (prune_low if a heavier clique
* hasn't been found); if a clique with weight at least min_size is found
* then min_size-1 is returned. If clique storage failed, -1 is returned.
*
* The largest clique found smaller than max_weight is stored in
* best_clique, if non-NULL.
*
* Uses current_clique to store the currently-being-searched clique.
* clique_size[] for all values in table must be defined and correct,
* otherwise inaccurate results may occur.
*
* To search for a single maximum clique, use min_weight==max_weight==INT_MAX,
* with best_clique non-NULL. To search for a single given-weight clique,
* use opts->clique_list and opts->user_function=false_function. When
* searching for all cliques, min_weight should be given the minimum weight
* desired.
*/
static int sub_weighted_all(int *table, int size, int weight,
int current_weight, int prune_low, int prune_high,
int min_weight, int max_weight, boolean maximal,
graph_t *g, clique_options *opts) {
int i;
int v,w;
int *newtable;
int *p1, *p2;
int newweight;
if (current_weight >= min_weight) {
if ((current_weight <= max_weight) &&
((!maximal) || is_maximal(current_clique,g))) {
/* We've found one. Store it. */
if (!store_clique(current_clique,g,opts)) {
return -1;
}
}
if (current_weight >= max_weight) {
/* Clique too heavy. */
return min_weight-1;
}
}
if (size <= 0) {
/* current_weight < min_weight, prune_low < min_weight,
* so return value is always < min_weight. */
if (current_weight>prune_low) {
if (best_clique)
set_copy(best_clique,current_clique);
if (current_weight < min_weight)
return current_weight;
else
return min_weight-1;
} else {
return prune_low;
}
}
/* Dynamic memory allocation with cache */
if (temp_count) {
temp_count--;
newtable=temp_list[temp_count];
} else {
newtable=malloc(g->n * sizeof(int));
}
for (i = size-1; i >= 0; i--) {
v = table[i];
if (current_weight+clique_size[v] <= prune_low) {
/* Dealing with subset without heavy enough clique. */
break;
}
if (current_weight+weight <= prune_low) {
/* Even if all elements are added, won't do. */
break;
}
/* Very ugly code, but works faster than "for (i=...)" */
p1 = newtable;
newweight = 0;
for (p2=table; p2 < table+i; p2++) {
w = *p2;
if (GRAPH_IS_EDGE(g, v, w)) {
*p1 = w;
newweight += g->weights[w];
p1++;
}
}
w=g->weights[v];
weight-=w;
/* Avoid a few unneccessary loops */
if (current_weight+w+newweight <= prune_low) {
continue;
}
SET_ADD_ELEMENT(current_clique,v);
prune_low=sub_weighted_all(newtable,p1-newtable,
newweight,
current_weight+w,
prune_low,prune_high,
min_weight,max_weight,maximal,
g,opts);
SET_DEL_ELEMENT(current_clique,v);
if ((prune_low<0) || (prune_low>=prune_high)) {
/* Impossible to find larger clique. */
break;
}
}
temp_list[temp_count++]=newtable;
return prune_low;
}
/***** Helper functions *****/
/*
* store_clique()
*
* Stores a clique according to given user options.
*
* clique - the clique to store
* opts - storage options
*
* Returns FALSE if opts->user_function() returned FALSE; otherwise
* returns TRUE.
*/
static boolean store_clique(set_t clique, graph_t *g, clique_options *opts) {
clique_list_count++;
/* clique_list[] */
if (opts->clique_list) {
/*
* This has been a major source of bugs:
* Has clique_list_count been set to 0 before calling
* the recursions?
*/
if (clique_list_count <= 0) {
fprintf(stderr,"CLIQUER INTERNAL ERROR: "
"clique_list_count has negative value!\n");
fprintf(stderr,"Please report as a bug.\n");
abort();
}
if (clique_list_count <= opts->clique_list_length)
opts->clique_list[clique_list_count-1] =
set_duplicate(clique);
}
/* user_function() */
if (opts->user_function) {
if (!opts->user_function(clique,g,opts)) {
/* User function requested abort. */
return FALSE;
}
}
return TRUE;
}
/*
* maximalize_clique()
*
* Adds greedily all possible vertices in g to set s to make it a maximal
* clique.
*
* s - clique of vertices to make maximal
* g - graph
*
* Note: Not very optimized (uses a simple O(n^2) routine), but is called
* at maximum once per clique_xxx() call, so it shouldn't matter.
*/
static void maximalize_clique(set_t s,graph_t *g) {
int i,j;
boolean add;
for (i=0; i < g->n; i++) {
add=TRUE;