File: bliss.cc

package info (click to toggle)
bliss 0.73-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 712 kB
  • sloc: cpp: 6,538; makefile: 144; sh: 6
file content (334 lines) | stat: -rw-r--r-- 9,277 bytes parent folder | download | duplicates (3)
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
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <bliss/defs.hh>
#include <bliss/graph.hh>
#include <bliss/timer.hh>
#include <bliss/utils.hh>

/*
  Copyright (c) 2003-2015 Tommi Junttila
  Released under the GNU Lesser General Public License version 3.
  
  This file is part of bliss.
  
  bliss is free software: you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation, version 3 of the License.

  bliss is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with bliss.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * \page executable bliss executable
 * \include bliss.cc
 */

/* Input file name */
static const char* infilename = 0;

static bool opt_directed = false;
static bool opt_canonize = false;
static const char* opt_output_can_file = 0;
static const char* opt_splitting_heuristics = "fsm";
static bool opt_use_failure_recording = true;
static bool opt_use_component_recursion = true;


/* Verbosity level and target stream */
static unsigned int verbose_level = 1;
static FILE* verbstr = stdout;



#if !defined(BLISS_COMPILED_DATE)
#define BLISS_COMPILED_DATE "compiled " __DATE__
#endif

static void
version(FILE* const fp)
{
  fprintf(fp,
"bliss version %s (" BLISS_COMPILED_DATE ")\n"
"Copyright (C) 2003-2015 Tommi Junttila.\n"
"\n"
"License LGPLv3+: GNU LGPL version 3 or later, <http://gnu.org/licenses/lgpl.html>.\n"
"This program comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
"and you are welcome to redistribute it under certain conditions;\n"
"see COPYING and COPYING.LESSER for details.\n"
      , bliss::version
	  );
}

static void
usage(FILE* const fp, const char* argv0)
{
  const char* program_name;
  
  program_name = rindex(argv0, '/');
  
  if(program_name) program_name++;
  else program_name = argv0;  
  if(!program_name or *program_name == 0) program_name = "bliss";

  fprintf(fp,
"Usage: %s [options] [<graphfile>]\n"
"  Run bliss on <graphfile>.\n"
"Options:\n"
"  -directed   the input graph is directed\n"
"  -can        compute canonical form\n"
"  -ocan=f     compute canonical form and output it in file f\n"
"  -v=N        set verbose level to N [N >= 0, default: 1]\n"
"  -sh=X       select splitting heuristics, where X is\n"
"                f    first non-singleton cell\n"
"                fl   first largest non-singleton cell\n"
"                fs   first smallest non-singleton cell\n"
"                fm   first maximally non-trivially connected\n"
"                     non-singleton cell\n"
"                flm  first largest maximally non-trivially connected\n"
"                     non-singleton cell\n"
"                fsm  first smallest maximally non-trivially connected\n"
"                     non-singleton cell [default]\n"
"  -fr=X       use failure recording? [X=y/n, default: y]\n"
"  -cr=X       use component recursion? [X=y/n, default: y]\n"
"  -version    print the version number and exit\n"
"  -help       print this help and exit\n"
"\n"
          ,program_name
	  );
}



static void
parse_options(const int argc, const char** argv)
{
  unsigned int tmp;
  for(int i = 1; i < argc; i++)
    {
      if(strcmp(argv[i], "-can") == 0)
	opt_canonize = true;
      else if((strncmp(argv[i], "-ocan=", 6) == 0) and (strlen(argv[i]) > 6))
	{
	  opt_canonize = true;
	  opt_output_can_file = argv[i]+6;
	}
      else if(sscanf(argv[i], "-v=%u", &tmp) == 1)
	verbose_level = tmp;
      else if(strcmp(argv[i], "-directed") == 0)
	opt_directed = true;
      else if(strcmp(argv[i], "-fr=n") == 0)
	opt_use_failure_recording = false;
      else if(strcmp(argv[i], "-fr=y") == 0)
	opt_use_failure_recording = true;
      else if(strcmp(argv[i], "-cr=n") == 0)
	opt_use_component_recursion = false;
      else if(strcmp(argv[i], "-cr=y") == 0)
	opt_use_component_recursion = true;
      else if((strncmp(argv[i], "-sh=", 4) == 0) and (strlen(argv[i]) > 4))
	{
	  opt_splitting_heuristics = argv[i]+4;
	}
      else if(strcmp(argv[i], "-version") == 0)
	{
	  version(stdout);
	  exit(0);
	}
      else if(strcmp(argv[i], "-help") == 0)
	{
	  usage(stdout, argv[0]);
	  exit(0);
	}
      else if(argv[i][0] == '-')
	{
	  fprintf(stderr, "Unknown command line argument `%s'\n", argv[i]);
	  usage(stderr, argv[0]);
	  exit(1);
	}
      else
	{
	  if(infilename)
	    {
	      fprintf(stderr, "Too many file arguments\n");
	      usage(stderr, argv[0]);
	      exit(1);
	    }
	  else
	    {
	      infilename = argv[i];
	    }
	}
    }
}



/**
 * The hook function that prints the found automorphisms.
 * \a param must be a file descriptor (FILE *).
 */
static void
report_aut(void* param, const unsigned int n, const unsigned int* aut)
{
  assert(param);
  fprintf((FILE*)param, "Generator: ");
  bliss::print_permutation((FILE*)param, n, aut, 1);
  fprintf((FILE*)param, "\n");
}



/* Output an error message and exit the whole program */
static void
_fatal(const char* fmt, ...)
{
  va_list ap;
  va_start(ap, fmt);
  vfprintf(stderr, fmt, ap); fprintf(stderr, "\n");
  va_end(ap);
  exit(1);
}



int
main(const int argc, const char** argv)
{
  bliss::Timer timer;
  bliss::AbstractGraph* g = 0;

  parse_options(argc, argv);
  
  /* Parse splitting heuristics */
  bliss::Digraph::SplittingHeuristic shs_directed = bliss::Digraph::shs_fsm;
  bliss::Graph::SplittingHeuristic shs_undirected = bliss::Graph::shs_fsm;
  if(opt_directed)
    {
      if(strcmp(opt_splitting_heuristics, "f") == 0)
	shs_directed = bliss::Digraph::shs_f;
      else if(strcmp(opt_splitting_heuristics, "fs") == 0)
	shs_directed = bliss::Digraph::shs_fs;
      else if(strcmp(opt_splitting_heuristics, "fl") == 0)
	shs_directed = bliss::Digraph::shs_fl;
      else if(strcmp(opt_splitting_heuristics, "fm") == 0)
	shs_directed = bliss::Digraph::shs_fm;
      else if(strcmp(opt_splitting_heuristics, "fsm") == 0)
	shs_directed = bliss::Digraph::shs_fsm;
      else if(strcmp(opt_splitting_heuristics, "flm") == 0)
	shs_directed = bliss::Digraph::shs_flm;
      else
	_fatal("Illegal option -sh=%s, aborting", opt_splitting_heuristics);
    }
  else
    {
      if(strcmp(opt_splitting_heuristics, "f") == 0)
	shs_undirected = bliss::Graph::shs_f;
      else if(strcmp(opt_splitting_heuristics, "fs") == 0)
	shs_undirected = bliss::Graph::shs_fs;
      else if(strcmp(opt_splitting_heuristics, "fl") == 0)
	shs_undirected = bliss::Graph::shs_fl;
      else if(strcmp(opt_splitting_heuristics, "fm") == 0)
	shs_undirected = bliss::Graph::shs_fm;
      else if(strcmp(opt_splitting_heuristics, "fsm") == 0)
	shs_undirected = bliss::Graph::shs_fsm;
      else if(strcmp(opt_splitting_heuristics, "flm") == 0)
	shs_undirected = bliss::Graph::shs_flm;
      else
	_fatal("Illegal option -sh=%s, aborting", opt_splitting_heuristics);
    }

  /* Open the input file */
  FILE* infile = stdin;
  if(infilename)
    {
      infile = fopen(infilename, "r");
      if(!infile)
	_fatal("Cannot not open `%s' for input, aborting", infilename);
    }

  /* Read the graph from the file */
  if(opt_directed)
    {
      /* Read directed graph in the DIMACS format */
      g = bliss::Digraph::read_dimacs(infile);
    }
  else
    {
      /* Read undirected graph in the DIMACS format */
      g = bliss::Graph::read_dimacs(infile);
    }
  
  if(infile != stdin)
    fclose(infile);

  if(!g)
    _fatal("Failed to read the graph, aborting");
  
  if(verbose_level >= 2)
    {
      fprintf(verbstr, "Graph read in %.2f seconds\n", timer.get_duration());
      fflush(verbstr);
    }


  bliss::Stats stats;

  /* Set splitting heuristics and verbose level */
  if(opt_directed)
    ((bliss::Digraph*)g)->set_splitting_heuristic(shs_directed);
  else
    ((bliss::Graph*)g)->set_splitting_heuristic(shs_undirected);
  g->set_verbose_level(verbose_level);
  g->set_verbose_file(verbstr);
  g->set_failure_recording(opt_use_failure_recording);
  g->set_component_recursion(opt_use_component_recursion);


  if(opt_canonize == false)
    {
      /* No canonical labeling, only automorphism group */
      g->find_automorphisms(stats, &report_aut, stdout);
    }
  else
    {
      /* Canonical labeling and automorphism group */
      const unsigned int* cl = g->canonical_form(stats, &report_aut, stdout);

      fprintf(stdout, "Canonical labeling: ");
      bliss::print_permutation(stdout, g->get_nof_vertices(), cl, 1);
      fprintf(stdout, "\n");

      if(opt_output_can_file)
	{
	  bliss::AbstractGraph* cf = g->permute(cl);
	  FILE* const fp = fopen(opt_output_can_file, "w");
	  if(!fp)
	    _fatal("Cannot open '%s' for outputting the canonical form, aborting", opt_output_can_file);
	  cf->write_dimacs(fp);
	  fclose(fp);
	  delete cf;
	}
    }

  /* Output search statistics */
  if(verbose_level > 0 and verbstr)
    stats.print(verbstr);

  if(verbose_level > 0)
    {
      fprintf(verbstr, "Total time:\t%.2f seconds\n", timer.get_duration());
      fflush(verbstr);
    }


  delete g; g = 0;

  return 0;
}