summaryrefslogtreecommitdiff
path: root/algos.c
blob: 3da67af869c453ca0ed11521b45bede57c8a2e42 (plain) (blame)
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
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>

#include "algos.h"


/* Heuristique de coloriage
 * Trouve un coloriage non-optimal (mais parfois proche) du sous-graphe engendré
 * par s dans le graphe g.
 * Ne donne pas le coloriage, mais juste le nombre de couleurs d'un coloriage possible (nombre que l'on cherche à minimiser)
 */

void color_subgraph_aux(const graph g, int *colors, int *v, int *nneigh, const int n) {
	int i, j, t;

	if (n == 0) return;

	if (n == 1) {
		colors[0] = 0;
		return;
	}

	// Find element with smallest neighbours count
	int x = 0;
	for (i = 1; i < n; i++) {
		if (nneigh[i] < nneigh[x]) x = i;
	}

	// Put that element at the beginning of vector v, so that the remaining subgraph is defined by v[1..]
	if (x != 0) {
		t = v[x];
		v[x] = v[0];
		v[0] = t;

		t = nneigh[x];
		nneigh[x] = nneigh[0];
		nneigh[0] = t;
	}

	// update nneigh
	for (i = 1; i < n; i++) {
		if (set_mem(v[i], graph_neighbours(g, v[0]))) nneigh[i]--;
	}

	// Color remaining subgraph (v[1..])
	color_subgraph_aux(g, colors + 1, v + 1, nneigh + 1, n - 1);

	// Find free color
	colors[0] = -1;
	bool used[n];
	for (i = 0; i < n; i++) used[i] = false;
	for (i = 1; i < n; i++) {
		if (set_mem(v[i], graph_neighbours(g, v[0])))
			used[colors[i]] = true;
	}
	for (i = 0; i < n; i++) {
		if (used[i] == false) {
			colors[0] = i;
			break;
		}
	}
	assert(colors[0] != -1);
}

int color_subgraph(const graph g, set s, int dump_colors) {
	int i, j, m;

	const int n = set_size(s);
	if (n == 0) return 0;

	int colors[n], vertices[n], nneigh[n];
	for (i = 0; i < n; i++)
		colors[i] = -1;
	
	// Put all vertices in vertice vector (order is not important)
	vertices[0] = elt_of_set(s);
	set_remove_ip(vertices[0], s);
	i = 1;
	while (!is_set_empty(s)) {
		vertices[i] = elt_of_set_heur(s, vertices[i-1]);
		set_remove_ip(vertices[i], s);
		i++;
	}

	// Calculate neighbour count for all nodes
	for (i = 0; i < n; i++) nneigh[i] = 0;
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			if (set_mem(vertices[i], graph_neighbours(g, vertices[j])))
				nneigh[i]++;
		}
	}

	// Color graph
	color_subgraph_aux(g, colors, vertices, nneigh, n);

	// Count colors
	m = -1;
	for (i = 0; i < n; i++) {
		assert(colors[i] != -1);
		if (colors[i] > m) m = colors[i];
		if (dump_colors) printf("%d:%d ", vertices[i], colors[i]);
	}
	if (dump_colors) printf("\n");
	
	return m + 1;
}


/*Premier algorithme naïf.
 * g : graphe où on cherche les cliques
 * k : clique actuelle
 * c : noeuds candidats à l'ajout
 * mc : clique maximale actuelle
*/
void max_clique_a(const graph g, set k, set c, set *mc) {
	if (is_set_empty(c)) {
		if (set_size(k) > set_size(*mc)) {
			delete_set(*mc);
			*mc = copy_set(k);
			printf("Found new max clique: "); dump_set(*mc); fflush(stdout);
		}
	} else {
		set cc = copy_set(c);
		while (!(is_set_empty(cc))) {
			int x = elt_of_set(cc);
			set_remove_ip(x, cc);

			set k2 = set_add(x, k);
			set c2 = set_inter(c, graph_neighbours(g, x));
			max_clique_a(g,k2, c2, mc);
			delete_set(k2);
			delete_set(c2);
		}
		delete_set(cc);
	}
}

/** Algorithme avec l'optimisation : on évite d'énumérer plusieurs fois
 * la même clique.
 * g : graphe où on cherche les cliques
 * k : clique actuelle
 * c : noeuds candidats à l'ajout
 * a : noeuds que l'on s'autorise à ajouter
 * mc : clique maximale actuelle
 * */
// Voir notice de convention ci-dessous
void max_clique_b(const graph g, set k, set c, set a, set *mc) {
	if (is_set_empty(c)) {
		if (set_size(k) > set_size(*mc)) {
			delete_set(*mc);
			*mc = copy_set(k);
			printf("Found new max clique: "); dump_set(*mc); fflush(stdout);
		}
	} else {
		while (!(is_set_empty(a))) {
			int x = elt_of_set(a);
			set_remove_ip(x, a);

			set k2 = set_add(x, k);
			set c2 = set_inter(c, graph_neighbours(g, x));
			set a2 = set_inter(a,  graph_neighbours(g, x));
			
			max_clique_b(g,k2, c2, a2, mc);
			delete_set(k2);
			delete_set(c2);
			delete_set(a2);
		}
	}
}

/** Algorithme avec la méthode du pivot
 * la même clique.
 * g : graphe où on cherche les cliques
 * k : clique actuelle
 * c : noeuds candidats à l'ajout
 * a : noeuds que l'on s'autorise à ajouter
 * mc : clique maximale actuelle
 * */
// Convention : lors d'un appel de fonction, les set donnés en
// argument peuvent être modifiés à la guise de l'algorithme
// Il est donc de la responsabilité de l'appellant de vérifier qu'à
// chaque appel les sets sont utilisables et cohérents
void max_clique_c(const graph g, set k, set c, set a, set *mc) {
	// If we have no chance of improving our max clique, exit
	if (set_size(k) + set_size(c) <= set_size(*mc)) return;

	// If we have improved our clique, great
	if (set_size(k) > set_size(*mc)) {
		delete_set(*mc);
		*mc = copy_set(k);
		printf("Found new max clique: "); dump_set(*mc); fflush(stdout);
	}

	// If we have no possibility to explore, return
	if (is_set_empty(c)) return;

	// Find u that maximises |C inter Gamma(u)|
	set c_it = copy_set(c);
	int u = elt_of_set(c_it), n = 0;
	set_remove_ip(u, c_it);

	{	set temp = set_inter(c, graph_neighbours(g, u));
		n = set_size(temp);
		delete_set(temp);
	}

	// Explore possibilites
	int heur = u;
	while (!is_set_empty(c_it)) {
		int uprime = elt_of_set_heur(c_it, heur);
		set_remove_ip(uprime, c_it);
		heur = uprime;

		set temp = set_inter(c, graph_neighbours(g, uprime));
		if (set_size(temp) > n) {
			n = set_size(temp);
			u = uprime;
		}
		delete_set(temp);
	}
	delete_set(c_it);


	set t = set_diff(a, graph_neighbours(g, u));
	heur = u;
	while (!is_set_empty(t)) {
		int x = elt_of_set_heur(t, heur);
		heur = x;

		set k2 = set_add(x, k);
		set c2 = set_inter(c, graph_neighbours(g, x));
		set a2 = set_inter(a, graph_neighbours(g, x));
		max_clique_c(g, k2, c2, a2, mc);
		delete_set(a2);
		delete_set(c2);
		delete_set(k2);

		set_remove_ip(x, a);
		delete_set(t);
		t = set_diff(a, graph_neighbours(g, u));
	}
	delete_set(t);
}

void max_clique_c_color(const graph g, set k, set c, set a, set *mc, int prev_size) {
	// If we have no chance of improving our max clique, exit
	if (set_size(k) + set_size(c) <= set_size(*mc)) return;

	// If we have improved our clique, great
	if (set_size(k) > set_size(*mc)) {
		delete_set(*mc);
		*mc = copy_set(k);
		printf("Found new max clique: "); dump_set(*mc); fflush(stdout);
	}

	// If we have no possibility to explore, return
	if (is_set_empty(c)) return;

	if (set_size(c) <= prev_size / 2 && (set_size(c) >= 20 || set_size(c) >= g->N / 24)) {
		prev_size = set_size(c);
		// Color graph : we may have no possibility
		set c_copy = copy_set(c);
		int color_count = color_subgraph(g, c_copy, 0);
		delete_set(c_copy);
		if (set_size(k) + color_count <= set_size(*mc)) return;
	}


	// Find u that maximises |C inter Gamma(u)|
	set c_it = copy_set(c);
	int u = elt_of_set(c_it), n = 0;
	set_remove_ip(u, c_it);

	{	set temp = set_inter(c, graph_neighbours(g, u));
		n = set_size(temp);
		delete_set(temp);
	}

	// Explore possibilites
	int heur = u;
	while (!is_set_empty(c_it)) {
		int uprime = elt_of_set_heur(c_it, heur);
		set_remove_ip(uprime, c_it);
		heur = uprime;

		set temp = set_inter(c, graph_neighbours(g, uprime));
		if (set_size(temp) > n) {
			n = set_size(temp);
			u = uprime;
		}
		delete_set(temp);
	}
	delete_set(c_it);


	set t = set_diff(a, graph_neighbours(g, u));
	heur = u;
	while (!is_set_empty(t)) {
		int x = elt_of_set_heur(t, heur);
		heur = x;

		set k2 = set_add(x, k);
		set c2 = set_inter(c, graph_neighbours(g, x));
		set a2 = set_inter(a, graph_neighbours(g, x));
		max_clique_c_color(g, k2, c2, a2, mc, prev_size);
		delete_set(a2);
		delete_set(c2);
		delete_set(k2);

		set_remove_ip(x, a);
		delete_set(t);
		t = set_diff(a, graph_neighbours(g, u));
	}
	delete_set(t);
}