File: cl.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 (403 lines) | stat: -rw-r--r-- 10,610 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

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#ifdef ENABLE_LONG_OPTIONS
#include <getopt.h>
#endif

#include "cliquer.h"


#define TRYFORHELP  "Try `%s -h' for more information.\n",argv[0]

static void printhelp(char *prog);
static void read_options(int argc, char **argv);
static void print_search(graph_t *g);
static boolean record_clique_func(set_t s,graph_t *g,clique_options *opts);
static boolean print_clique_func(set_t s,graph_t *g,clique_options *opts);
static void print_clique(set_t s,graph_t *g);


/* Options, changed by command-line arguments. */
static boolean find_all=FALSE;
static int min_weight=0;
static boolean min_weight_set=FALSE;
static int max_weight=0;
static boolean max_weight_set=FALSE;
static boolean maximal=FALSE;
static boolean unweighted=FALSE;
static boolean number1=TRUE;
static int quiet=0;
static boolean only_weight=FALSE;
static int *(*reorder)(graph_t *, boolean)=reorder_by_default;
static char *file;

/* Dynamically allocated storage for cliques. */
static set_t *clique_list;
static int clique_count=0;
static int clique_list_size=0;


int main(int argc, char **argv) {
	FILE *fp;
	graph_t *g;
	set_t s;
	clique_options *opts;
	int i,j;

	if (argc<=1)
		printhelp(argv[0]);
	read_options(argc,argv);

	if (file[0]=='-' && file[1]==0) {
		fp=stdin;
		if (quiet<=1)
			fprintf(stderr,"Reading graph from stdin...");
	} else {
		fp=fopen(file,"rb");
		if (fp==NULL) {
			perror(file);
			exit(2);
		}
		if (quiet<=1)
			fprintf(stderr,"Reading graph from %s...",file);
	}
	g=graph_read_dimacs(fp);
	if (g==NULL) {
		fprintf(stderr,"Error in graph file.\n");
		return 1;
	}
	if (quiet<=1)
		fprintf(stderr,"OK\n");
	fclose(fp);

	/* Set stdout to be line-buffered even if redirected. */
	setvbuf(stdout,(char *)NULL,_IOLBF,0);

	/* Initialize out clique_options */
	opts=malloc(sizeof(clique_options));
	if (quiet)
		opts->time_function=NULL;
	else
		opts->time_function=clique_print_time;
	opts->output=stderr;
	opts->reorder_function=reorder;
	opts->reorder_map=NULL;
	if (quiet)
		opts->user_function=print_clique_func;
	else
		opts->user_function=record_clique_func;
	opts->user_data=NULL;
	opts->clique_list=NULL;
	opts->clique_list_length=0;

	/* Report what we are doing. */
	if (quiet<=1)
		print_search(g);

	if (only_weight) {
		if (unweighted) {
			j=clique_unweighted_max_weight(g,opts);
			printf("Largest clique: %d\n",j);
		} else {
			j=clique_max_weight(g,opts);
			printf("Heaviest clique: %d\n",j);
		}
	} else if (find_all) {
		if (unweighted) {
			clique_unweighted_find_all(g,min_weight,max_weight,
						   maximal,opts);
		} else {
			clique_find_all(g,min_weight,max_weight,maximal,opts);
		}
		if (quiet<=0) {
			fprintf(stderr,"Found %d clique%s:\n",
				clique_count,(clique_count==1)?"":"s");
			for (i=0; i<clique_count; i++) {
				print_clique(clique_list[i],g);
			}
		}
	} else {
		if (unweighted) {
			s=clique_unweighted_find_single(g,min_weight,
							max_weight,maximal,
							opts);
		} else {
			s=clique_find_single(g,min_weight,max_weight,maximal,
					     opts);
		}
		if (s==NULL) {
			if (quiet<=1)
				fprintf(stderr,"No such clique found.\n");
			return 0;
		}
		print_clique(s,g);
	}

	return 0;
}


/*
 * Prints the help message to stdout.  prog is the program name
 * (from argv[0]).
 */
static void printhelp(char *prog) {
	printf("Usage:  %s [options] <dimacs-file>\n"
	       "\n"
	       "<dimacs-file> is the ASCII or binary DIMACS-format file from which to read\n"
	       "the graph.  Options are the following:\n"
	       "\n"
	       " -h    --help         This help text.\n"
	       " -a    --all          Find all cliques.\n"
	       " -s    --single       Find only one clique (default).\n"
	       " -w    --weight       Tell only maximum weight (no faster than -s).\n"
	       " -m N  --min N        Search for cliques with weight at least N.  If N=0,\n"
	       "                      searches for maximum weight clique (default).\n"
	       " -M N  --max N        Search for cliques with weight at most N.  If N=0,\n"
	       "                      no limit is imposed (default).  N being positive is\n"
	       "                      incompatible with \"--min 0\" (\"--min 1\" is assumed).\n"
	       " -x    --maximal      Require cliques to be maximal.\n"
	       " -u    --unweighted   Assume weight 1 for all vertices.\n"
	       " -0    --from-0       Number vertices 0 to n-1 instead of 1 to n when writing.\n"
	       " -r F  --reorder F    Reorder with function F.  See below for details.\n"
	       " -q    --quiet        Suppresses progress output.  Specifying -q twice\n"
	       "                      suppresses all output except the actual result.\n"
	       "\n"
	       "Available reordering functions are the following:\n"
	       "\n"
	       "      none        No ordering (same order as in the file).\n"
	       "      reverse     Reverse order as in the file.\n"
	       "      default     One of the two below, depending on weightedness.\n"
	       "      unweighted-coloring   Coloring method efficient for unweighted graphs.\n"
	       "      weighted-coloring     Coloring method efficient for weighted graphs.\n"
	       "      degree      Order by ascending degree.\n"
	       "      random      Random order.\n"
	       "\n",
	       prog);
#ifndef ENABLE_LONG_OPTIONS
	printf("(Long options not enabled during compile.)\n\n");
#endif
	exit(0);
}


/*
 * Reads the command-line options and sets the global options accordingly.
 */
static void read_options(int argc, char **argv) {
	int c;
	char *ptr;

	while (TRUE) {
#ifdef ENABLE_LONG_OPTIONS
		int option_index = 0;
		static struct option long_options[] = {
			{ "all", no_argument, NULL, 'a' },
			{ "single", no_argument, NULL, 's' },
			{ "weight", no_argument, NULL, 'w' },
			{ "min", required_argument, NULL, 'm' },
			{ "max", required_argument, NULL, 'M' },
			{ "maximal", no_argument, NULL, 'x' },
			{ "unweighted", no_argument, NULL, 'u' },
			{ "reorder", required_argument, NULL, 'r' },
			{ "from-0", no_argument, NULL, '0' },
			{ "quiet", no_argument, NULL, 'q' },
			{ "help", no_argument, NULL, 'h' },
			{ 0,0,0,0 }
		};

		c=getopt_long(argc,argv,"aswm:M:xur:1qh",
			      long_options,&option_index);
#else  /* !ENABLE_LONG_OPTIONS */
		c=getopt(argc,argv,"aswm:M:xur:1qh-");
#endif /* !ENABLE_LONG_OPTIONS */
		if (c==-1)
			break;

		switch (c) {
		case 0:
			break;
		case 'a':
			find_all=TRUE;
			break;
		case 's':
			find_all=FALSE;
			break;
		case 'w':
			only_weight=TRUE;
			break;
		case 'm':
			min_weight=strtol(optarg,&ptr,10);
			if ((*optarg == 0) || (*ptr != 0) || (min_weight<0)) {
				fprintf(stderr,"Bad argument: %s\n",optarg);
				fprintf(stderr,TRYFORHELP);
				exit(1);
			}
			if (min_weight==0 && max_weight>0) {
				if (max_weight_set) {
					fprintf(stderr,"Error: --max >0 is "
						"incompatible with --min 0\n");
					fprintf(stderr,TRYFORHELP);
					exit(1);
				}
				max_weight=0;
			}
			min_weight_set=TRUE;
			break;
		case 'M':
			max_weight=strtol(optarg,&ptr,10);
			if ((*optarg == 0) || (*ptr != 0) || (max_weight<0)) {
				fprintf(stderr,"Bad argument: %s\n",optarg);
				fprintf(stderr,TRYFORHELP);
				exit(1);
			}
			if (max_weight>0 && min_weight==0) {
				if (min_weight_set) {
					fprintf(stderr,"Error: --max >0 is "
						"incompatible with --min 0\n");
					fprintf(stderr,TRYFORHELP);
					exit(1);
				}
				min_weight=1;
			}
			max_weight_set=TRUE;
			break;
		case 'x':
			maximal=TRUE;
			break;
		case 'u':
			unweighted=TRUE;
			break;
		case 'r':
			if (strcasecmp(optarg,"none")==0)
				reorder=NULL;
			else if (strcasecmp(optarg,"reverse")==0)
				reorder=reorder_by_reverse;
			else if (strcasecmp(optarg,"default")==0)
				reorder=reorder_by_default;
			else if (strcasecmp(optarg,"unweighted-coloring")==0)
				reorder=reorder_by_unweighted_greedy_coloring;
			else if (strcasecmp(optarg,"weighted-coloring")==0)
				reorder=reorder_by_weighted_greedy_coloring;
			else if (strcasecmp(optarg,"degree")==0)
				reorder=reorder_by_degree;
			else if (strcasecmp(optarg,"random")==0)
				reorder=reorder_by_random;
			else {
				fprintf(stderr,"Bad reordering type: %s\n",
					optarg);
				fprintf(stderr,TRYFORHELP);
				exit(1);
			}
			break;
		case '0':
			number1=FALSE;
			break;
		case 'q':
			quiet++;
			break;
		case 'h':
			printhelp(argv[0]);
			break;
#ifndef ENABLE_LONG_OPTIONS
		case '-':
			fprintf(stderr,"%s: Long options not enabled "
				"during compile.\n",argv[0]);
			exit(1);
#endif
		case '?':
			fprintf(stderr,TRYFORHELP);
			exit(1);
		default:
			fprintf(stderr,"Internal bug: line %d should never "
				"be reached!",__LINE__);
			exit(-1);
		}
	}

	if (optind >= argc) {
		fprintf(stderr,"Missing filename.\n");
		fprintf(stderr,TRYFORHELP);
		exit(1);
	}
	if (optind < argc-1) {
		fprintf(stderr,"Bad command line option: %s\n",argv[optind+1]);
		fprintf(stderr,TRYFORHELP);
		exit(1);
	}

	file=argv[optind];
	return;
}


/*
 * Prints a line to stderr indicating what is being searched for.
 */
static void print_search(graph_t *g) {
	fprintf(stderr,"Searching for %s ",find_all?"all":"a single");
	if (min_weight==0) {
		fprintf(stderr,"maximum %s clique%s...\n",
			unweighted?"size":"weight",find_all?"s":"");
		return;
	}
	if (min_weight>0 && maximal)
		fprintf(stderr,"maximal ");
	fprintf(stderr,"clique%s with %s ",find_all?"s":"",
		unweighted?"size":"weight");
	if (max_weight==0) {
		fprintf(stderr,"at least %d...",min_weight);
	} else if (min_weight==max_weight) {
		fprintf(stderr,"exactly %d...",min_weight);
	} else {
		fprintf(stderr,"within range %d-%d...",min_weight,max_weight);
	}
	fprintf(stderr,"\n");
	return;
}

/*
 * Prints the clique s along with it's size and weight.
 */
static void print_clique(set_t s,graph_t *g) {
	int i;

	printf("size=%d, weight=%d:  ",set_size(s),graph_subgraph_weight(g,s));
	for (i=0; i<SET_MAX_SIZE(s); i++) {
		if (SET_CONTAINS(s,i)) {
			if (number1)
				printf(" %d",i+1);
			else
				printf(" %d",i);
		}
	}
	printf("\n");
	return;
}

/*
 * Records a clique into the clique list using dynamic allocation.
 * Used as opts->user_function.
 */
static boolean record_clique_func(set_t s,graph_t *g,clique_options *opts) {
	if (clique_count>=clique_list_size) {
		clique_list=realloc(clique_list,(clique_list_size+512) * 
				    sizeof(set_t));
		clique_list_size+=512;
	}
	clique_list[clique_count]=set_duplicate(s);
	clique_count++;
	return TRUE;
}

/*
 * Prints a clique.  Used as opts->user_function.
 */
static boolean print_clique_func(set_t s,graph_t *g,clique_options *opts) {
	print_clique(s,g);
	return TRUE;
}