Skip to main content

Solve exact cover problems

Project description

Finding Exact Covers in NumPy

PyPI version Deploy wheels to pypi Run Python tests

This is a Python 3 package to solve exact cover problems using Numpy. It is based on https://github.com/moygit/exact_cover_np by Moy Easwaran. Jack Grahl ported it to Python 3, fixed some bugs and made lots of small improvements to the packaging.

The original package by Moy was designed to solve sudoku. Now this package is only designed to solve exact cover problems given as boolean arrays. It can be used to solve sudoku and a variety of combinatorial problems. However the code to reduce a sudoku to an exact cover problem is no longer part of this project. It can be found at:

Another project, 'polyomino' by Jack Grahl uses this algorithm to solve polyomino tiling problems. It can be found at:

Summary

The exact cover problem is as follows: given a set X and a collection S of subsets of X, we want to find a subcollection S* of S that is an exact cover or partition of X. In other words, S* is a bunch of subsets of X whose union is X, and which have empty intersection with each other. (Example below; more details on wikipedia.)

This NumPy module uses Donald Knuth's Algorithm X (also known as Dancing Links) to find exact covers of sets. For details on Algorithm X please see either the Wikipedia page or Knuth's paper. Specifically, we use the Knuth/Hitotsumatsu/Noshita method of Dancing Links for efficient backtracking. Please see Knuth's paper for details.

How to Use It

Suppose X = {0,1,2,3,4}, and suppose S = {A,B,C,D}, where

A = {0, 3}
B = {0, 1, 2}
C = {1, 2}
D = {4}.

Here we can just eyeball these sets and conclude that S* = {A,C,D} forms an exact cover: each element of X is in one of these sets (i.e. is "covered" by one of these sets), and no element of X is in more than one.

We'd use exact_cover to solve the problem as follows: using 1 to denote that a particular member of X is in a subset and 0 to denote that it's not, we can represent the sets as

A = 1,0,0,1,0    # The 0th and 3rd entries are 1 since 0 and 3 are in A; the rest are 0.
B = 1,1,1,0,0    # The 0th, 1st, and 2nd entries are 1, and the rest are 0,
C = 0,1,1,0,0    # etc.
D = 0,0,0,0,1

Now we can call exact_cover:

>>> import numpy as np
>>> import exact_cover as ec
>>> S = np.array([[1,0,0,1,0],[1,1,1,0,0],[0,1,1,0,0],[0,0,0,0,1]], dtype='int32')
>>> ec.get_exact_cover(S)
array([0, 2, 3])

This is telling us that the 0th row (i.e. A), the 2nd row (i.e. C), and the 3rd row (i.e. D) together form an exact cover.

Implementation Overview

The NumPy module (exact_cover) is implemented in four pieces:

  • The lowest level is quad_linked_list, which implements a circular linked-list with left-, right-, up-, and down-links.
  • This is used in sparse_matrix to implement the type of sparse representation of matrices that Knuth describes in his paper (in brief, each column contains all its non-zero entries, and each non-zero cell also points to the (horizontally) next non-zero cell in either direction).
  • Sparse matrices are used in dlx to implement Knuth's Dancing Links version of his Algorithm X, which calculates exact covers.
  • exact_cover provides the glue code letting us invoke dlx on NumPy arrays.

The package now has some pure Python modules for helper functions, with the main algorithm in the C-only package exact_cover_impl.

How to develop

The package uses poetry and most of the setup for development uses that tool.

To install locally (as an editable package): poetry install

To build: poetry build

To run tests: poetry run test or poetry run doctest

To open a Python shell with the package available: poetry run python

The exception is running the C unit tests: make c_tests

Repository

  • build/ The location where files are built.
  • dist/ The location for fully prepared files.
  • exact_cover/ The Python code.
  • obj/ Where the compiled C code is going to be output.
  • src/ The C sources.
  • tests/ Tests for both the Python package and the C code.
  • tools/ Code used in analysing and working with the library. This is not distributed with the package.

Acknowledgements

Thanks very much to Moy Easwaran (https://github.com/moygit) for his inspiring work!

Munit aka µnit (https://nemequ.github.io/munit/) is a wonderful unit testing framework for C code.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

exact_cover-0.6.3.tar.gz (18.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

exact_cover-0.6.3-cp38-cp38-win_amd64.whl (21.7 kB view details)

Uploaded CPython 3.8Windows x86-64

exact_cover-0.6.3-cp38-cp38-manylinux_2_31_x86_64.whl (43.1 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.31+ x86-64

exact_cover-0.6.3-cp38-cp38-macosx_10_15_x86_64.whl (17.7 kB view details)

Uploaded CPython 3.8macOS 10.15+ x86-64

exact_cover-0.6.3-cp37-cp37m-win_amd64.whl (21.7 kB view details)

Uploaded CPython 3.7mWindows x86-64

exact_cover-0.6.3-cp37-cp37m-manylinux_2_31_x86_64.whl (43.6 kB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.31+ x86-64

exact_cover-0.6.3-cp37-cp37m-macosx_10_15_x86_64.whl (17.7 kB view details)

Uploaded CPython 3.7mmacOS 10.15+ x86-64

File details

Details for the file exact_cover-0.6.3.tar.gz.

File metadata

  • Download URL: exact_cover-0.6.3.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.7.10 Linux/5.4.0-1046-azure

File hashes

Hashes for exact_cover-0.6.3.tar.gz
Algorithm Hash digest
SHA256 cae8e8884d6fd25a207f44d29d6a77f800866bfbedd1f9d38bc24f7d1197f7bd
MD5 d8b21ee890975f3a032ee5ef5a4b6fd8
BLAKE2b-256 73f21a6744075cc82aba00b5b1b0a8806445bbf55aac0aec08518b06427f6b73

See more details on using hashes here.

File details

Details for the file exact_cover-0.6.3-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: exact_cover-0.6.3-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 21.7 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.9 Windows/10

File hashes

Hashes for exact_cover-0.6.3-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 6c5b01c794e08a66886878e680bd420bd0858510b8a621f3cd0b1f503f5589f6
MD5 49b0c1c175fbc13734e973e6a042de55
BLAKE2b-256 0f76330648d9cc8a5dc846cbc71c2ab62aff208539e51bbd340a9473f15671c6

See more details on using hashes here.

File details

Details for the file exact_cover-0.6.3-cp38-cp38-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for exact_cover-0.6.3-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 8583bc2e6487bb3ec76f9d21fe1a086fe7c7c6b298984c058eed179abdfa6e46
MD5 bb7d17bd0e9caed248248aa6f1d7e24f
BLAKE2b-256 b59ee702b891daafdc668bfeb175b03f03d1085b32b81a3898e87956cba6574b

See more details on using hashes here.

File details

Details for the file exact_cover-0.6.3-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for exact_cover-0.6.3-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 24f582dabce65533d62c76272c4f9b693bcbddc3e8e6055a516f2acd37d96dde
MD5 917f452f9d4366ccee4f3b97665a1816
BLAKE2b-256 52f4b5548f16e8311ba68da5125162a835bb92ef55d02fea6ec44460ec8ffd09

See more details on using hashes here.

File details

Details for the file exact_cover-0.6.3-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: exact_cover-0.6.3-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 21.7 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.7.9 Windows/10

File hashes

Hashes for exact_cover-0.6.3-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 d83a479203e8d0fb8403bc1614aa7baf1716e727acb080aa04d1e0c23df6cb97
MD5 216d5043be0fdf951fae1ccff9b32779
BLAKE2b-256 23ca549ae9f8ea10213526fb727842b513f44a28c8186a970d1b8d368a0a0471

See more details on using hashes here.

File details

Details for the file exact_cover-0.6.3-cp37-cp37m-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for exact_cover-0.6.3-cp37-cp37m-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 b0287369da21337a6826ecb31f2576a1784d8f8e4d8bdecbcae8bfef735660db
MD5 b93302586475d380470f397f1bcf26e5
BLAKE2b-256 1b6585aa47e8850903fb5ed24e5213928bbba0ad6abb24a2688d96a7f9aaa74a

See more details on using hashes here.

File details

Details for the file exact_cover-0.6.3-cp37-cp37m-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for exact_cover-0.6.3-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 363059bf5d39aa530a252f923cda482faa0076588814a02aac294f870a3693ac
MD5 89af9aafb1e9f8b05c14720cc8fe3e39
BLAKE2b-256 1b89b105fa8c745f46de4492666a5f653c1ccbc52c98f06c4c9d21f7cd777f27

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page