The Černý conjecture on synchronizing automata
Show that every synchronizing automaton with n states has a reset word of length at most (n − 1)².

| 1 | """The Černý automaton C_n and its shortest reset word, of length (n - 1)^2. States 0..n-1; the input a turns every |
| 2 | state q to q + 1 (mod n), the input b sends state 0 to 1 and keeps the others. Breadth-first search over subsets of |
| 3 | states gives the shortest reset word; it is checked here to have length (n - 1)^2 for n = 2 to 9. |
| 4 | |
| 5 | The figure follows C_5 along that word, b aaaa b aaaa b aaaa b, from each of its 5 possible starting states: one strand |
| 6 | per start, one bead per input. It is drawn in the turning frame (a strand's row is its state minus the number of a's so |
| 7 | far, mod 5), so a turn leaves the strands level and each b merges one strand into the next: 16 inputs to leave one.""" |
| 8 | from collections import deque |
| 9 | from pathlib import Path |
| 10 | |
| 11 | import kit |
| 12 | |
| 13 | |
| 14 | def step(n, q, c): |
| 15 | return (q + 1) % n if c == 'a' else (1 if q == 0 else q) |
| 16 | |
| 17 | |
| 18 | def shortest_reset_word(n): |
| 19 | start = frozenset(range(n)) |
| 20 | seen, todo = {start: ''}, deque([start]) |
| 21 | while todo: |
| 22 | s = todo.popleft() |
| 23 | if len(s) == 1: |
| 24 | return seen[s] |
| 25 | for c in 'ab': |
| 26 | t = frozenset(step(n, q, c) for q in s) |
| 27 | if t not in seen: |
| 28 | seen[t] = seen[s] + c |
| 29 | todo.append(t) |
| 30 | |
| 31 | |
| 32 | for n in range(2, 10): |
| 33 | assert len(shortest_reset_word(n)) == (n - 1) ** 2, n |
| 34 | |
| 35 | n = 5 |
| 36 | word = shortest_reset_word(n) |
| 37 | assert len(word) == (n - 1) ** 2 == 16 |
| 38 | # each start's state after t inputs, and its row in the turning frame |
| 39 | states = [[q] for q in range(n)] |
| 40 | for c in word: |
| 41 | for s in states: |
| 42 | s.append(step(n, s[-1], c)) |
| 43 | turns = [word[:t].count('a') for t in range(len(word) + 1)] |
| 44 | rows = [[(s[t] - turns[t]) % n for t in range(len(word) + 1)] for s in states] |
| 45 | assert len({s[-1] for s in states}) == 1 |
| 46 | |
| 47 | x0, dx, y0, dy = 44, 34.5, 22, 54 |
| 48 | |
| 49 | |
| 50 | def at(t, r): |
| 51 | return x0 + t * dx, y0 + r * dy |
| 52 | |
| 53 | |
| 54 | segs, beads = set(), set() |
| 55 | for r in rows: |
| 56 | for t in range(len(word)): |
| 57 | segs.add((t, r[t], r[t + 1])) |
| 58 | beads |= {(t, r[t]) for t in range(len(word) + 1)} |
| 59 | body = [kit.line(*at(t, a), *at(t + 1, b), 'ink2-s' if a == b else 'f-s', 3 if a == b else 4) for t, a, b in sorted(segs)] |
| 60 | body += [kit.dot(*at(t, r), 7 if t == 0 else 5) for t, r in sorted(beads) if t < len(word)] |
| 61 | body.append(kit.dot(*at(len(word), rows[0][-1]), 10, 'f')) |
| 62 | kit.write(__file__, 'mathematics', ''.join(body), label='n = 5 states · shortest reset (n−1)² = 16 inputs') |