Orbiter 2022
Combinatorial Objects
rainbow_cliques.cpp
Go to the documentation of this file.
1// rainbow_cliques.cpp
2//
3// Anton Betten
4//
5// started: October 28, 2012
6
7
8
9
10#include "foundations.h"
11
12
13using namespace std;
14
15
16namespace orbiter {
17namespace layer1_foundations {
18namespace graph_theory {
19
20
22{
23 ost_sol = NULL;
24 //f_output_solution_raw = FALSE;
25
26 graph = NULL;
27 CF = NULL;
28 f_color_satisfied = NULL;
30 color_frequency = NULL;
31 //target_depth = 0;
32
33 null();
34}
35
37{
38}
39
41{
42}
43
45{
46 null();
47}
48
50 clique_finder_control *Control,
51 colored_graph *graph,
52 std::ostream &ost_sol,
53 int verbose_level)
54{
55 int f_v = (verbose_level >= 1);
56 int i;
58
59 if (f_v) {
60 cout << "rainbow_cliques::search" << endl;
61 }
62
64 //rainbow_cliques::f_output_solution_raw = f_output_solution_raw;
65
71
72 for (i = 0; i < graph->nb_colors; i++) {
74 }
75
76
78
80 if (f_v) {
81 cout << "rainbow_cliques::search target_depth = " << Control->target_size << endl;
82 }
83
86 FALSE, NULL,
88 verbose_level - 2);
89
95
96 //CF->call_back_after_reduction = call_back_after_reduction;
98
100
101
102 if (Control->f_restrictions) {
103 if (f_v) {
104 cout << "rainbow_cliques::search "
105 "before init_restrictions" << endl;
106 }
107 CF->init_restrictions(Control->restrictions, verbose_level - 2);
108 }
109
110 if (Control->f_tree) {
112 }
113
114 int t0, t1;
115
116 t0 = Os.os_ticks();
117
118 if (f_v) {
119 cout << "rainbow_cliques::search before backtrack_search" << endl;
120 }
121
122
123 CF->backtrack_search(0, 0 /*verbose_level*/);
124
125
126 if (f_v) {
127 cout << "rainbow_cliques::search after backtrack_search" << endl;
128 }
129
130 if (f_v) {
131 cout << "depth : level_counter" << endl;
132 for (i = 0; i < Control->target_size; i++) {
133 cout << setw(3) << i << " : " << setw(6)
134 << CF->level_counter[i] << endl;
135 }
136 }
137
138 if (Control->f_tree) {
140 }
141
144
145
147 Control->nb_sol = CF->solutions.size();
148
149
150 long int nb_sol;
151
152 if (f_v) {
153 cout << "rainbow_cliques::search before CF->get_solutions" << endl;
154 }
155
157 nb_sol, Control->target_size, verbose_level);
158
159 if (f_v) {
160 cout << "rainbow_cliques::search after CF->get_solutions" << endl;
161 }
162 }
163
164
165
166 t1 = Os.os_ticks();
167
168
169 Control->dt = t1 - t0;
170
171
176
177 CF = NULL;
178 f_color_satisfied = NULL;
180 color_frequency = NULL;
181
182 if (f_v) {
183 cout << "rainbow_cliques::search done" << endl;
184 }
185}
186
188 int current_clique_size, int *current_clique,
189 int nb_pts, int &reduced_nb_pts,
190 int *pt_list, int *pt_list_inv,
191 int *candidates, int verbose_level)
192{
193 int f_v = (verbose_level >= 1);
194 int c, i, j, h, c0, c0_freq, pt;
195
196 if (f_v) {
197 cout << "rainbow_cliques::find_candidates "
198 "nb_pts = " << nb_pts << endl;
199 }
200 reduced_nb_pts = nb_pts;
201
202 // determine the array color_frequency[].
203 // color_frequency[i] is the frequency of points with color i
204 // in the list pt_list[]:
205
207 for (i = 0; i < nb_pts; i++) {
208 pt = pt_list[i];
209 if (pt >= graph->nb_points) {
210 cout << "rainbow_cliques::find_candidates pt >= nb_points" << endl;
211 exit(1);
212 }
213 for (j = 0; j < graph->nb_colors_per_vertex; j++) {
215 if (c >= graph->nb_colors) {
216 cout << "rainbow_cliques::find_candidates c >= nb_colors" << endl;
217 exit(1);
218 }
219 color_frequency[c]++;
220 }
221 }
222 if (f_v) {
223 cout << "rainbow_cliques::find_candidates color_frequency: ";
225 cout << endl;
226 }
227
228 // Determine the color c0 with the least positive frequency
229 // A frequency of zero means that we cannot complete the partial rainbow clique:
230
231 c0 = -1;
232 c0_freq = 0;
233 for (c = 0; c < graph->nb_colors; c++) {
234 if (f_color_satisfied[c]) {
235 if (color_frequency[c]) {
236 cout << "rainbow_cliques::find_candidates "
237 "satisfied color appears with positive "
238 "frequency" << endl;
239 cout << "current clique:";
240 Int_vec_print(cout, current_clique, current_clique_size);
241 cout << endl;
242 exit(1);
243 }
244 }
245 else {
246 if (color_frequency[c] == 0) {
247 return 0;
248 }
249 if (c0 == -1) {
250 c0 = c;
251 c0_freq = color_frequency[c];
252 }
253 else {
254 if (color_frequency[c] < c0_freq) {
255 c0 = c;
256 c0_freq = color_frequency[c];
257 }
258 }
259 }
260 }
261 if (f_v) {
262 cout << "rainbow_cliques::find_candidates "
263 "minimal color is " << c0 << " with frequency "
264 << c0_freq << endl;
265 }
266
267 // And now we collect the points with color c0
268 // in the array candidates:
269 h = 0;
270 for (i = 0; i < nb_pts; i++) {
271 for (j = 0; j < graph->nb_colors_per_vertex; j++) {
272 c = graph->point_color[pt_list[i] * graph->nb_colors_per_vertex + j];
273 if (c == c0) {
274 break;
275 }
276 }
277 if (j < graph->nb_colors_per_vertex) {
278 candidates[h++] = pt_list[i];
279 }
280 }
281 if (h != c0_freq) {
282 // this should not happen.
283 // I may happen if the coloring is not correct.
284 // For instance, each color can appear at most once for each vertex (i.e., no repeats allowed)
285
286 cout << "rainbow_cliques::find_candidates h != c0_freq" << endl;
287 cout << "h=" << h << endl;
288 cout << "c0_freq=" << c0_freq << endl;
289 cout << "c0=" << c0 << endl;
290 cout << "nb_pts=" << nb_pts << endl;
291 cout << "nb_colors=" << graph->nb_colors << endl;
292 cout << "nb_points=" << graph->nb_points << endl;
293 cout << "nb_colors_per_vertex=" << graph->nb_colors_per_vertex << endl;
294 cout << "current_clique_size=" << current_clique_size << endl;
295 cout << "color_frequency=";
297 cout << endl;
298 cout << "f_color_satisfied=";
300 cout << endl;
301 cout << "current clique:";
302 Int_vec_print(cout, current_clique, current_clique_size);
303 cout << endl;
304 cout << "c : f_color_satisfied[c] : color_frequency[c]" << endl;
305 for (c = 0; c < graph->nb_colors; c++) {
306 cout << c << " : " << f_color_satisfied[c] << " : " << color_frequency[c] << endl;
307 }
308 exit(1);
309 }
310
311 // mark color c0 as chosen:
312 color_chosen_at_depth[current_clique_size] = c0;
313
314 // we return the size of the candidate set:
315 return c0_freq;
316}
317
319 int *current_clique, int verbose_level)
320{
321 int i;
322
323 for (i = 0; i < Control->target_size; i++) {
324 *ost_sol << current_clique[i] << " ";
325 }
326 *ost_sol << endl;
327}
328
330 int *current_clique, int verbose_level)
331{
332 int i;
333
335 for (i = 0; i < graph->user_data_size; i++) {
336 *ost_sol << graph->user_data[i] << " ";
337 }
338 for (i = 0; i < Control->target_size; i++) {
339 *ost_sol << graph->points[current_clique[i]] << " ";
340 }
341 *ost_sol << endl;
342}
343
344
346 clique_finder *CF, int verbose_level)
347{
348 int f_v = FALSE; //(verbose_level >= 1);
349
350 //cout << "call_back_colored_graph_clique_found" << endl;
351
353
354 if (f_v) {
355 int i, j, pt, c;
356
357 cout << "call_back_colored_graph_clique_found clique";
359 cout << endl;
360 for (i = 0; i < CF->Control->target_size; i++) {
361 pt = CF->current_clique[i];
362 cout << i << " : " << pt << " : ";
363 for (j = 0; j < R->graph->nb_colors_per_vertex; j++) {
364 c = R->graph->point_color[pt * R->graph->nb_colors_per_vertex + j];
365 cout << c;
366 if (j < R->graph->nb_colors_per_vertex) {
367 cout << ", ";
368 }
369 }
370 cout << endl;
371 }
372 }
374 R->clique_found(CF->current_clique, verbose_level);
375 }
376 else {
378 CF->current_clique, verbose_level);
379 }
380}
381
383 int current_clique_size, int *current_clique,
384 int pt, int verbose_level)
385{
386 int f_v = (verbose_level >= 1);
389 int c, j;
390
391 for (j = 0; j < R->graph->nb_colors_per_vertex; j++) {
392 c = R->graph->point_color[pt * R->graph->nb_colors_per_vertex + j];
393 if (R->f_color_satisfied[c]) {
394 cout << "call_back_colored_graph_add_point "
395 "color already satisfied" << endl;
396 exit(1);
397 }
398 R->f_color_satisfied[c] = TRUE;
399 }
400 if (f_v) {
401 cout << "call_back_colored_graph_add_point "
402 "add_point " << pt << " at depth "
403 << current_clique_size << endl;
404 }
405}
406
408 int current_clique_size, int *current_clique,
409 int pt, int verbose_level)
410{
411 int f_v = (verbose_level >= 1);
414 int c, j;
415
416 for (j = 0; j < R->graph->nb_colors_per_vertex; j++) {
417 c = R->graph->point_color[pt * R->graph->nb_colors_per_vertex + j];
418 if (!R->f_color_satisfied[c]) {
419 cout << "call_back_colored_graph_delete_point "
420 "color not satisfied" << endl;
421 exit(1);
422 }
423 R->f_color_satisfied[c] = FALSE;
424 }
425 if (f_v) {
426 cout << "call_back_colored_graph_delete_point "
427 "delete_point " << pt << " at depth "
428 << current_clique_size << endl;
429 }
430}
431
433 int current_clique_size, int *current_clique,
434 int nb_pts, int &reduced_nb_pts,
435 int *pt_list, int *pt_list_inv,
436 int *candidates, int verbose_level)
437{
438 //verbose_level = 1;
439 int f_v = (verbose_level >= 1);
441 int ret;
442
444
445 int tmp_nb_points;
446
447 if (f_v) {
448 cout << "call_back_colored_graph_find_candidates "
449 "before call_back_additional_test_function" << endl;
450 }
452 current_clique_size, current_clique,
453 nb_pts, tmp_nb_points,
454 pt_list, pt_list_inv,
455 verbose_level);
456
457 nb_pts = tmp_nb_points;
458
459 if (f_v) {
460 cout << "call_back_colored_graph_find_candidates "
461 "after call_back_additional_test_function "
462 "nb_pts = " << nb_pts << endl;
463 }
464
465 }
466
467 if (f_v) {
468 cout << "call_back_colored_graph_find_candidates "
469 "before R->find_candidates" << endl;
470 }
471 ret = R->find_candidates(current_clique_size, current_clique,
472 nb_pts, reduced_nb_pts,
473 pt_list, pt_list_inv,
474 candidates, verbose_level);
475 if (f_v) {
476 cout << "call_back_colored_graph_find_candidates "
477 "after R->find_candidates" << endl;
478 }
479
480 return ret;
481}
482
483
484}}}
485
486
487
void set_print(std::ostream &ost, int *v, int len)
Definition: int_vec.cpp:1043
parameters that control the clique finding process
Definition: graph_theory.h:32
void(* call_back_additional_test_function)(rainbow_cliques *R, void *user_data, int current_clique_size, int *current_clique, int nb_pts, int &reduced_nb_pts, int *pt_list, int *pt_list_inv, int verbose_level)
Definition: graph_theory.h:71
int restrictions[CLIQUE_FINDER_CONTROL_MAX_RESTRICTIONS *3]
Definition: graph_theory.h:60
finds all cliques of a certain size in a graph
Definition: graph_theory.h:115
void init(clique_finder_control *Control, std::string &label, int n, int f_has_adj_list, int *adj_list_coded, int f_has_bitvector, data_structures::bitvector *Bitvec_adjacency, int verbose_level)
int(* call_back_is_adjacent)(clique_finder *CF, int pt1, int pt2, int verbose_level)
Definition: graph_theory.h:187
int(* call_back_find_candidates)(clique_finder *CF, int current_clique_size, int *current_clique, int nb_pts, int &reduced_nb_pts, int *pt_list, int *pt_list_inv, int *candidates, int verbose_level)
Definition: graph_theory.h:180
void get_solutions(int *&Sol, long int &nb_solutions, int &clique_sz, int verbose_level)
void(* call_back_delete_point)(clique_finder *CF, int current_clique_size, int *current_clique, int pt, int verbose_level)
Definition: graph_theory.h:177
void(* call_back_clique_found)(clique_finder *CF, int verbose_level)
Definition: graph_theory.h:171
void init_restrictions(int *restrictions, int verbose_level)
void(* call_back_add_point)(clique_finder *CF, int current_clique_size, int *current_clique, int pt, int verbose_level)
Definition: graph_theory.h:174
void(* call_back_after_reduction)(clique_finder *CF, int depth, int nb_points, int verbose_level)
Definition: graph_theory.h:191
to search for rainbow cliques in graphs
Definition: graph_theory.h:736
void clique_found_record_in_original_labels(int *current_clique, int verbose_level)
void clique_found(int *current_clique, int verbose_level)
int find_candidates(int current_clique_size, int *current_clique, int nb_pts, int &reduced_nb_pts, int *pt_list, int *pt_list_inv, int *candidates, int verbose_level)
void search(clique_finder_control *Control, colored_graph *graph, std::ostream &ost_sol, int verbose_level)
#define FREE_int(p)
Definition: foundations.h:640
#define Int_vec_zero(A, B)
Definition: foundations.h:713
#define NEW_OBJECT(type)
Definition: foundations.h:638
#define FREE_OBJECT(p)
Definition: foundations.h:651
#define NEW_int(n)
Definition: foundations.h:625
#define TRUE
Definition: foundations.h:231
#define FALSE
Definition: foundations.h:234
#define Int_vec_print(A, B, C)
Definition: foundations.h:685
void call_back_colored_graph_clique_found(clique_finder *CF, int verbose_level)
void call_back_colored_graph_add_point(clique_finder *CF, int current_clique_size, int *current_clique, int pt, int verbose_level)
void call_back_colored_graph_delete_point(clique_finder *CF, int current_clique_size, int *current_clique, int pt, int verbose_level)
int call_back_colored_graph_find_candidates(clique_finder *CF, int current_clique_size, int *current_clique, int nb_pts, int &reduced_nb_pts, int *pt_list, int *pt_list_inv, int *candidates, int verbose_level)
orbiter_kernel_system::orbiter_session * Orbiter
global Orbiter session
the orbiter library for the classification of combinatorial objects