Cardinality constraint 2026-10-25
A cardinality constraint restricts how many members of a specified collection are selected or satisfy a property. For Boolean variables, exactly selected entries gives the displayed sum; upper and lower bounds give at-most and at-least variants. Constraint propagation can maintain the number already selected and the number still available. A requirement involving many variables is a global constraint, even when it has a compact arithmetic description.
Nonattacking knights problem 2026-10-25
Choose distinct squares of an chessboard so that no chosen pair has absolute coordinate differences or . One constraint satisfaction problem representation has square-valued variables, requiring distinctness and pairwise nonattack. Another has one Boolean variable per square, a cardinality constraint fixing their sum to , and one mutual-exclusion constraint for every edge of the knight attack graph. The first has fewer variables when is small but represents each unlabelled placement up to times; the second uses only binary constraint domains and sparse local attack restrictions.
One variable per knight. Use , with each constraint domain equal to . For every pair , requireEvery satisfying assignment places distinct nonattacking knights, and any such placement gives an assignment after labelling its knights. This has only variables and binary constraints, but domains of size . Relabelling the knights gives assignments for the same unlabelled board, so the representation contains considerable redundancy.
One Boolean variable per square. Use for every . Impose the cardinality constraintand for every knight-attack pair impose . There are variables, each with a two-element constraint domain, and only local attack restrictions, because each square attacks at most eight others. The single counting constraint is a global constraint, not a binary constraint.
The square encoding avoids knight-label redundancy and has sparse local attack relations; the knight encoding uses fewer variables when is small and makes single-knight moves natural. Both are exact representations of the nonattacking knights problem. Neither may impose one knight per row: several knights can legally share a row.