File: reorder.c

package info (click to toggle)
cliquer 1.21-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 484 kB
  • sloc: ansic: 5,394; makefile: 180
file content (428 lines) | stat: -rw-r--r-- 8,970 bytes parent folder | download | duplicates (2)
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

/*
 * This file contains the vertex reordering routines.
 *
 * Copyright (C) 2002 Sampo Niskanen, Patric �sterg�rd.
 * Licensed under the GNU GPL, read the file LICENSE for details.
 */

#include "reorder.h"

#include <time.h>
#include <sys/times.h>
#include <stdlib.h>

#include <limits.h>


/*
 * reorder_set()
 *
 * Reorders the set s with a function  i -> order[i].
 *
 * Note: Assumes that order is the same size as SET_MAX_SIZE(s).
 */
void reorder_set(set_t s,int *order) {
        set_t tmp;
        int i,j;
        setelement e;

        ASSERT(reorder_is_bijection(order,SET_MAX_SIZE(s)));

        tmp=set_new(SET_MAX_SIZE(s));

        for (i=0; i<(SET_MAX_SIZE(s)/ELEMENTSIZE); i++) {
                e=s[i];
                if (e==0)
                        continue;
                for (j=0; j<ELEMENTSIZE; j++) {
                        if (e&1) {
                                SET_ADD_ELEMENT(tmp,order[i*ELEMENTSIZE+j]);
                        }
                        e = e>>1;
                }
        }
        if (SET_MAX_SIZE(s)%ELEMENTSIZE) {
                e=s[i];
                for (j=0; j<(SET_MAX_SIZE(s)%ELEMENTSIZE); j++) {
                        if (e&1) {
                                SET_ADD_ELEMENT(tmp,order[i*ELEMENTSIZE+j]);
                        }
                        e = e>>1;
                }
        }
        set_copy(s,tmp);
        set_free(tmp);
        return;
}


/*
 * reorder_graph()
 *
 * Reorders the vertices in the graph with function  i -> order[i].
 *
 * Note: Assumes that order is of size g->n.
 */
void reorder_graph(graph_t *g, int *order) {
        int i;
        set_t *tmp_e;
        int *tmp_w;

        ASSERT(reorder_is_bijection(order,g->n));

        tmp_e=malloc(g->n * sizeof(set_t));
        tmp_w=malloc(g->n * sizeof(int));
        for (i=0; i<g->n; i++) {
                reorder_set(g->edges[i],order);
                tmp_e[order[i]]=g->edges[i];
                tmp_w[order[i]]=g->weights[i];
        }
        for (i=0; i<g->n; i++) {
                g->edges[i]=tmp_e[i];
                g->weights[i]=tmp_w[i];
        }
        free(tmp_e);
        free(tmp_w);
        return;
}



/*
 * reorder_duplicate()
 *
 * Returns a newly allocated duplicate of the given ordering.
 */
int *reorder_duplicate(int *order,int n) {
	int *new;

	new=malloc(n*sizeof(int));
	memcpy(new,order,n*sizeof(int));
	return new;
}

/*
 * reorder_invert()
 *
 * Inverts the given ordering so that new[old[i]]==i.
 *
 * Note: Asserts that order is a bijection.
 */
void reorder_invert(int *order,int n) {
	int *new;
	int i;

	ASSERT(reorder_is_bijection(order,n));

	new=malloc(n*sizeof(int));
	for (i=0; i<n; i++)
		new[order[i]]=i;
	for (i=0; i<n; i++)
		order[i]=new[i];
	free(new);
	return;
}

/*
 * reorder_reverse()
 *
 * Reverses the given ordering so that  new[i] == n-1 - old[i].
 */
void reorder_reverse(int *order,int n) {
	int i;

	for (i=0; i<n; i++)
		order[i] = n-1 - order[i];
	return;
}

/*
 * reorder_is_bijection
 *
 * Checks that an ordering is a bijection {0,...,n-1} -> {0,...,n-1}.
 *
 * Returns TRUE if it is a bijection, FALSE otherwise.
 */
boolean reorder_is_bijection(int *order,int n) {
	boolean *used;
	int i;

	used=calloc(n,sizeof(boolean));
	for (i=0; i<n; i++) {
		if (order[i]<0 || order[i]>=n) {
			free(used);
			return FALSE;
		}
		if (used[order[i]]) {
			free(used);
			return FALSE;
		}
		used[order[i]]=TRUE;
	}
	for (i=0; i<n; i++) {
		if (!used[i]) {
			free(used);
			return FALSE;
		}
	}
	free(used);
	return TRUE;
}

/*
 * reorder_ident()
 *
 * Returns a newly allocated identity ordering of size n, ie. order[i]==i.
 */
int *reorder_ident(int n) {
	int i;
	int *order;

	order=malloc(n*sizeof(int));
	for (i=0; i<n; i++)
		order[i]=i;
	return order;
}



/*** Reordering functions for use in clique_options ***/

/*
 * reorder_by_ident()
 *
 * Returns an identity ordering.
 */
int *reorder_by_ident(graph_t *g,boolean weighted) {
	return reorder_ident(g->n);
}

/*
 * reorder_by_reverse()
 *
 * Returns a reverse identity ordering.
 */
int *reorder_by_reverse(graph_t *g,boolean weighted) {
	int i;
	int *order;

	order=malloc(g->n * sizeof(int));
	for (i=0; i < g->n; i++)
		order[i]=g->n-i-1;
	return order;
}

/*
 * reorder_by_greedy_coloring()
 *
 * Equivalent to reorder_by_weighted_greedy_coloring or
 * reorder_by_unweighted_greedy_coloring according to the value of weighted.
 */
int *reorder_by_greedy_coloring(graph_t *g,boolean weighted) {
	if (weighted)
		return reorder_by_weighted_greedy_coloring(g,weighted);
	else
		return reorder_by_unweighted_greedy_coloring(g,weighted);
}


/*
 * reorder_by_unweighted_greedy_coloring()
 *
 * Returns an ordering for the graph g by coloring the clique one
 * color at a time, always adding the vertex of largest degree within
 * the uncolored graph, and numbering these vertices 0, 1, ...
 *
 * Experimentally efficient for use with unweighted graphs.
 */
int *reorder_by_unweighted_greedy_coloring(graph_t *g,boolean weighted) {
	int i,j,v;
	boolean *tmp_used;
	int *degree;   /* -1 for used vertices */
	int *order;
	int maxdegree,maxvertex=0;
	boolean samecolor;

	tmp_used=calloc(g->n,sizeof(boolean));
	degree=calloc(g->n,sizeof(int));
	order=calloc(g->n,sizeof(int));

	for (i=0; i < g->n; i++) {
		for (j=0; j < g->n; j++) {
			ASSERT(!((i==j) && GRAPH_IS_EDGE(g,i,j)));
			if (GRAPH_IS_EDGE(g,i,j))
				degree[i]++;
		}
	}

	v=0;
	while (v < g->n) {
		/* Reset tmp_used. */
		memset(tmp_used,0,g->n * sizeof(boolean));

		do {
			/* Find vertex to be colored. */
			maxdegree=0;
			samecolor=FALSE;
			for (i=0; i < g->n; i++) {
				if (!tmp_used[i] && degree[i] >= maxdegree) {
					maxvertex=i;
					maxdegree=degree[i];
					samecolor=TRUE;
				}
			}
			if (samecolor) {
				order[v]=maxvertex;
				degree[maxvertex]=-1;
				v++;

				/* Mark neighbors not to color with same
				 * color and update neighbor degrees. */
				for (i=0; i < g->n; i++) {
					if (GRAPH_IS_EDGE(g,maxvertex,i)) {
						tmp_used[i]=TRUE;
						degree[i]--;
					}
				}
			}
		} while (samecolor);
	}

	free(tmp_used);
	free(degree);
	return order;
}

/*
 * reorder_by_weighted_greedy_coloring()
 *
 * Returns an ordering for the graph g by coloring the clique one
 * color at a time, always adding the vertex that (in order of importance):
 *  1. has the minimum weight in the remaining graph
 *  2. has the largest sum of weights surrounding the vertex
 *
 * Experimentally efficient for use with weighted graphs.
 */
int *reorder_by_weighted_greedy_coloring(graph_t *g, boolean weighted) {
	int i,j,p=0;
	int cnt;
	int *nwt;    /* Sum of surrounding vertices' weights */
	int min_wt,max_nwt;
	boolean *used;
	int *order;
	
	nwt=malloc(g->n * sizeof(int));
	order=malloc(g->n * sizeof(int));
	used=calloc(g->n,sizeof(boolean));
	
	for (i=0; i < g->n; i++) {
		nwt[i]=0;
		for (j=0; j < g->n; j++)
			if (GRAPH_IS_EDGE(g, i, j))
				nwt[i] += g->weights[j];
	}

	for (cnt=0; cnt < g->n; cnt++) {
		min_wt=INT_MAX;
		max_nwt=-1;
		for (i=g->n-1; i>=0; i--)
			if ((!used[i]) && (g->weights[i] < min_wt))
				min_wt=g->weights[i];
		for (i=g->n-1; i>=0; i--) {
			if (used[i] || (g->weights[i] > min_wt))
				continue;
			if (nwt[i] > max_nwt) {
				max_nwt=nwt[i];
				p=i;
			}
		}
		order[cnt]=p;
		used[p]=TRUE;
		for (j=0; j < g->n; j++)
			if ((!used[j]) && (GRAPH_IS_EDGE(g, p, j)))
				nwt[j] -= g->weights[p];
	}

	free(nwt);
	free(used);

	ASSERT(reorder_is_bijection(order,g->n));

	return order;
}

/*
 * reorder_by_degree()
 *
 * Returns a reordering of the graph g so that the vertices with largest
 * degrees (most neighbors) are first.
 */
int *reorder_by_degree(graph_t *g, boolean weighted) {
	int i,j,v;
	int *degree;
	int *order;
	int maxdegree,maxvertex=0;

	degree=calloc(g->n,sizeof(int));
	order=calloc(g->n,sizeof(int));

	for (i=0; i < g->n; i++) {
		for (j=0; j < g->n; j++) {
			ASSERT(!((i==j) && GRAPH_IS_EDGE(g,i,j)));
			if (GRAPH_IS_EDGE(g,i,j))
				degree[i]++;
		}
	}

	for (v=0; v < g->n; v++) {
		maxdegree=0;
		for (i=0; i < g->n; i++) {
			if (degree[i] >= maxdegree) {
				maxvertex=i;
				maxdegree=degree[i];
			}
		}
		order[v]=maxvertex;
		degree[maxvertex]=-1;  /* used */
/*** Max. degree withing unselected graph:
		for (i=0; i < g->n; i++) {
			if (GRAPH_IS_EDGE(g,maxvertex,i))
				degree[i]--;
		}
***/
	}

	free(degree);
	return order;
}

/*
 * reorder_by_random()
 *
 * Returns a random reordering for graph g.
 * Note: Used the functions rand() and srand() to generate the random
 *       numbers.  srand() is re-initialized every time reorder_by_random()
 *       is called using the system time.
 */
int *reorder_by_random(graph_t *g, boolean weighted) {
	struct tms t;
	int i,r;
	int *new;
	boolean *used;

	srand(times(&t)+time(NULL));

	new=calloc(g->n, sizeof(int));
	used=calloc(g->n, sizeof(boolean));
	for (i=0; i < g->n; i++) {
		do {
			r=rand() % g->n;
		} while (used[r]);
		new[i]=r;
		used[r]=TRUE;
	}
	free(used);
	return new;
}