All-different by bipartite matching
Also known as Régin's all-different, GAC all-different, Hall-set reasoning
constraint constraint-search
All-different keeps exactly the values that some assignment of pairwise distinct values uses (generalised arc consistency). Each run builds the graph between the variables and the values of their domains and matches every variable to its own value by augmenting paths, from an empty matching; if no such matching exists, or fewer values than variables remain, the constraint fails. With matched edges pointing from variable to value and the others from value to variable, an edge stays if it is matched, if its variable and value lie in one strongly connected component, or if a free value reaches its value; every other value goes. This catches every Hall set: k variables that share k values keep them to themselves. Use it for Sudoku groups, Latin squares and logic grids.
Pseudocode
algorithm All-Different(x₁, …, x_n, D)input: variables x₁, …, x_n with domains Doutput: D with every value that no distinct assignment uses removed, or failureV ← D(x₁) ∪ … ∪ D(x_n)if |V| < n thenreturn failure ▷ fewer values than variablesM ← ∅ ▷ a new matching on every runfor i in 1, …, n doif not augment(x_i, M) then ▷ one augmenting-path search per variablereturn failureG ← x → v for each (x, v) ∈ M, and v → x for each other v ∈ D(x)R ← the nodes of G reachable from a value that M leaves freeC ← strongly-connected-components(G) ▷ Tarjan, without recursionfor each x_i and each v ∈ D(x_i) doif (x_i, v) ∉ M and C(x_i) ≠ C(v) and v ∉ R thenremove(D, x_i, v) ▷ no maximum matching uses the edgereturn DIn the crates
constraint-searchComplete (generalised arc consistent) all-different from a maximum matching and the strongly connected components of the variable–value graph.
Lineage
Comes from
builds on Constraint propagation to a fixpoint
Led to
Nothing on this site.
Papers
- A Filtering Algorithm for Constraints of Difference in CSPs Jean-Charles Régin · 1994· Proceedings of the Twelfth National Conference on Artificial Intelligence (AAAI-94)