2 * Copyright (c) 2018, 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
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.
29 #define zmax(x, y) (((x) > (y)) ? (x) : (y))
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
;
39 } else if (*as
> *bs
) {
47 check_iso(struct quiver
*a
, struct quiver
*b
, const char **fix
, size_t fix_len
)
58 size_t next_highest
= 0;
59 size_t next_highest_idx
= 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
71 if (!(a_i
= calloc(n
, sizeof *a_i
))) {
76 if (!(b_i
= calloc(n
, sizeof *b_i
))) {
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
))) {
89 if (!(strcmp(b
->v
[k
].name
, name
))) {
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 */
101 /* Find smallest a_next value that's not already used */
102 for (a_next
= 0; a_next
< n
; ++a_next
) {
105 for (size_t k
= 0; k
< i_idx
; ++k
) {
106 if (a_i
[k
] == a_next
) {
117 /* The same; for b */
118 for (b_next
= 0; b_next
< n
; ++b_next
) {
121 for (size_t k
= 0; k
< i_idx
; ++k
) {
122 if (b_i
[k
] == b_next
) {
137 * Require that, restricted to the fixed parts, we have a
140 for (size_t j
= 0; j
< fix_len
; ++j
) {
144 if (a
->v
[ja
].fatness
!= b
->v
[jb
].fatness
) {
146 "Fixed vertex \u00ab%s\u00bb has inconsistent fatness %d, %d\n",
147 a
->v
[ja
].name
, a
->v
[ja
].fatness
,
152 for (size_t k
= 0; k
< fix_len
; ++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
||
161 "S: \u00ab%s\u00bb \u2192 \u00ab%s\u00bb, %d/%d\n",
162 a
->v
[ja
].name
, a
->v
[ka
].name
, ea
->p
,
165 "T: \u00ab%s\u00bb \u2192 \u00ab%s\u00bb, %d/%d\n",
166 b
->v
[jb
].name
, b
->v
[kb
].name
, eb
->p
,
169 "These are fixed vertices; so ");
170 fprintf(stderr
, "no isomorphism is possible\n");
178 check_partial_isomorphism
:
181 * At this point, a_i and b_i are valid permutations
182 * (completely, up to the last position). Furthermore, the
183 * map Gₐ[a_i[k] <-> Gᵦ[b_i[k]], restricted to k < i_idx,
184 * gives a graph isomorphism. We seek to check if incrementing
185 * i_idx by one works.
190 if (a
->v
[ia
].fatness
!= b
->v
[ib
].fatness
) {
191 goto increment_i_idxth_place
;
194 for (size_t j
= 0; j
< i_idx
; ++j
) {
197 struct rational
*ea
= &a
->e
[ia
* a
->v_len
+ ja
];
198 struct rational
*eb
= &b
->e
[ib
* b
->v_len
+ jb
];
199 struct rational
*fa
= &a
->e
[ja
* a
->v_len
+ ia
];
200 struct rational
*fb
= &b
->e
[jb
* b
->v_len
+ ib
];
202 if (ea
->p
!= eb
->p
||
206 goto increment_i_idxth_place
;
211 * Expanding the isomorphism works. Good, let's try
212 * to expand a bit more
217 goto check_partial_isomorphism
;
220 /* The whole thing is an isomorphism. Good, we're done. */
221 printf("Isomorphism found\n");
223 printf("------------|------------\n");
225 for (size_t j
= 0; j
< n
; ++j
) {
229 printf("%11s | %s\n", a
->v
[ja
].name
, b
->v
[jb
].name
);
234 increment_i_idxth_place
:
237 * Try to get the next in the orderings of b_i.
238 * It's a permutation, so we're in the easy case
239 * of the incrementation algorithm.
241 next_highest
= (size_t) -1;
242 next_highest_idx
= i_idx
;
244 for (size_t j
= i_idx
+ 1; j
< n
; ++j
) {
245 if (b_i
[j
] < next_highest
&&
246 b_i
[j
] > b_i
[i_idx
]) {
247 next_highest
= b_i
[j
];
248 next_highest_idx
= j
;
252 if (next_highest
== (size_t) -1) {
253 if (i_idx
<= fix_len
) {
254 /* We've checked all we can */
255 printf("No isomorphism possible\n");
260 goto increment_i_idxth_place
;
264 b_i
[i_idx
] = b_i
[next_highest_idx
];
265 b_i
[next_highest_idx
] = t
;
266 qsort(b_i
+ i_idx
+ 1, n
- i_idx
- 1, sizeof *b_i
, cmp
);
279 printf("Usage: %s -s <file> -t <file> [ -f <v> [ -f <v> [ ... ] ] ]\n",
281 printf(" <file>: a quiver file output by clav\n");
282 printf(" <v>: a name of a vertex in both files\n");
284 printf("Find (and possibly output) some graph isomorphism between s\n");
285 printf("and t, fixing every vertex named by ‘-f’.\n");
287 printf("Returns 0 if such an isomorphism was found, < 0 if not.\n");
292 main(int argc
, char **argv
)
296 const char *sarg
= 0;
297 const char *targ
= 0;
298 struct quiver qs
= { 0 };
299 struct quiver qt
= { 0 };
302 const char **fixnames
= 0;
303 const char *errstr
= 0;
306 if (!(fixnames
= calloc(1 + (argc
/ 2), sizeof *fixnames
))) {
311 while ((opt
= getopt(argc
, argv
, "hs:t:f:")) != -1) {
320 fixnames
[fidx
] = optarg
;
334 fprintf(stderr
, "\u2018-s\u2019 is required\n");
340 fprintf(stderr
, "\u2018-t\u2019 is required\n");
345 if (!(fs
= fopen(sarg
, "r"))) {
350 if ((ret
= quiver_load_from_file(&qs
, fs
, &errstr
))) {
351 fprintf(stderr
, "cannot load \u00ab%s\u00bb: %s\n", sarg
,
359 if (!(ft
= fopen(targ
, "r"))) {
364 if ((ret
= quiver_load_from_file(&qt
, ft
, &errstr
))) {
365 fprintf(stderr
, "cannot load \u00ab%s\u00bb: %s\n", sarg
,
373 if (qs
.v_num
!= qt
.v_num
) {
375 "\u00ab%s\u00bb and \u00ab%s\u00bb have different |V|\n",
381 /* Make sure all the fixed vertices are present exactly once */
382 for (size_t j
= 0; j
< fidx
; ++j
) {
383 const char *f
= fixnames
[j
];
387 for (size_t k
= 0; k
< qs
.v_num
; ++k
) {
388 seen_in_s
+= !strcmp(qs
.v
[k
].name
, f
);
389 seen_in_t
+= !strcmp(qt
.v
[k
].name
, f
);
392 if (seen_in_s
!= 1 ||
394 fprintf(stderr
, "\u00ab%s\u00bb appears\n", f
);
395 fprintf(stderr
, " %d time%s in \u00ab%s\u00bb\n",
396 seen_in_s
, (seen_in_s
== 1) ? " " : "s", sarg
);
397 fprintf(stderr
, " %d time%s in \u00ab%s\u00bb\n",
398 seen_in_t
, (seen_in_t
== 1) ? " " : "s", targ
);
403 /* Make sure all vertices in all files are distinct */
404 for (size_t j
= 0; j
< qs
.v_num
; ++j
) {
405 for (size_t k
= 0; k
< j
; ++k
) {
406 if (!(strcmp(qs
.v
[j
].name
, qs
.v
[k
].name
))) {
408 "\u00ab%s\u00bb is not distinct in \u00ab%s\u00bb\n",
412 if (!(strcmp(qt
.v
[j
].name
, qt
.v
[k
].name
))) {
414 "\u00ab%s\u00bb is not distinct in \u00ab%s\u00bb\n",
421 * No immediate reason why there shouldn't be an isomorphism.
424 ret
= check_iso(&qs
, &qt
, fixnames
, fidx
);