Skip to content

Commit

Permalink
Roughly halve the search space in Langford pairs example
Browse files Browse the repository at this point in the history
Note that the reflection of a Langford sequence is also a Langford
sequence. Fix the position of the first 1 in the sequence to reduce
the search space.
  • Loading branch information
hsanzg committed Jan 25, 2024
1 parent 76382bb commit 56b24cf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions examples/langford_pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ fn main() {

let mut solver = ExactCovers::new(&items, &[]);
for i in 1..=N {
for j in 1..2 * N - i {
// Optimization: half of the Langford pairs for a given value of $n$
// are reflections of the others. Reduce the search space by placing
// the first 1 in position $1\leq s_j<n$.
let first_slot_range = 1..if i == 1 { N } else { 2 * N - i };
for j in first_slot_range {
let k = i + j + 1;
let option = [&Item::Number(i), &Item::Slot(j), &Item::Slot(k)];
solver.add_option(option);
Expand All @@ -62,7 +66,9 @@ fn main() {
unreachable!("ordered option should match (number, slot, slot) pattern");
}
}
// Print the found Langford sequence.
// Print the found Langford sequence, and its reflection.
println!("{:?}", placement);
placement.reverse();
println!("{:?}", placement);
})
}
2 changes: 1 addition & 1 deletion src/xc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl<'i, I: Eq> ExactCovers<'i, I> {
/// Appends an option to the exact cover problem.
///
/// Once all options have been specified, use [`Self::solve`] to visit
/// all exact coverings of $I$ with a subset of options in $O$.
/// all exact coverings of $I$ with a subset of options in $\mathcal{O}$.
///
/// # Panics
///
Expand Down

0 comments on commit 56b24cf

Please sign in to comment.