All-different constraint Created 2026-10-20 Updated 2026-11-05
An all-different constraint requires its variables to take pairwise distinct values. For domains and , the first two variables consume values one and two in every solution, forcing . Pairwise inequality propagation may miss this deduction, while a global propagator can use a matching in a bipartite graph between variables and values. Hall's marriage theorem supplies the feasibility condition: every subset of variables needs at least as many available values as variables. This constraint appears in schedules, assignments and numerical puzzles.
Four-page University of Oxford Finals examination, Trinity Term 2015, dated Tuesday 16 June 2015, 9:30 am–11:00 am. The original marks Questions 1 and 3 as corrected. Printed p. 2 concerns Horn clause proof search and admissible heuristics; p. 3 concerns a STRIPS encoding of a multi-stack Tower of Hanoi puzzle; p. 4 concerns constraint satisfaction problems, tree decomposition, and all-different constraints.
Public archive: Lawrence Attachment.zip, member 2015-is.pdf.
Held original: pdf/computer-science/finals/intelligent-systems/2015.pdf in the Oxford examinations repository. Editable transcription: tex/computer-science/finals/intelligent-systems/2015.tex.
Kakuro Created 2026-10-20 Updated 2026-11-05
Kakuro is a numerical cross-sum puzzle on a grid. Each open cell holds a digit from one to nine; every specified horizontal or vertical run must sum to its clue without repeating a digit within that run. It is naturally a constraint satisfaction problem: cells are variables, digits are domains, and each run combines a sum constraint with an all-different constraint. This formulation supports general reasoning about feasible domains and search strategies without selecting or solving a particular puzzle.
Use one variable for each cell, with . An empty cell has constraint domain ; a clue containing has singleton constraint domain . Thus all clues are retained automatically.
For every row impose , and for every column impose . For each impose an all-different constraint on
These are the nine prescribed boxes. There are 27 all-different constraints altogether. Nine pairwise distinct values chosen from a set of nine values must use every value exactly once, so these restrictions are precisely the Sudoku requirements. Equivalently, expand each all-different constraint into pairwise binary constraints for cells sharing a row, column, or box.
Use the ordinary treewidth of the primal graph of the constraint hypergraph: two variables are adjacent whenever they occur together in an all-different constraint. Equivalently, a tree decomposition of this constraint hypergraph has a bag containing every constraint scope, together with the running-intersection requirement for each variable. This is distinct from taking the treewidth of its incidence drawing.
Introduce a central bag and the five scope bags
Join to each of , join to , and join to :
Figure 1.
A minimum-width tree decomposition with central bag containing variables 2, 4, and 5
. Details.
Every constraint scope is contained in its own bag, and every variable occurs. The nontrivial running-intersection checks are: occurs along ; along the connected branches ; along ; along ; and along . Each of the other variables occurs in just one bag. This is therefore a valid tree decomposition of width .
Conversely, any of the original three-variable constraint scopes forms a clique of size three in the primal graph. Such a clique must be contained in one bag of every tree decomposition, forcing width at least two. The upper and lower bounds agree:
Choose the distinct values of and call them . There are choices. Constraint (5), on , immediately forces .
Constraints (2) and (3) require and , respectively. Since the constraint domain has exactly three values, . Constraint (2) now forces , and constraint (3) forces . Finally, constraint (4) permits or .
Every choice just described satisfies all five all-different constraints, and the forced-value argument shows there are no other possibilities. Hence
In the order , the two forms are
where ranges over the six permutations of .
True in some cases and false in others. Equality of the constraint domain size and treewidth does not decide satisfiability.
For an unsatisfiable example, let and impose the three binary all-different constraints , , and . The primal graph is a triangle, with treewidth two. It needs three different values, so no solution exists despite .
For a satisfiable example, take four variables with the same constraint domain and constraints
Its primal graph is a four-cycle of treewidth two: bags and give width two, while width one would require a forest. The assignment satisfies all constraints.
True in some cases and false in others: the two numerical inequalities do not guarantee a solution.
For an unsatisfiable example satisfying both inequalities, take six variables, a constraint domain with five values, and , , with all-different constraints on
Any pair of the six variables occurs together in at least one of these scopes: view the variables as the three pairs , and each scope as the union of two pairs. All six values must therefore be distinct. This is impossible with five available values, even though and .
For a satisfiable example with the same , take three disjoint groups of four variables and impose one all-different constraint on each group. Assign values separately in every group. All three constraints hold with .
These examples concern the intended cases in which the antecedent holds; they do not rely on vacuous truth of an implication.
Always true. An all-different constraint of arity requires distinct values. If , the pigeonhole principle makes even one such constraint impossible. Since , at least one is present, and consequently
Always true. Replace every all-different constraint by the pairwise inequalities on its scope. Then solutions are precisely the proper graph colourings of the primal graph using values from as colours. Put .
A graph of treewidth has a proper graph colouring with at most colours. Here is a constructive proof. In a width- tree decomposition, remove any leaf bag contained in its neighbor. If more than one bag remains, a leaf bag has a vertex absent from its neighbor. The running-intersection property means that vertex occurs only in the leaf bag, so every neighbor of the vertex belongs to that bag. It has at most neighbors. If only one bag remains, it has at most vertices and the same degree bound holds.
Remove the chosen vertex and repeat; deleting vertices cannot increase the width of the restricted tree decomposition. Now colour in reverse removal order. At most already coloured neighbors forbid colours, so one of colours is always available. Since the integer inequality gives , this produces a satisfying assignment:
The common unrestricted constraint domain and the exclusively all-different constraints are essential: arbitrary constraint relations do not reduce to graph colouring in this way.
Always true for a nonempty finite constraint domain, as conventionally assumed. Write and identify its values with . For each satisfying assignment , simultaneously replace every value by
This permutation of the common constraint domain preserves every equality and inequality, hence every all-different constraint. The resulting assignments are distinct: equality of even one coordinate before and after a shift forces . There is at least one variable, since and .
The cyclic group of shifts therefore partitions the solution set into disjoint orbits of size . It follows that
The conclusion includes an unsatisfiable instance, whose solution count is zero. Full permutation group orbits need not have size , because unused values can be permuted without changing an assignment; the cyclic-shift argument avoids that mistake.
If an empty constraint domain is permitted, there are zero assignments here, but the wording “divisible by ” then asks about divisibility by zero, which is ordinarily left undefined. The asserted divisibility uses the standard nonempty-domain convention, and the no-solution conclusion still holds for that degenerate input.
Use seven variables , with and every other constraint domain . Each conflict is a binary constraint .
The audience assumptions are part of the constraint satisfaction problem. Talks occupy whole common-length slots, room capacity and travel times impose no additional restrictions, and different CS year cohorts do not overlap. Physics attendees are treated as separate from those cohorts and the Head of Department. To protect possible society overlaps, assume society members occur in each relevant student cohort and that the Head of Department may also need to attend . No additional overlap between otherwise distinct audience groups is assumed. These are declared modelling choices, rather than claims about the actual membership of any group.
The shared-audience restrictions give the edge pairs
For example, the Head of Department's three introductions produce all three pairwise inequalities on ; the Physics audience gives . The possible society overlaps add
Together these thirteen inequalities and give a binary formulation. There is no need for an all-different constraint on all seven variables: compatible talks may run in parallel. At least two rooms suffice for the solution below.
If the Head of Department is stipulated not to attend , remove just ; the exhibited schedule remains valid. If actual society membership excludes a specified cohort, remove its corresponding additional conflicts too. This separation makes clear which restrictions are direct requirements and which protect assumed overlapping attendance.
Write for the prescribed occupied cells, with . The source’s lowercase in its coordinate bound is read as the stated board size . If two prescribed queens already attack each other, no completion exists. Each model below correctly expresses that impossibility as well as consistent inputs.
Model 1: one Boolean variable per cell. Let indicate occupancy. Require
and impose for every two distinct cells sharing a row, a column or a diagonal. Equivalently, impose exactly one queen in each row and column and at most one on each diagonal. Explicit diagonal constraints are
Together with the total-count constraint and the attacking-pair binary constraints, these force exactly the intended boards. The count and diagonal-sum constraints are legitimate many-variable constraints; this model need not be binary.
Model 2: one variable per labeled queen. Let for . Enumerate and restrict for . For every , impose the binary constraint
There are exactly variables and distinct rows, so these are distinct queens. Labeling the prescribed queens does not lose a board completion: the queens are physically indistinguishable, and any completion can be labeled to give the prescribed ones those indices. It does create redundant assignments for the remaining labels.
Model 3: one variable per row. Let be the queen’s column in row . For every , require
and impose for every . These restrictions give queens, one per row; the all-different constraint on the columns gives one per column. For nonattacking queens on rows, exactly one per row is forced by the pigeonhole principle, so no solutions are lost. Inconsistent prescribed positions in a single row give incompatible unary restrictions. All three are exact constraint satisfaction problem encodings of the queens completion problem.
Primal graph 2026-10-31
The graph with one vertex per variable and an edge between distinct variables occurring in a common constraint scope. Each hypergraph edge of the constraint hypergraph becomes a clique. A constraint satisfaction problem consisting only of all-different constraints on a common constraint domain is exactly a proper graph colouring problem on this primal graph.