dlx_rs
dlx_rs is a Rust library for solving exact cover/constraint problems problems using Knuth's Dancing Links (DLX) algorithm.
It also provides specific interfaces for some common exact cover problems, specifically:
- arbitrary Sudokus
- N queens problem
- Aztec diamond
- Pentomino tilings (TODO)
- graph colouring (TODO)
Setting up a general constraint problem
A constraint problem may be expressed in terms of a number of items [i_1,...,i_N] and options [o_1,...,o_M]. Each of the options "covers" some of the items, e.g. picking option o1 might involve selecting items i1, i5, and i7. The constraint problem is to find a collection of options which cover all of the items exactly once.
This can be expressed in terms of a matrix, where each option covers the items for which the corresponding entry is 1, and doesn't if it is 0
i1 i2 i3 i4 i5 i6 i7
o1 0 0 1 0 1 0 0
o2 1 0 0 1 0 0 0
o3 0 1 1 0 0 0 0
o4 1 0 0 1 0 1 0
o5 0 1 0 0 0 0 1
o6 0 0 0 1 1 0 1
The exact cover problem is that of finding a collection of options such that a 1 appears exactly once in each column.
This is achieved in the case above by selecting options [o_1,o_4,o_5].
The code to solve this is
use Solver;
let mut s = new;
s.add_option
.add_option
.add_option
.add_option
.add_option
.add_option;
let sol = s.next.unwrap_or_default;
assert_eq!;
Or, we can use strings in a case where we might want to generate the options at runtime
use Solver;
let mut s = new;
s.add_option
.add_option
.add_option
.add_option
.add_option
.add_option;
let sol = s.next.unwrap_or_default;
assert_eq!;
Solving a Sudoku
use Sudoku;
// Define sudoku grid, 0 is unknown number
let sudoku = vec!;
// Create new sudoku from this grid
let mut s = new_from_input;
let true_solution = vec!;
// Checks only solution is true solution
let solution = s.next.unwrap_or_default;
assert_eq!;
assert_eq!;