Skip to content

Fix #417: Add ConsecutiveOnesSubmatrix model#667

Open
GiggleLiu wants to merge 5 commits intomainfrom
issue-417-consecutive-ones-submatrix
Open

Fix #417: Add ConsecutiveOnesSubmatrix model#667
GiggleLiu wants to merge 5 commits intomainfrom
issue-417-consecutive-ones-submatrix

Conversation

@GiggleLiu
Copy link
Contributor

Summary

Add ConsecutiveOnesSubmatrix model — a satisfaction (decision) problem from Garey & Johnson A4 SR14. Given an m×n binary matrix and integer K, determines whether K columns can be selected and permuted so that each row's 1-entries are consecutive.

Fixes #417

@codecov
Copy link

codecov bot commented Mar 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.02%. Comparing base (a4c3516) to head (d7f6f58).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #667      +/-   ##
==========================================
+ Coverage   96.97%   97.02%   +0.04%     
==========================================
  Files         277      281       +4     
  Lines       37161    37685     +524     
==========================================
+ Hits        36038    36564     +526     
+ Misses       1123     1121       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

GiggleLiu and others added 2 commits March 16, 2026 20:25
Add satisfaction problem for the Consecutive Ones Submatrix problem
(Garey & Johnson A4 SR14): given an m×n binary matrix and bound K,
decide if K columns can be selected and permuted so each row's
1-entries are consecutive (C1P).

- Model in src/models/algebraic/ with Heap's algorithm for permutations
- 14 unit tests including Tucker matrix examples
- CLI support (pred create ConsecutiveOnesSubmatrix --matrix --k)
- Paper entry with CeTZ figure and bibliography
- Trait consistency and example_db registration

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@GiggleLiu
Copy link
Contributor Author

Implementation Summary

Changes

  • src/models/algebraic/consecutive_ones_submatrix.rs — New satisfaction problem model (ConsecutiveOnesSubmatrix). Uses Heap's algorithm to enumerate all K! permutations of selected columns and check each row's 1-entries are contiguous (C1P).
  • src/unit_tests/models/algebraic/consecutive_ones_submatrix.rs — 14 unit tests covering evaluation, brute force solving, serialization, edge cases (K=0, wrong config length, invalid values), and panic tests.
  • src/models/algebraic/mod.rs / src/models/mod.rs — Module registration and re-export.
  • src/unit_tests/trait_consistency.rs — Added trait consistency check.
  • problemreductions-cli/src/commands/create.rs — CLI pred create support with --matrix and --k flags.
  • problemreductions-cli/src/cli.rs — Help text updated.
  • docs/paper/reductions.typ — Problem definition entry with formal definition, background (C1P, PQ-trees, Booth & Lueker), complexity note, Tucker matrix example, and CeTZ figure.
  • docs/paper/references.bib — Added Booth 1975 (PhD thesis) and Booth & Lueker 1976 bibliography entries.
  • src/example_db/fixtures/examples.json — Regenerated (33 model examples).
  • docs/src/reductions/problem_schemas.json — Regenerated (42 schemas).

Deviations from Plan

  • Tucker matrix paper example assertion corrected: only 2 of 4 three-column subsets have C1P (not 4 as initially assumed). Verified by manual analysis.

Open Questions

  • None

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new algebraic satisfaction model, ConsecutiveOnesSubmatrix, to the core library, along with CLI support, examples/fixtures, tests, and regenerated schema/reduction-graph documentation.

Changes:

  • Implement ConsecutiveOnesSubmatrix model (schema registration, evaluation, variants, example-db spec).
  • Add unit tests and wire the model into trait-consistency checks and module re-exports.
  • Integrate with CLI pred create, example fixtures, and regenerate docs JSON / paper references & section.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/unit_tests/trait_consistency.rs Adds trait/dims sanity check coverage for the new model.
src/unit_tests/models/algebraic/consecutive_ones_submatrix.rs Introduces model-specific unit tests, brute-force solver checks, and serialization tests.
src/models/mod.rs Re-exports ConsecutiveOnesSubmatrix from the models top-level module.
src/models/algebraic/mod.rs Registers the new algebraic module and adds example-db spec wiring.
src/models/algebraic/consecutive_ones_submatrix.rs Implements the model, registers schema metadata, declares variants, and adds example-db entry + tests module.
src/example_db/fixtures/examples.json Adds a fixture instance + sample/optimal configs for the new model.
problemreductions-cli/src/commands/create.rs Adds pred create ConsecutiveOnesSubmatrix instance construction via --matrix and --k.
problemreductions-cli/src/cli.rs Documents the new problem’s CLI flags in the help text.
docs/src/reductions/reduction_graph.json Adds the model node to the generated reduction graph docs (including shifted indices).
docs/src/reductions/problem_schemas.json Adds the model schema to the generated schema docs.
docs/paper/references.bib Adds Booth (1975) and Booth–Lueker (1976) references.
docs/paper/reductions.typ Adds a paper definition + worked example/figure block for the model.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +133 to +164
/// Check if any permutation of the given columns has C1P.
fn any_permutation_has_c1p(&self, cols: &[usize]) -> bool {
let k = cols.len();
if k == 0 {
return true;
}
let mut perm: Vec<usize> = cols.to_vec();
// Generate all permutations using Heap's algorithm
let mut c = vec![0usize; k];
if self.has_c1p(&perm) {
return true;
}
let mut i = 0;
while i < k {
if c[i] < i {
if i % 2 == 0 {
perm.swap(0, i);
} else {
perm.swap(c[i], i);
}
if self.has_c1p(&perm) {
return true;
}
c[i] += 1;
i = 0;
} else {
c[i] = 0;
i += 1;
}
}
false
}
impl SatisfactionProblem for ConsecutiveOnesSubmatrix {}

crate::declare_variants! {
default sat ConsecutiveOnesSubmatrix => "2^(num_cols) * num_rows",
let selected = cfg.enumerate().filter(((i, v)) => v == 1).map(((i, v)) => i)
[
#problem-def("ConsecutiveOnesSubmatrix")[
Given an $m times n$ binary matrix $A$ and a positive integer $K <= n$, determine whether there exists a subset of $K$ columns of $A$ whose columns can be permuted so that in each row all 1's occur consecutively (the _consecutive ones property_).
Comment on lines +3 to +6
//! Given an m×n binary matrix A and a positive integer K ≤ n, determine whether
//! there exists a subset of K columns whose columns can be permuted so that in
//! each row all 1's occur consecutively. NP-complete (Booth, 1975) via
//! transformation from Hamiltonian Path.
…17-consecutive-ones-submatrix

# Conflicts:
#	docs/paper/reductions.typ
#	docs/src/reductions/problem_schemas.json
#	docs/src/reductions/reduction_graph.json
#	problemreductions-cli/src/commands/create.rs
#	src/example_db/fixtures/examples.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Model] ConsecutiveOnesSubmatrix

2 participants