Skip to main content

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.

Metadata

Release files for numberpartitioning 0.0.2

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

Source distribution (sdist)

Source distribution for numberpartitioning 0.0.2
File Size Uploaded
numberpartitioning-0.0.2.tar.gz 7.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for numberpartitioning 0.0.2
File Interpreter ABI Platform
numberpartitioning-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 15.5 kB

Release files / numberpartitioning-0.0.2.tar.gz

Download URL numberpartitioning-0.0.2.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
69f91be64a9c9e303f1ab681c2e065ac1b72926f382f3abcf916d205db6db5d2
BLAKE2b-256 checksum
How to use checksums
4d91caf55fccdd6cb455f867f7dc539c6ebfe82774d704d05caa09e88b3e2d8c
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / numberpartitioning-0.0.2-py3-none-any.whl

Download URL numberpartitioning-0.0.2-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6145e972f6b4e9ba403f73d4dd3d5b8dc96b368e7827a74e573b6104cce572d6
BLAKE2b-256 checksum
How to use checksums
cc042c0110ac8c5e0ddde048d1069bb0f59018e1d71d4c7f134f8a86c8dce185
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 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