Fix false positive in bfzIII word detection
[clav.git] / check-graph-iso.c
blob032046f51181403c0380e1a9d3e7ae990df55e30
1 /*
2 * Copyright (c) 2016-2019, S. Gilles <sgilles@math.umd.edu>
4 * Permission to use, copy, modify, and/or distribute this software
5 * for any purpose with or without fee is hereby granted, provided
6 * that the above copyright notice and this permission notice appear
7 * in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
13 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
26 #include "macros.h"
27 #include "quiver.h"
29 #define zmax(x, y) (((x) > (y)) ? (x) : (y))
31 static int
32 cmp(const void *a, const void *b)
34 const size_t *as = (const size_t *) a;
35 const size_t *bs = (const size_t *) b;
37 if (*as < *bs) {
38 return -1;
39 } else if (*as > *bs) {
40 return 1;
43 return 0;
46 int
47 check_iso(struct quiver *a, struct quiver *b, const char **fix, size_t fix_len)
49 int ret = -1;
50 size_t n = a->v_num;
51 size_t *a_i = 0;
52 size_t *b_i = 0;
53 size_t i_idx = 0;
54 size_t ia = 0;
55 size_t ib = 0;
56 size_t a_next = 0;
57 size_t b_next = 0;
58 size_t next_highest = 0;
59 size_t next_highest_idx = 0;
60 size_t t = 0;
63 a_i is { f₁, f₂, …, fₘ, u₁, u₂, …, uₙ₋ₘ }, where f's are
64 fixed indices and u₁ < u₂ < … < uₙ₋ₘ .
66 b_i will be the same, but as the sequence progresses the
67 u's will be permuted, and we require always that a and
68 b, when restricted to indices less than i_idx, are
69 isomorphic.
71 if (!(a_i = calloc(n, sizeof *a_i))) {
72 perror(L("calloc"));
73 goto done;
76 if (!(b_i = calloc(n, sizeof *b_i))) {
77 perror(L("calloc"));
78 goto done;
81 for (size_t j = 0; j < fix_len; ++j) {
82 const char *name = fix[j];
84 for (size_t k = 0; k < n; ++k) {
85 if (!(strcmp(a->v[k].name, name))) {
86 a_i[j] = k;
89 if (!(strcmp(b->v[k].name, name))) {
90 b_i[j] = k;
95 /* Fill out the permutation to make u's increasing */
96 for (i_idx = fix_len; i_idx < n; ++i_idx) {
97 /* This is incredibly inefficient, but it's readable */
98 int seen_a_next = 0;
99 int seen_b_next = 0;
101 /* Find smallest a_next value that's not already used */
102 for (a_next = 0; a_next < n; ++a_next) {
103 seen_a_next = 0;
105 for (size_t k = 0; k < i_idx; ++k) {
106 if (a_i[k] == a_next) {
107 seen_a_next = 1;
111 if (!seen_a_next) {
112 a_i[i_idx] = a_next;
113 break;
117 /* The same; for b */
118 for (b_next = 0; b_next < n; ++b_next) {
119 seen_b_next = 0;
121 for (size_t k = 0; k < i_idx; ++k) {
122 if (b_i[k] == b_next) {
123 seen_b_next = 1;
127 if (!seen_b_next) {
128 b_i[i_idx] = b_next;
129 break;
134 i_idx = fix_len;
137 * Require that, restricted to the fixed parts, we have a
138 * graph isomorphism.
140 for (size_t j = 0; j < fix_len; ++j) {
141 size_t ja = a_i[j];
142 size_t jb = b_i[j];
144 if (a->v[ja].fatness != b->v[jb].fatness) {
145 fprintf(stderr,
146 "Fixed vertex \u00ab%s\u00bb has inconsistent fatness %d, %d\n",
147 a->v[ja].name, a->v[ja].fatness,
148 b->v[ja].fatness);
149 goto done;
152 for (size_t k = 0; k < fix_len; ++k) {
153 size_t ka = a_i[k];
154 size_t kb = b_i[k];
155 struct rational *ea = &a->e[ja * a->v_len + ka];
156 struct rational *eb = &b->e[jb * b->v_len + kb];
158 if (ea->p != eb->p ||
159 ea->q != eb->q) {
160 fprintf(stderr,
161 "S: \u00ab%s\u00bb \u2192 \u00ab%s\u00bb, %ld/%ld\n",
162 a->v[ja].name, a->v[ka].name, (long
163 int) ea->
165 (long int) ea->q);
166 fprintf(stderr,
167 "T: \u00ab%s\u00bb \u2192 \u00ab%s\u00bb, %ld/%ld\n",
168 b->v[jb].name, b->v[kb].name, (long
169 int) eb->
171 (long int) eb->q);
172 fprintf(stderr,
173 "These are fixed vertices; so ");
174 fprintf(stderr, "no isomorphism is possible\n");
175 goto done;
180 /* The long haul */
181 while (1) {
182 check_partial_isomorphism:
185 * At this point, a_i and b_i are valid permutations
186 * (completely, up to the last position). Furthermore, the
187 * map Gₐ[a_i[k] <-> Gᵦ[b_i[k]], restricted to k < i_idx,
188 * gives a graph isomorphism. We seek to check if incrementing
189 * i_idx by one works.
191 ia = a_i[i_idx];
192 ib = b_i[i_idx];
194 if (a->v[ia].fatness != b->v[ib].fatness) {
195 goto increment_i_idxth_place;
198 for (size_t j = 0; j < i_idx; ++j) {
199 size_t ja = a_i[j];
200 size_t jb = b_i[j];
201 struct rational *ea = &a->e[ia * a->v_len + ja];
202 struct rational *eb = &b->e[ib * b->v_len + jb];
203 struct rational *fa = &a->e[ja * a->v_len + ia];
204 struct rational *fb = &b->e[jb * b->v_len + ib];
206 if (ea->p != eb->p ||
207 ea->q != eb->q ||
208 fa->p != fb->p ||
209 fa->q != fb->q) {
210 goto increment_i_idxth_place;
215 * Expanding the isomorphism works. Good, let's try
216 * to expand a bit more
218 i_idx++;
220 if (i_idx < n) {
221 goto check_partial_isomorphism;
224 /* The whole thing is an isomorphism. Good, we're done. */
225 printf("Isomorphism found\n");
226 printf(" S | T \n");
227 printf("------------|------------\n");
229 for (size_t j = 0; j < n; ++j) {
230 size_t ja = a_i[j];
231 size_t jb = b_i[j];
233 printf("%11s | %s\n", a->v[ja].name, b->v[jb].name);
236 ret = 0;
237 goto done;
238 increment_i_idxth_place:
241 * Try to get the next in the orderings of b_i.
242 * It's a permutation, so we're in the easy case
243 * of the incrementation algorithm.
245 next_highest = (size_t) -1;
246 next_highest_idx = i_idx;
248 for (size_t j = i_idx + 1; j < n; ++j) {
249 if (b_i[j] < next_highest &&
250 b_i[j] > b_i[i_idx]) {
251 next_highest = b_i[j];
252 next_highest_idx = j;
256 if (next_highest == (size_t) -1) {
257 if (i_idx <= fix_len) {
258 /* We've checked all we can */
259 printf("No isomorphism possible\n");
260 goto done;
263 i_idx--;
264 goto increment_i_idxth_place;
267 t = b_i[i_idx];
268 b_i[i_idx] = b_i[next_highest_idx];
269 b_i[next_highest_idx] = t;
270 qsort(b_i + i_idx + 1, n - i_idx - 1, sizeof *b_i, cmp);
273 done:
274 free(a_i);
275 free(b_i);
277 return ret;
280 static void
281 usage(char *argv0)
283 printf("Usage: %s -s <file> -t <file> [ -f <v> [ -f <v> [ ... ] ] ]\n",
284 argv0);
285 printf(" <file>: a quiver file output by clav\n");
286 printf(" <v>: a name of a vertex in both files\n");
287 printf("\n");
288 printf("Find (and possibly output) some graph isomorphism between s\n");
289 printf("and t, fixing every vertex named by ‘-f’.\n");
290 printf("\n");
291 printf("Returns 0 if such an isomorphism was found, < 0 if not.\n");
294 /* Run the thing */
296 main(int argc, char **argv)
298 int ret = -1;
299 int opt = 0;
300 const char *sarg = 0;
301 const char *targ = 0;
302 struct quiver qs = { 0 };
303 struct quiver qt = { 0 };
304 FILE *fs = 0;
305 FILE *ft = 0;
306 const char **fixnames = 0;
307 const char *errstr = 0;
308 size_t fidx = 0;
310 if (!(fixnames = calloc(1 + (argc / 2), sizeof *fixnames))) {
311 perror(L("calloc"));
312 goto done;
315 while ((opt = getopt(argc, argv, "hs:t:f:")) != -1) {
316 switch (opt) {
317 case 's':
318 sarg = optarg;
319 break;
320 case 't':
321 targ = optarg;
322 break;
323 case 'f':
324 fixnames[fidx] = optarg;
325 fidx++;
326 break;
327 case 'h':
328 ret = 0;
330 /* fall through */
331 default:
332 usage(argv[0]);
333 goto done;
337 if (!sarg) {
338 fprintf(stderr, "\u2018-s\u2019 is required\n");
339 usage(argv[0]);
340 goto done;
343 if (!targ) {
344 fprintf(stderr, "\u2018-t\u2019 is required\n");
345 usage(argv[0]);
346 goto done;
349 if (!(fs = fopen(sarg, "r"))) {
350 perror(L("fopen"));
351 goto done;
354 if ((ret = quiver_load_from_file(&qs, fs, &errstr))) {
355 fprintf(stderr, "cannot load \u00ab%s\u00bb: %s\n", sarg,
356 errstr);
357 goto done;
360 fclose(fs);
361 fs = 0;
363 if (!(ft = fopen(targ, "r"))) {
364 perror(L("fopen"));
365 goto done;
368 if ((ret = quiver_load_from_file(&qt, ft, &errstr))) {
369 fprintf(stderr, "cannot load \u00ab%s\u00bb: %s\n", sarg,
370 errstr);
371 goto done;
374 fclose(ft);
375 ft = 0;
377 if (qs.v_num != qt.v_num) {
378 fprintf(stderr,
379 "\u00ab%s\u00bb and \u00ab%s\u00bb have different |V|\n",
380 sarg,
381 targ);
382 goto done;
385 /* Make sure all the fixed vertices are present exactly once */
386 for (size_t j = 0; j < fidx; ++j) {
387 const char *f = fixnames[j];
388 int seen_in_s = 0;
389 int seen_in_t = 0;
391 for (size_t k = 0; k < qs.v_num; ++k) {
392 seen_in_s += !strcmp(qs.v[k].name, f);
393 seen_in_t += !strcmp(qt.v[k].name, f);
396 if (seen_in_s != 1 ||
397 seen_in_t != 1) {
398 fprintf(stderr, "\u00ab%s\u00bb appears\n", f);
399 fprintf(stderr, " %d time%s in \u00ab%s\u00bb\n",
400 seen_in_s, (seen_in_s == 1) ? " " : "s", sarg);
401 fprintf(stderr, " %d time%s in \u00ab%s\u00bb\n",
402 seen_in_t, (seen_in_t == 1) ? " " : "s", targ);
403 goto done;
407 /* Make sure all vertices in all files are distinct */
408 for (size_t j = 0; j < qs.v_num; ++j) {
409 for (size_t k = 0; k < j; ++k) {
410 if (!(strcmp(qs.v[j].name, qs.v[k].name))) {
411 fprintf(stderr,
412 "\u00ab%s\u00bb is not distinct in \u00ab%s\u00bb\n",
413 qs.v[j].name, sarg);
416 if (!(strcmp(qt.v[j].name, qt.v[k].name))) {
417 fprintf(stderr,
418 "\u00ab%s\u00bb is not distinct in \u00ab%s\u00bb\n",
419 qs.v[j].name, sarg);
425 * No immediate reason why there shouldn't be an isomorphism.
426 * Let's try it.
428 ret = check_iso(&qs, &qt, fixnames, fidx);
429 done:
430 quiver_clean(&qs);
431 quiver_clean(&qt);
433 if (fs) {
434 fclose(fs);
437 if (ft) {
438 fclose(ft);
441 free(fixnames);
443 return ret;