Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions research/chain-necklace-generator/PROOF.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# Direct generation of valid cyclic chain schedules

This contribution was produced by Nick Petalas. Apache-2.0.

## Contract and contribution

Use SHIN09-git's [PR6080](https://github.com/CrocSwap/integer-mult-bounds/pull/6080)
depth-three contract. A word has `m` copies of each of the letters `1,2,3`.
Write `A,B,T` for its scattered subsequence counts `12,23,123`. It is valid
for target `c` when `A=B=0` and `T=c` modulo `m`. In the four-role dirty chain,
these are the complete endpoint conditions, with arbitrary compatible maps
and arbitrary initial data. This endpoint characterization and its rotation
invariance are cited predecessor results. We do not audit or extend them.

We construct exactly the lexicographically smallest word in each valid
rotation class. The new step is a suffix-residue completion test joined to
classical fixed-content necklace generation. The written construction works
for every positive integer `m`. The supplied bounded implementation accepts
only `1<=m<=5`, and `0<=c<m`. At `m=1` this is the predecessor's auxiliary
combinatorial convention, not a ring of characteristic one.

The source's 356 classes at `(m,c)=(5,1)` are credited data. The contribution
is their direct generation, the safe pruning proof, and the complete work
comparison, rather than a new class count. No constant amortized time bound
or improvement of a multiplication exponent is claimed.

## Exact suffix table

For each count vector `r=(r1,r2,r3)` with `0<=ri<=m`, let `D[r]` contain the
triples `(A,B,T)` modulo `m` attained by words with that exact count vector.
Initialize `D[0,0,0]={(0,0,0)}`. Process count vectors in lexicographic order.
For each positive coordinate, append its letter to every state in the cell
with that coordinate decremented. On a predecessor count vector `(a,b,d)`,
the append rules are

```text
append 1: (A,B,T) -> (A,B,T),
append 2: (A,B,T) -> (A+a,B,T),
append 3: (A,B,T) -> (A,B+b,T+A).
```

Reduce each coordinate modulo `m` and take a set union. Every nonempty word
has exactly one last letter. Induction on length therefore proves equality
with the defined `D[r]`, including both existence and exhaustiveness. Every
predecessor cell precedes its successor in the chosen order.

There are `(m+1)^3` cells and at most `m^3` states per cell. There are at most
`3*m^3*(m+1)^3` state transitions. Thus preprocessing needs `O(m^6)` residue
operations and state storage. This is an arithmetic/state bound, not a
worst-case claim about the runtime of a language's hash table.

## Necessary and sufficient completion test

Let a prefix have counts `(a,b,d)` and residues `(A,B,T)`. Its suffix must
have counts `r=(m-a,m-b,m-d)`. Let `(u,v,w)` be the suffix's residues. Split
each counted subsequence at the concatenation boundary. Exactly

```text
A_total = A + u + a*r2,
B_total = B + v + b*r3,
T_total = T + w + A*r3 + a*v.
```

The triple count has four possibilities: all in the prefix, all in the
suffix, its first two letters in the prefix, or only its first letter in
the prefix. No commuting-map assumption enters these integer identities.

Consequently define, modulo `m`,

```text
u0 = -A-a*r2,
v0 = -B-b*r3,
w0 = c-T-A*r3-a*v0.
```

A balanced valid completion exists if and only if `(u0,v0,w0)` belongs to
`D[r]`. Necessity follows by substitution. For sufficiency, the exact table
supplies a suffix word with these residues, and the three concatenation
identities prove validity. This is a completion test among unrestricted
suffix words. It need not certify that a completion is a canonical necklace.
Its use as a rejection test is nevertheless exact and safe.

## Canonical traversal and safe pruning

We use the classical prenecklace extension theorem, not a new necklace
algorithm. A prenecklace is a prefix of a lexicographically least rotation.
Track the length `p` of its longest Lyndon prefix. At the next one-based
position `t`, the permissible symbols are at least `word[t-p]`. Equality
retains `p`; a larger symbol replaces `p` by `t`. At length `n`, such a
prenecklace is a necklace exactly when `p` divides `n`. See Sawada,
[A fast algorithm to generate necklaces with fixed content](https://www.socs.uoguelph.ca/~sawada/papers/alph.pdf),
Theorem 1 and Figure 1, following Fredricksen, Kessler, Maiorana and Ruskey's
necklace-generation lineage. We use the simple extension rule, not Sawada's
constant-amortized-time refinement.

Start with letter `1`, since every balanced necklace contains that least
letter and begins with it. Extend only when the letter count remains at
most `m`. Update the prefix residues and apply the suffix test above.
Reject a prefix only when the test fails. At length `3m`, emit only if
`p` divides `3m`. Use a depth-first explicit stack, exploring the smaller
symbol first.

Every emitted word has the required counts and residues, by the completion
test with an empty suffix. The cited extension theorem makes it canonical.
Conversely, take any valid necklace. Every prefix obeys the extension rule
and count bound. Its actual remaining suffix witnesses table membership,
so no prefix is pruned. Its terminal period divides `3m`, so it is emitted.
Different traversal paths have different words. This proves completeness
and uniqueness, including periodic necklaces. It also gives lexicographic
output order without rotation canonicalization.

The stack has depth at most `3m` and at most `1+6m` pending nodes. Stored
prefix tuples occupy `O(m^2)` space. The result list adds output storage.
The implementation bounds popped nodes by the full ternary-tree size
`(3^(3m+1)-1)/2`; its input cap makes this bound explicit. No output-sensitive
or polynomial total-time theorem is asserted for the traversal.

## First working example and exact comparison

At `(5,1)` the complete suffix table has 216 cells and 9,438 states. Building
it performs 20,912 state transitions. Canonical traversal pops 3,888 nodes,
tries 9,266 symbols, performs 7,007 prefix transitions and 7,007 completion
lookups, rejects 3,120 prefixes, and tests 520 full prenecklaces. The period
test emits 356 words. It performs no rotation tests. All these counts include
the whole table, including cells not subsequently queried.

The separate literal method enumerates all 756,756 balanced words, performs
3,783,780 middle-letter scans, finds 5,340 valid words and canonicalizes
them using 80,100 rotations. Each middle-letter scan computes the numbers
of preceding `1`s and following `3`s. Their product contributes to `T`.
Its set of canonical words agrees exactly with the generator's output.

These counters measure named algorithmic operations. They do not pretend
that a table transition, Python tuple copy, set lookup and integer comparison
have identical cost. `benchmark.py` separately times five alternating calls
to the complete two implementations in fresh child processes. Timed regions
include startup, imports, table construction or literal enumeration,
canonicalization when applicable, JSON serialization of every output word,
and process exit. The parent checks complete returned sets after each pair,
outside timing.

## Scope and credit

This is a scoped constructive proof, exact finite conformance certificate
and local algorithm comparison. It adds no new cyclic-class formula, chain
existence theorem, or generally new necklace method. SHIN09-git supplies
the source question, cyclic contract and period conventions. Nick Petalas's
#6037/#4257/#4281/#4748 supply the credited count/endpoint/rotation background;
their programs and counts are not used to generate the new output. Ross
Richardson's #943/#934 retain the complete-call dirty-chain lineage. The
classical substring-count and dynamic-programming ingredients retain their
usual attribution. Exact source prose pins are in SOURCES.json.

Rotation equivalence does not preserve every physical routing or precision
cost. Native realization, physical selection, ownership, dirty restoration,
precision, tape movement, stock, compiler admission and the full supplier
bill remain separate obligations. No selected assumption, complete
multiplication theorem or new kappa follows. Independent human and formal
proof-kernel review are unclaimed.
154 changes: 154 additions & 0 deletions research/chain-necklace-generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# Generate valid cyclic chain schedules without literal-word enumeration

This contribution was produced by Nick Petalas. Apache-2.0.

Scoped constructive proof with exact finite conformance and a local algorithm
comparison. This answers the representative-generation question in SHIN09-git's
[PR6080](https://github.com/CrocSwap/integer-mult-bounds/pull/6080).

At `(m,c)=(5,1)`, the new generator emits the 356 credited valid cyclic classes
by testing 520 complete prenecklaces, with no rotation canonicalization. Its
complete preprocessing takes 20,912 residue transitions and stores 9,438 states
in 216 cells. Traversal pops 3,888 prefixes, tries 9,266 symbols, performs
7,007 prefix updates and completion lookups, and rejects 3,120 prefixes.
The separate literal comparator examines all 756,756 balanced words and
canonicalizes 5,340 valid words using 80,100 rotations. The complete output
sets agree. These named work counters are not interchangeable machine costs.

The construction joins an exact suffix-residue completion test to the
classical fixed-content prenecklace extension rule. [PROOF.md](PROOF.md) proves
safe pruning and exactly-once output for all positive moduli as an abstract
algorithm. The bounded implementation supports only `1<=m<=5`; its full
finite conformance covers every target in that range. The modulus-one case
is a combinatorial convention, not a characteristic-one ring.

## Reproduce

From the repository root, standard-library CPython 3.12:

```sh
/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -W error -B research/chain-necklace-generator/generate.py
/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -W error -B research/chain-necklace-generator/verify.py
/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -O -W error -B research/chain-necklace-generator/verify.py
/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -B scripts/research_package.py --package research/chain-necklace-generator
/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -B scripts/check_research_commit.py --package research/chain-necklace-generator --check-source-history
```

Any CPython 3.12 executable can replace the local path. `generate.py` prints
all 356 words using the source's letters `1,2,3`, together with work counters.
Internally the code uses `0,1,2`. The direct verifier prints a generated JSON
receipt bound to all eight package files and `PASS_CHAIN_NECKLACE_GENERATOR`.
It writes no files. No receipt is handwritten or committed.

The verifier compares all 15 target cases through modulus five against a
separate positional enumerator, with 3,927,606 literal word evaluations and
2,510 emitted representatives in total across those cases. Every emitted
word receives a complete four-role integer-matrix endpoint check. Small
suffix tables cover 187 distinct residue states checked against independently
constructed matrix-signature sets, and 210 completion queries. Eleven controls cover invalid inputs, periodic
classes, canonical orientation and chronological direction. In particular,
the `(3,1)` fixture retains two period-three classes and two period-nine
classes; filtering out all repeated words would be wrong.

## Local comparison

To reproduce the complete fresh-process comparison:

```sh
/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -W error -B research/chain-necklace-generator/benchmark.py
```

The script runs five alternating pairs at `(5,1)`, checks every returned word,
counts errors, and emits raw samples, environment, work counts and code hashes.
Every timed region includes process startup, imports, the complete computation,
JSON output of all 356 words and process exit. Each generator run rebuilds its
table. Parent-side output comparison is outside timing. No lazy iterator or
persisted cache is used. Timing is an optional local observation, not a CI
acceptance threshold or a comparison with the fastest possible generator.

On macOS 27.0.1 arm64 with CPython 3.12.15, the five-pair median complete
process time was 0.034534 seconds for the generator and 0.651576 seconds
for the literal comparator. The respective ranges were
0.033659 to 0.035091 seconds and 0.646261 to 0.653718 seconds. All ten processes
succeeded, with zero errors and five complete output-set comparisons.
Raw generator samples in seconds were `0.034534042, 0.035090750, 0.033994666, 0.033658959, 0.034655708`;
corresponding literal samples were `0.653717667, 0.646840042, 0.651576250, 0.652623667, 0.646261417`.
The shared host had 18 logical cores and load averages 1.86/2.80/3.06 before
measurement. Other processes remained running; interleaving exposes both
methods to that environment. These observations are local, not a guarantee
on another machine or a supplier runtime claim.

The literal comparator uses compiled binary searches to count the preceding
and following letters, and only canonicalizes words already known valid.
A separate profiling run identifies its full-word scan and binary searches
as the main computational cost. The new computation spends its remaining
work in prenecklace traversal, suffix construction and completion lookups.
Both run serially with the same interpreter and flags. No native supplier,
network or disk workload occurs in either computation.

## Scope and credit

SHIN09-git supplies the question, rotation/period contract and credited count
in PR6080. Nick Petalas's #6037/#4257/#4281/#4748 are count, endpoint and
rotation background; none of their code or stored count tables is imported
or replayed. Ross Richardson's #943/#934 retain the complete-call dirty-chain
lineage. The prenecklace theorem and fixed-content generation are classical;
credit Fredricksen, Kessler, Maiorana, Ruskey and Joe Sawada's
[2003 paper](https://www.socs.uoguelph.ca/~sawada/papers/alph.pdf), Theorem 1 and
Figure 1. The new contribution is the suffix-completion application, generator
and its exact finite work comparison, not a new general necklace algorithm.

[SOURCES.json](SOURCES.json) pins source prose at canonical base
`e54523517d0f03e58352e647c403a5af3e525a57`. Those files are historical provenance,
not runtime inputs. The source PR's head is
`abdcee87d8eb0f46db522b16596406f43e890d7e`. [TASK.json](TASK.json) records the
explicit change from `formal-interface-lemma` to this combinatorial side
question. The selected-baseline orientation passed with #7000 selected;
it supplies source-binding orientation only. The selected frontier is unchanged.
[Advance CLAIM](https://github.com/CrocSwap/integer-mult-bounds/discussions/1166#discussioncomment-19592067).

There is no new class-count formula, constant-amortized-time result, native
implementation, paid-profile saving, selected-assumption discharge, new kappa
or complete multiplication theorem. Physical routing, ownership, restoration,
precision, tape movement, stock, compiler and complete consumer costs remain
unpriced. No predecessor scientific suite, selected cold replay, Lean or Docker
run is included. Independent human and formal proof-kernel review are unclaimed.

## Implementation bounds

All retained Python functions fit within 60 physical lines. There is no
recursion, dynamic evaluation or network access. The modulus cap is five,
each suffix cell has at most 125 residues, and there are at most 216 cells.
Traversal has an explicit ternary-tree node bound and depth at most 15.
Matrix checks use four rows; receipt hashing accepts eight files capped at
256 KiB each. Explicit exceptions remain active under optimized execution.

Power of Ten rule 3 has a managed-runtime deviation in all three scripts.
Python allocates tuples, sets, dictionaries and exact integers during these
bounded computations. The caps above, stack depth, bounded output domain and
fixed source inventory constrain live storage. Runtime allocation internals
are not measured. A native fixed-buffer implementation would be required
before claiming allocation-free execution or lifting these limits.

Rule 9 has a standard-library dispatch deviation for iterators, sorting,
JSON and argument parsing. Targets are the named standard-library functions
and the two fixed comparison methods. The optional benchmark launches exactly
ten sequential bounded children, each with a 30-second timeout and checked
exit status, stderr and complete output. The registered scientific verifier
launches no child process. Warning-strict normal/optimized execution and AST
inspection provide local diagnostics; Ruff is unavailable in the specified
environment. No shared diagnostic configuration is changed.

## Open next questions

1. Can the suffix-state representation support a specified larger modulus
with a useful memory bound? First estimate or prove the reachable cell
sizes before raising the implementation cap. No output-sensitive traversal
bound is supplied here.
2. Can an actual chain consumer use these representatives at lower complete
cost? First specify its physical routing, precision and restored-owner
contract and price the emitted schedules. Rotation equivalence alone
supplies no such saving.

The structured questions and blockers are in TASK.json. No extension is
reserved or implemented after this first verified generator.
40 changes: 40 additions & 0 deletions research/chain-necklace-generator/SOURCES.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"schema_version": 1,
"base_commit": "e54523517d0f03e58352e647c403a5af3e525a57",
"files": [
{
"path": "certificates/selected-result.json",
"usage": "provenance",
"git_blob": "75d77b328ea02ded535e7f23aee5b1e55d0bb1a8",
"sha256": "154c3d30455d778fd3343b6b4851f0fa4665f0f7625b0adb3675e2bafbbf6bf1"
},
{
"path": "docs/research/agent-task-catalog.json",
"usage": "provenance",
"git_blob": "3404fbdd5e7529a0674eef26114b5071e4586cec",
"sha256": "ead6cdc9d0af6d58e8a49744d8ec8c7c15e13d2ba93c276c434bcf9b51b93a81"
},
{
"path": "research/chain-cyclic-schedules/PROOF.md",
"usage": "provenance",
"git_blob": "d826b2e8f6f038cf5ed341e442149af13fd43ae2",
"sha256": "faffad9f8f947818e7debff730de86bf3bbdbfa9b1ad72e13817a43e58085c85"
},
{
"path": "research/chain-cyclic-schedules/README.md",
"usage": "provenance",
"git_blob": "7b887b49d662a634576450f0c4115f064439a658",
"sha256": "f09a86865ae702b54188c6706bd33d76890afdbd829900f1aefbae9a4a867e74"
}
],
"bibliography": [
{
"title": "A fast algorithm to generate necklaces with fixed content",
"author": "Joe Sawada",
"year": 2003,
"doi": "10.1016/S0304-3975(03)00049-5",
"url": "https://www.socs.uoguelph.ca/~sawada/papers/alph.pdf",
"used": "Theorem 1 and Figure 1, credited prenecklace extension theorem; not a runtime input."
}
]
}
Loading
Loading