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 will be published separately in the future.

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 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.

As an example, we use this NumPy module to solve Sudoku. As a bonus feature for the Sudoku piece, we also calculate an approximate rating of the puzzle (easy, medium, hard, or very hard).

How to Use It (Example)

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.

Repository

  • build/ The location where files are built.
  • dist/ The location for fully prepared files.
  • exact_cover/ The build tool 'poetry', seems to need this folder with a dummy python file so it doesn't worry about there not being any package.
  • 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.

Acknowledgement

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

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.4.2.tar.gz (17.2 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.4.2-cp38-cp38-win_amd64.whl (19.4 kB view details)

Uploaded CPython 3.8Windows x86-64

exact_cover-0.4.2-cp38-cp38-manylinux2014_x86_64.whl (39.3 kB view details)

Uploaded CPython 3.8

exact_cover-0.4.2-cp38-cp38-macosx_10_15_x86_64.whl (16.4 kB view details)

Uploaded CPython 3.8macOS 10.15+ x86-64

exact_cover-0.4.2-cp37-cp37m-win_amd64.whl (19.3 kB view details)

Uploaded CPython 3.7mWindows x86-64

exact_cover-0.4.2-cp37-cp37m-manylinux2014_x86_64.whl (39.8 kB view details)

Uploaded CPython 3.7m

exact_cover-0.4.2-cp37-cp37m-macosx_10_15_x86_64.whl (16.4 kB view details)

Uploaded CPython 3.7mmacOS 10.15+ x86-64

File details

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

File metadata

  • Download URL: exact_cover-0.4.2.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.7.9 Linux/5.4.0-1032-azure

File hashes

Hashes for exact_cover-0.4.2.tar.gz
Algorithm Hash digest
SHA256 55eb703422cf9730db28a51b45e56376865dedd7dc7052a036fdd559d23ca4a8
MD5 bff21a80444d95a841ac59878e94d8b4
BLAKE2b-256 310297235fa44cb720c0016b9e41c0125776be6275c1b8699c4bd681e522e1e7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: exact_cover-0.4.2-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 19.4 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.6 Windows/10

File hashes

Hashes for exact_cover-0.4.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 694b9ad9c1885a44ff4ba139f2e41eec1dcb5909271631b37d546c2db9e59445
MD5 1b0ca9a3a266e84e88ecdd22a28c0400
BLAKE2b-256 1c8eb7060f56ccf81fb21933c009e03400ed0619a22693964513b2773af22bbe

See more details on using hashes here.

File details

Details for the file exact_cover-0.4.2-cp38-cp38-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for exact_cover-0.4.2-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 011c92e1842c368435cfc6318f1a02377cc63a0a709a37838d366009b08720f9
MD5 511b257020cd1ae5d770d1dd24fd0768
BLAKE2b-256 6f8e9eeb82fcb6d10bc6d344cfc6dbb8d1dd97e501fd94e7531eb4dbcca1d4d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exact_cover-0.4.2-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 d37ed8350dc3daa08c0c1322ec371f46ed4c68c47e91c5adda0ff062199111dc
MD5 84c102fb7ece4f8b9dffe9f371decf15
BLAKE2b-256 d4609193a630157b7a284e34f42c106764898bd55ece2c7ad65aa05cce98e0e2

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for exact_cover-0.4.2-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 7feeccbb5d32d023de57e36c38b8272cafac24c5f3845e23e6b8937af66ee381
MD5 8563082156bcdaad5e72a28ca2484d07
BLAKE2b-256 ab6070341ccf2c4e5e0de75503421c54043ed4412a938a870b66e4e21e165763

See more details on using hashes here.

File details

Details for the file exact_cover-0.4.2-cp37-cp37m-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for exact_cover-0.4.2-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 174c3439d5658444670531a92e7369d4751423ee29cf9f7fa73e2651691c4396
MD5 a4335279d5c4fedf1564f4c31db82562
BLAKE2b-256 0149eb882f374ba4f5123c9039462a4deed8dc78f97db27fce34d59f59543d1a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exact_cover-0.4.2-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 e6c29ea6ddbbcdbeafde4ee2a4bacfb7b63786137263888118dea680ef19f88f
MD5 a8f5fb9d51ee27478702b7a9d112b052
BLAKE2b-256 617c03a10ad5440202f200c9f5f8bff2eb9aef6afdd3f5c6c6c47fd391ba289e

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