Giter Site home page Giter Site logo

jw013 / exact-cover-rs Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 47 KB

Rust implementation of Knuth's Algorithm X for solving exact cover problems

Home Page: https://docs.rs/xcov/

License: Mozilla Public License 2.0

Rust 100.00%
algorithm-x dancing-links dlx exact-cover knuth-algorithm-x

exact-cover-rs's Introduction

Exact Cover

This crate provides an implementation of Knuth's Algorithm X for solving exact cover problems.

The algorithms and data structures in this module, as well as the terminology of "options" and "items", come from Donald Knuth's The Art of Computer Programming, section 7.2.2.1 "Dancing Links"1.

  • No dependencies
  • 100% safe Rust
  • Choose whether to find a single solution or all possible solutions
  • Designed for flexibility (see below)

From a high level, Algorithm X is simply a backtracking exhaustive search algorithm. At a lower level, Algorithm X cleverly manipulates linked lists using the "dancing links" technique to make the backtracking more efficient. This crate implements the backtracking search as a provided trait method ExactCoverProblem::search, while the dancing links data structure and operations are implemented by the Dlx struct. A custom implementation of the ExactCoverProblem trait can make use of the dancing links structure provided by Dlx, control the order items are selected for covering, and apply additional filters to options for problems beyond pure exact cover.

A ready-to-use implementation using the minimum remaining values heuristic to select items (i.e. select the item covered by the fewest number of available options) is provided with MrvExactCoverSearch. Its code can be helpful as a starting point for custom implementations.

Examples

The following pure exact cover problem is often used as an example in texts by Knuth, and consists of 7 items and 6 options. For a more practical application, see examples/sudoku.rs.

# use xcov::*;
let mut builder = DlxBuilder::new(7, 0);
builder.add_option(&[      3,    5      ]);
builder.add_option(&[1,       4,       7]);
builder.add_option(&[   2, 3,       6   ]);
builder.add_option(&[1,       4,    6   ]);
builder.add_option(&[   2,             7]);
builder.add_option(&[         4, 5,    7]);

let mut problem = MrvExactCoverSearch::new(builder.build());
problem.search();
let solution = problem
   .current_solution()
   .expect("this example problem has a solution")
   .into_iter()
   .collect::<Vec<_>>();
assert_eq!(
   solution,
   [
      &[1, 4, 6][..],
      &[2, 7],
      &[3, 5],
   ]
);

// can look for additional solutions but this problem has none
assert!(!problem.search());
assert!(problem.current_solution().is_none());

Footnotes

  1. A pre-print is accessible at https://www-cs-faculty.stanford.edu/~knuth/fasc5c.ps.gz. โ†ฉ

exact-cover-rs's People

Contributors

jw013 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.