Pure Python solver for the multi-way partition problem
Project description
Partition problem solvers in Python
This repository includes an implementation of the Karmarkar--Karp algorithm (also known as the largest differencing method) for the multiway number partitioning optimization problem, as well as some greedy algorithms.
The problem
Concretely, the problem we solve is the following: Suppose S is some collection of integers, and k is some positive integer, find a partition of S into k parts so that the sums of the integers in each part are as close as possible.
The objective function describing "closeness" is usually taken to be the difference between the largest and smallest sum among all parts. The optimization version is NP-hard, and the bundled algorithm only aims to provide a good solution in short time. This also means that they can be useful for other objective functions such as, say, the variance of all sums.
Installation
The package is available from PyPI:
pip install numberpartitioning
It can also be obtained from conda-forge:
mamba install -c conda-forge numberpartitioning
Examples
Suppose we want to split the collection [4, 6, 7, 5, 8]
into three parts. We can achieve that as follows:
from numberpartitioning import karmarkar_karp
numbers = [4, 6, 7, 5, 8]
result = karmarkar_karp(numbers, num_parts=3)
Here, result.partition
becomes [[8], [4, 7], [5, 6]]
, and result.sizes
are the sums of each part, [8, 11, 11]
. This happens to be optimal.
As noted on Wikipedia, an example where this approach does not give the optimal result is the following:
from numberpartitioning import karmarkar_karp
numbers = [5, 5, 5, 4, 4, 3, 3, 1]
result = karmarkar_karp(numbers, num_parts=3)
Here, result.sizes
is [9, 10, 11]
but it is possible to achieve a solution in which the sums of each part is 10.
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
Built Distribution
File details
Details for the file numberpartitioning-0.0.2.tar.gz
.
File metadata
- Download URL: numberpartitioning-0.0.2.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69f91be64a9c9e303f1ab681c2e065ac1b72926f382f3abcf916d205db6db5d2 |
|
MD5 | 8970f06ed866cb3214455a5cf4409d38 |
|
BLAKE2b-256 | 4d91caf55fccdd6cb455f867f7dc539c6ebfe82774d704d05caa09e88b3e2d8c |
File details
Details for the file numberpartitioning-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: numberpartitioning-0.0.2-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6145e972f6b4e9ba403f73d4dd3d5b8dc96b368e7827a74e573b6104cce572d6 |
|
MD5 | 03679f5a87d80f6d19f959684514d87d |
|
BLAKE2b-256 | cc042c0110ac8c5e0ddde048d1069bb0f59018e1d71d4c7f134f8a86c8dce185 |