Skip to main content

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=bool)
>>> print(ec.get_exact_cover(S))
[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.

To see the total number of distinct solutions, we can use the function get_solution_count:

>>> ec.get_solution_count(S)
1

See the file examples.md for more detailed examples of use.

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.

Metadata

Release files for exact-cover 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for exact-cover 1.4.0
File Size Uploaded
exact_cover-1.4.0.tar.gz 20.4 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for exact-cover 1.4.0
File
exact_cover-1.4.0-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
exact_cover-1.4.0-cp311-cp311-manylinux_2_31_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.31+ x86-64 Details
exact_cover-1.4.0-cp311-cp311-macosx_12_0_x86_64.whl CPython 3.11 CPython 3.11 macOS 12.0+ x86-64 Details
exact_cover-1.4.0-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
exact_cover-1.4.0-cp310-cp310-manylinux_2_31_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.31+ x86-64 Details
exact_cover-1.4.0-cp310-cp310-macosx_12_0_x86_64.whl CPython 3.10 CPython 3.10 macOS 12.0+ x86-64 Details
exact_cover-1.4.0-cp39-cp39-win_amd64.whl CPython 3.9 CPython 3.9 Windows x86-64 Details
exact_cover-1.4.0-cp39-cp39-manylinux_2_31_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.31+ x86-64 Details
exact_cover-1.4.0-cp39-cp39-macosx_12_0_x86_64.whl CPython 3.9 CPython 3.9 macOS 12.0+ x86-64 Details
exact_cover-1.4.0-cp38-cp38-win_amd64.whl CPython 3.8 CPython 3.8 Windows x86-64 Details
exact_cover-1.4.0-cp38-cp38-manylinux_2_31_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.31+ x86-64 Details
exact_cover-1.4.0-cp38-cp38-macosx_12_0_x86_64.whl CPython 3.8 CPython 3.8 macOS 12.0+ x86-64 Details
exact_cover-1.4.0-cp37-cp37m-win_amd64.whl CPython 3.7 CPython 3.7 pymalloc Windows x86-64 Details
exact_cover-1.4.0-cp37-cp37m-manylinux_2_31_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.31+ x86-64 Details
exact_cover-1.4.0-cp37-cp37m-macosx_12_0_x86_64.whl CPython 3.7 CPython 3.7 pymalloc macOS 12.0+ x86-64 Details

Total release size: 493.8 kB

Release files / exact_cover-1.4.0.tar.gz

Download URL exact_cover-1.4.0.tar.gz
Size 20.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6eae7fec29ea5569ba1006b55f68a87656e43582a6e3cb5016499e902180c2d5
BLAKE2b-256 checksum
How to use checksums
3495956415fa886ccd74c86cc8de35a97cec3e2e6890be5d90f730c08a8fcc40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.7.17 Linux/5.15.0-1059-azure

Release files / exact_cover-1.4.0-cp311-cp311-win_amd64.whl

Download URL exact_cover-1.4.0-cp311-cp311-win_amd64.whl
Size 22.1 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
b41cd1395325eb208161a362387b06cce61dd414e871751e7a3feb94235ad602
BLAKE2b-256 checksum
How to use checksums
4ed8339cf330759550c634c2e07b8e3553b5a46b6ea175480b363fb375ade70f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.11.8 Windows/10

Release files / exact_cover-1.4.0-cp311-cp311-manylinux_2_31_x86_64.whl

Download URL exact_cover-1.4.0-cp311-cp311-manylinux_2_31_x86_64.whl
Size 50.9 kB
Tags CPython 3.11 Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
d5cbc366ce1daf3519aafd2fb6d2ebc421dc71b352dd22baa5453f4524d67160
BLAKE2b-256 checksum
How to use checksums
5814de4605f96c36db51bceb469ef22f97dc30bc591bd562dd8e71a49bf6df80
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.11.8 Linux/5.15.0-1059-azure

Release files / exact_cover-1.4.0-cp311-cp311-macosx_12_0_x86_64.whl

Download URL exact_cover-1.4.0-cp311-cp311-macosx_12_0_x86_64.whl
Size 28.4 kB
Tags CPython 3.11 macOS 12.0+ x86-64
SHA-256 checksum
How to use checksums
859d9545ddc5b7792f93478533d93c3acad4d7b858f6936fcae7d664ecdeada8
BLAKE2b-256 checksum
How to use checksums
0087e533b858c364f893efc9bbfaf58621f5263b06aeae0aa828870582654f5d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.11.8 Darwin/21.6.0

Release files / exact_cover-1.4.0-cp310-cp310-win_amd64.whl

Download URL exact_cover-1.4.0-cp310-cp310-win_amd64.whl
Size 22.1 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
db4d8c17891a1b3e369415fb7392f2b0fb3065e3115026e92b1466458ff92461
BLAKE2b-256 checksum
How to use checksums
5c35a21ea1a81127e2c1ca4894b1fbeb48575a8cd17abaaae22839372acf105f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.10.11 Windows/10

Release files / exact_cover-1.4.0-cp310-cp310-manylinux_2_31_x86_64.whl

Download URL exact_cover-1.4.0-cp310-cp310-manylinux_2_31_x86_64.whl
Size 49.8 kB
Tags CPython 3.10 Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
8dde4fb1e4878667f7a58555f3e295997167c36bd3c72db5c5d981cea7611d04
BLAKE2b-256 checksum
How to use checksums
ecac196474d45b9e97f4990a15e67af89c73f47145b5009c9baa30e30d3eb2e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.10.14 Linux/5.15.0-1059-azure

Release files / exact_cover-1.4.0-cp310-cp310-macosx_12_0_x86_64.whl

Download URL exact_cover-1.4.0-cp310-cp310-macosx_12_0_x86_64.whl
Size 20.9 kB
Tags CPython 3.10 macOS 12.0+ x86-64
SHA-256 checksum
How to use checksums
5c3f409f7c2b1260578345d707ddf6504597d0885a4edba75586f0bfb74176c4
BLAKE2b-256 checksum
How to use checksums
ec6c6a5d3097651345d151cdc74382d7f9df485de41593f7121d370ed3278d6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.10.13 Darwin/21.6.0

Release files / exact_cover-1.4.0-cp39-cp39-win_amd64.whl

Download URL exact_cover-1.4.0-cp39-cp39-win_amd64.whl
Size 22.1 kB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
091254a47753001f2f3821c098655e638301c3221b37826590e61528078fd3ee
BLAKE2b-256 checksum
How to use checksums
2438be7304ded969410d1f2b531b234014c95cc241bb9774ee4300935401792b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.9.13 Windows/10

Release files / exact_cover-1.4.0-cp39-cp39-manylinux_2_31_x86_64.whl

Download URL exact_cover-1.4.0-cp39-cp39-manylinux_2_31_x86_64.whl
Size 49.6 kB
Tags CPython 3.9 Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
7680dffff5e69f5ec431dc1f65f9badb9cf3d22d1ace22c5b4d248c86d3e469a
BLAKE2b-256 checksum
How to use checksums
26b3a9e53a8a93b162f09b5b607fa838039a04641a420996682e063db7e4b746
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.9.19 Linux/5.15.0-1059-azure

Release files / exact_cover-1.4.0-cp39-cp39-macosx_12_0_x86_64.whl

Download URL exact_cover-1.4.0-cp39-cp39-macosx_12_0_x86_64.whl
Size 20.9 kB
Tags CPython 3.9 macOS 12.0+ x86-64
SHA-256 checksum
How to use checksums
9d9dccb4be8beb132fd98ac904e5d42769edbe8090bc6c8c91b6eb5795e1cd28
BLAKE2b-256 checksum
How to use checksums
f07130656230a8cee647ef5ff83dc1850956c73123839d3a8fdc05597016fc72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.9.18 Darwin/21.6.0

Release files / exact_cover-1.4.0-cp38-cp38-win_amd64.whl

Download URL exact_cover-1.4.0-cp38-cp38-win_amd64.whl
Size 22.1 kB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
867397e767e24aca133386a711b42a2a7dfecc561fa496c30628cb2c68b1b478
BLAKE2b-256 checksum
How to use checksums
9c045cd538914e6f79883e70716aef566decdee59087b6ba1ee6b0f3a492a723
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.8.10 Windows/10

Release files / exact_cover-1.4.0-cp38-cp38-manylinux_2_31_x86_64.whl

Download URL exact_cover-1.4.0-cp38-cp38-manylinux_2_31_x86_64.whl
Size 50.1 kB
Tags CPython 3.8 Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
c6b55675205ce6f8a13ea48127046fc6fa04de6d7f535dc46eb6c020002dc2b1
BLAKE2b-256 checksum
How to use checksums
0d049bd737074ea95a34f6cce418b4d574f52bb229d2e2e50048bb79e97f4374
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.8.18 Linux/5.15.0-1059-azure

Release files / exact_cover-1.4.0-cp38-cp38-macosx_12_0_x86_64.whl

Download URL exact_cover-1.4.0-cp38-cp38-macosx_12_0_x86_64.whl
Size 20.9 kB
Tags CPython 3.8 macOS 12.0+ x86-64
SHA-256 checksum
How to use checksums
a05e0ed3f2a9d66487a34ef17fe6f4f743a753769a26940523c87c7bcf10c2fc
BLAKE2b-256 checksum
How to use checksums
d26cd9dbbad4ef8f19a57545c8e69a16578e5de3cb4d636458a3d1cf136b6f66
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.8.18 Darwin/21.6.0

Release files / exact_cover-1.4.0-cp37-cp37m-win_amd64.whl

Download URL exact_cover-1.4.0-cp37-cp37m-win_amd64.whl
Size 22.1 kB
Tags CPython 3.7 CPython 3.7 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
ea77e068696fb9507c6b98ed5f2eb0e5ad1422ed2abf5a9ffc81c39c0980780d
BLAKE2b-256 checksum
How to use checksums
41fb53d0765315f4de56581c3f314b9885f905b1df3784861a5ca6e87c461589
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.7.9 Windows/10

Release files / exact_cover-1.4.0-cp37-cp37m-manylinux_2_31_x86_64.whl

Download URL exact_cover-1.4.0-cp37-cp37m-manylinux_2_31_x86_64.whl
Size 50.7 kB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
9da08df0167d36f88eb03be1d4fb04870d416273ab361c7059af0c9fd34abf3e
BLAKE2b-256 checksum
How to use checksums
ec401bada59c653ea49c11012e44f08717d5543d0ea12dfedde889a18bcdb9a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.7.17 Linux/5.15.0-1059-azure

Release files / exact_cover-1.4.0-cp37-cp37m-macosx_12_0_x86_64.whl

Download URL exact_cover-1.4.0-cp37-cp37m-macosx_12_0_x86_64.whl
Size 20.8 kB
Tags CPython 3.7 CPython 3.7 pymalloc macOS 12.0+ x86-64
SHA-256 checksum
How to use checksums
e2945d951b6ac8c4873451d0c6f4957af9480829b517a68b429d2f1edf61c549
BLAKE2b-256 checksum
How to use checksums
ce1ed35de5dd2e4d2476fc05a24246d21e2135a26c03da03ca139c8cc0146bbb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.7.17 Darwin/21.6.0

Release history Release notifications | RSS feed

1.5.0

15 release files

This release

1.4.0 This release

16 release files

1.3.0

16 release files

1.2.2

16 release files

1.2.1

16 release files

1.2.0

16 release files

1.1.0

20 release files

1.0.1

9 release files

1.0.0

5 release files

0.8.0

7 release files

0.7.2

7 release files

0.7.1

7 release files

0.7.0

7 release files

0.6.3

7 release files

0.6.2

7 release files

0.6.1

7 release files

0.6.0

7 release files

0.5.0

7 release files

0.4.2

7 release files

0.4.1

7 release files

0.4.0

7 release files

0.3.2

7 release files

0.3.1

7 release files

0.3.0

7 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

1 release file

0.2.1

2 release files

0.1.0

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page