summaryrefslogtreecommitdiff
path: root/main.c
blob: 4eb0f5d2f22d332124229e5d44600c68e11bbc8b (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
/*
	Projet d'algorithmique et programmation 2013-2014
	(cours de C.Matthieu et J.Stern)
	Alex AUVOLAT, Mendes OULAMARA

	Sujet : Algorithme de Bron-Kerbosch pour Maximum-Clique
	(cf maxclique.pdf)
*/

#include <stdlib.h>

#include "sets.h"
#include "graph.h"
#include "algos.h"

/*
	max_clique calculates the maximum clique in a graph. Arguments :
	- g : the graph where the clique is looked for
	- k : the clique we are currently examining
	- c : the graph nodes we can potentially add to the clique
	- a : the nodes we can actually add to the clique
	- mc : a pointer to the set containing the maximum clique found until now
	Returns nothing (result is in *mc).
*/


// Driver

void usage(char *pname) {
	printf("\nUsage:\n\t%s [options] [<graph file>]\n\n", pname);
	printf("Available options:\n");
	printf("\n    -d\n\tRead input in DIMACS format\n");
	printf("\n    -b\n\tUse algorithm B\n");
	printf("\n    -c\n\tUse algorithm C\n");
	printf("\n    -o <file.dot>\n\tDump graph in graphwiz .dot format\n");
	printf("\n    -h, --help\n\tShow this help page\n");
	exit(1);
}

int main(int argc, char **argv) {
	int i;
	int dimacs = 0;
	char *filename = "-";
	char *dump = NULL;
	int algo = 0;

	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "-d")) {
			dimacs = 1;
		} else if (!strcmp(argv[i], "-o")) {
			if (++i == argc) usage(argv[0]);
			dump = argv[i];
		} else if (!strcmp(argv[i], "-b")) {
			algo = 1;
		} else if (!strcmp(argv[i], "-c")) {
			algo = 2;
		} else if (argv[i][0] == '-') {
			usage(argv[0]);
		} else {
			filename = argv[i];
		}
	}

	FILE *f = stdin;
	if (strcmp(filename, "-")) {
		f = fopen(filename, "r");
		if (f == NULL) {
			fprintf(stderr, "Error: could not open file %s\n", filename);
			return 1;
		}
	}
	graph g = (dimacs ? load_graph_dimacs(f) : load_graph(f));
	if (g == NULL) {
		fprintf(stderr, "Error loading file %s\n", filename);
		return 1;
	}
	fclose(f);

	if (dump != NULL) {
		f = fopen(dump, "w");
		if (f == NULL) {
			fprintf(stderr, "Error: could not open file %s for writing\n", dump);
			return 1;
		}
		dump_graphviz(g, f);
		fclose(f);
	}

	// do stuff with graph
	if (algo == 0) {
		set max_clique = empty_set(g->N);
		set init_s = full_set(g->N);
		set init_k = empty_set(g->N);
		max_clique_a(g, init_k, init_s, &max_clique);
		printf("Max clique: "); dump_set(max_clique);
	} else if (algo == 1) {
		set max_clique = empty_set(g->N);
		set init_c = full_set(g->N);
		set init_a = full_set(g->N);
		set init_k = empty_set(g->N);
		max_clique_b(g, init_k, init_c, init_a, &max_clique);
		printf("Max clique: "); dump_set(max_clique);
	} else if (algo == 2) {
		set max_clique = empty_set(g->N);
		set init_c = full_set(g->N);
		set init_a = full_set(g->N);
		set init_k = empty_set(g->N);
		max_clique_c(g, init_k, init_c, init_a, &max_clique);
		printf("Max clique: "); dump_set(max_clique);
	}

	delete_graph(g);
	return 0;
}