Two-Colour Zig-Zag Numberlink on Rectangles: A Forbidden-Pattern Characterization, a Constructive Polynomial-Time Algorithm, and a Lean Verification
Zzyv Zzyzek
cs.DS
Oct 19, 2026 · v1
cs.LG
TL;DR
About 27,000 lines of Lean 4 with Mathlib verify both directions of the characterization and the thin-case DP; finite case checks use native_decide.
Abstract
In Zig-Zag Numberlink, pairs of endpoints in a grid must be joined by vertex-disjoint paths that together visit every cell. The problem is NP-complete when the number of pairs is un- bounded (Adcock et al.). For a single pair it is the Hamiltonian path problem in rectangular grid graphs, solved by Itai, Papadimitriou and Szwarcfiter. Two pairs on a rectangle form the paired 2-disjoint path cover problem. We settle the case of two pairs on rectangles with the endpoints anywhere in the grid. If both sides of the rectangle are at least 11, an instance is solvable if and only if no entry of a finite catalogue of forbidden patterns occurs. The catalogue consists of a parity condition, an alternation condition on the border, a handful of local corner and edge patterns, 65 listed corner configurations, and a rule that propagates forced routes out of the corners before testing alternation. If a side is at most 10, a frontier dynamic programming algorithm decides the instance in time linear in the long side. The characterization is construc- tive. Solutions are built by deleting empty lines, splitting the rectangle by straight cuts, and splicing the pieces back together, in time O((R+C) RC). Both directions of the characterization and the correctness of the dynamic programming algorithm are verified in Lean 4. The finite case analyses, which are window certificates for the corner configurations and a check of all irreducible instances with sides 11-22, are carried out by the Lean compiler via native decide. We describe the algorithm, the catalogue, the proof structure, the compromises made in the formalization, and implementations in Python, JavaScript and C that produce identical output.
Problem
The paper decides when two-colour Zig-Zag Numberlink instances on R×C rectangles are solvable, with endpoints allowed anywhere in the grid. This is the paired 2-disjoint path cover problem, and it extends the single-pair Hamiltonian path characterization of Itai, Papadimitriou and Szwarcfiter.
Approach
For rectangles with both sides at least 11, solvability is characterized by a finite catalogue of forbidden patterns: parity, border alternation, local corner and edge patterns, listed corner configurations, and forced-route propagation. Completeness is proved by induction on area, using line-deletion reductions undone by band extension and straight-cut moves whose pieces glue back together. Rectangles with a side of at most 10 are decided by a frontier (plug) dynamic programming algorithm. Both directions and the DP's correctness are formalized in Lean 4 with Mathlib, and the finite case analyses run through native_decide.
Results
The characterization yields a constructive O((R+C)RC) algorithm, and the thin case is decided in time linear in the long side. The Lean development has about 27,000 lines, including 10 window-certificate theorems and 144 base-case theorems for boxes with sides 11–22. The polynomial time bound is not formalized. Python, JavaScript and C implementations produce identical output and agree with the Lean definitions in randomized tests.