Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/graphs/cliquer/cl.c
8817 views
1
#include "cl.h"
2
#include "cliquer/cliquer.h"
3
4
static int maximal;
5
static int sage_clique_count=0;
6
static set_t *sage_clique_list;
7
static int sage_clique_list_size=0;
8
9
// As the global variables remain between two SAGE call, they
10
// have to be reset each time
11
void sage_reset_global_variables(){
12
maximal=FALSE; // reachable from read_options
13
sage_clique_count=0;
14
sage_clique_list_size=0;
15
}
16
17
static boolean sage_record_clique_func(set_t s,graph_t *g,clique_options *opts) {
18
if (sage_clique_count>=sage_clique_list_size) {
19
sage_clique_list=realloc(sage_clique_list,(sage_clique_list_size+512) *
20
sizeof(set_t));
21
sage_clique_list_size+=512;
22
}
23
sage_clique_list[sage_clique_count]=set_duplicate(s);
24
sage_clique_count++;
25
return TRUE;
26
}
27
28
static int *(*reorder)(graph_t *, boolean)=reorder_by_default;
29
static int quiet=0;
30
// The opt structure has to be initialised in each SAGE function
31
clique_options * sage_init_clique_opt(){
32
sage_reset_global_variables();
33
clique_options *opts;
34
quiet++;
35
opts=malloc(sizeof(clique_options));
36
if (quiet)
37
opts->time_function=NULL;
38
else
39
opts->time_function=clique_print_time;
40
opts->output=stderr;
41
opts->reorder_function=reorder;
42
opts->reorder_map=NULL;
43
opts->user_function=sage_record_clique_func;
44
opts->user_data=NULL;
45
opts->clique_list=NULL;
46
opts->clique_list_length=0;
47
return opts;
48
}
49
50
// Computes a maximum clique of the graph g and return its size
51
// The table list contains the ID of the vertices
52
int sage_clique_max(graph_t *g,int **list){
53
sage_reset_global_variables();
54
quiet++;
55
set_t s;
56
int i,l;
57
clique_options *opts = sage_init_clique_opt();
58
s=clique_unweighted_find_single(g,/*min_weight*/0,
59
/*max_weight*/0,/*maximal*/TRUE,
60
opts);
61
free(opts);
62
63
// Writing the answer into a int [] to be read by Sage
64
int size=set_size(s);
65
*list=malloc(sizeof(int)*size);
66
l=0;
67
for (i=0; i<SET_MAX_SIZE(s); i++) {
68
if (SET_CONTAINS(s,i)) {
69
*((*list)+l)=i;
70
l++;
71
}
72
}
73
return size;
74
}
75
76
int sage_all_clique_max(graph_t *g,int **list){
77
sage_reset_global_variables();
78
quiet++;
79
maximal=TRUE;
80
int i,j,l;
81
82
clique_options *opts = sage_init_clique_opt();
83
clique_unweighted_find_all(g,/*min_weight*/0,/*max_weight*/0,
84
maximal,opts);
85
free(opts);
86
87
int size=set_size(sage_clique_list[0]);
88
*list=malloc(sizeof(int)*(size+1)*sage_clique_count);
89
l=0;
90
91
for (j=0; j<sage_clique_count; j++) {
92
for (i=0; i<SET_MAX_SIZE(sage_clique_list[j]); i++) {
93
if (SET_CONTAINS(sage_clique_list[j],i)) {
94
*((*list)+l)=i;
95
l++;
96
}
97
}
98
set_free(sage_clique_list[j]);
99
*((*list)+l)=-1;
100
l++;
101
}
102
return (1+size)*sage_clique_count;
103
}
104
105
int sage_clique_number(graph_t *g){
106
sage_reset_global_variables();
107
maximal=TRUE;
108
clique_options *opts;
109
opts=sage_init_clique_opt();
110
int n = clique_unweighted_max_weight(g,opts);
111
free(opts);
112
opts = NULL;
113
return n;
114
}
115
116