Books · Solving Puzzles through Mathematical Programming
Chapter 8
Langford’s Problem
A Langford pairing of order is a sequence of integers containing each of twice, subject to the condition that the two copies of are separated by exactly other integers. For order , the sequence is a pairing: the two s have exactly one element between them, the two s have exactly two elements between them, and the two s have exactly three elements between them. For the sequence works: the s are separated by one element, the s by two, and so on.
The problem was posed by C. Dudley Langford in . He noticed that his son arranged blocks with the pattern of a pairing and asked: for which do such sequences exist, and how many are there? Langford’s first observation is striking: pairings exist iff or . The proof is elementary; we sketch it below. The enumeration question is harder. For small the counts of distinct pairings (up to reversal) are with no closed-form expression known. Since the count grows super-exponentially, the problem fits naturally inside constraint programming: Langford’s original question, “find one pairing,” is a short CP-SAT model that takes milliseconds; the enumeration question, “count all pairings,” is a longer run of the same model with the enumerate_all_solutions flag.
Why
Suppose a pairing of order exists. Call the two positions of the copies of the pair with ; the condition is . Summing over , Separately, . Adding the two identities, The left side is an even integer, so . Since , this reduces to . The product is the product of two consecutive integers, so exactly one of them is even; the condition therefore forces or , which is or . Roy O. Davies () proved the converse by an explicit construction, so the criterion is both necessary and sufficient.
The programming model
The CP-SAT encoding uses a single integer variable for each position of each value.
Variables
For each , introduce two integer variables and , each with domain , representing the two positions occupied by the copies of .
Pairing constraint
Distinctness
Reversal symmetry
A Langford pairing and its reversal are both pairings. To break this symmetry, the standard convention is to force the first occurrence of to precede its midpoint: This pins the sequence’s orientation and halves the enumeration count.
A solver in twenty lines
from ortools.sat.python import cp_model
def langford(n):
if n % 4 not in (0, 3):
return None
m = cp_model.CpModel()
pos = [[m.NewIntVar(1, 2*n, "")
for _ in range(2)]
for _ in range(n)]
flat = [pos[i][j] for i in range(n)
for j in range(2)]
m.AddAllDifferent(flat)
for i in range(n):
m.Add(pos[i][1] - pos[i][0] == i + 2)
m.Add(pos[0][0] < n) # reversal symmetry
s = cp_model.CpSolver()
s.Solve(m)
seq = [0] * (2 * n)
for i in range(n):
for j in range(2):
seq[s.Value(pos[i][j]) - 1] = i + 1
return seq
A single pairing comes back in under one hundred milliseconds for any up to about ; enumeration of all pairings is exponentially harder, but CP-SAT still handles in seconds.
Small instances
The pairing returned for is Figure 8.1 shows it as a row of eight cells with coloured arcs linking the paired positions; the arc for pair spans positions, illustrating the separation constraint.
For , the solver returns one of twenty-six pairings of that order. Figure 8.2 displays it in the same arc-diagram format.
Sources. C. Dudley Langford’s original problem appeared as Note in The Mathematical Gazette volume (), page , inspired by watching his son arrange coloured blocks. Roy O. Davies’s constructive solution of the existence question appeared in The Mathematical Gazette volume (), pages –; his formula appears verbatim in Donald Knuth’s The Art of Computer Programming, Volume , Exercise , with a short clean proof. The enumeration counts up to have been tabulated by John E. Miller; Miller’s values agree with CP-SAT enumeration up to the values the solver can complete in reasonable time (about ). The generalisation to triples (three copies of each number, separated appropriately) is a Skolem-like sequence and has its own existence criterion.