GitXplorerGitXplorer
f

numberpartitioning

public
20 stars
4 forks
4 issues

Commits

List of commits on branch master.
Unverified
36d0de82a460e7d2ee9ce20adf9c72d28cf165f9

Bump version number and update changelog

ffuglede committed 3 years ago
Verified
24803bd5be58e6c76f6fc3dbe187cd2d9c0995dd

Merge pull request #1 from madsthoisen/master

ffuglede committed 3 years ago
Unverified
10375ff7763dcf5af51c0835344c63780c99586e

Sort combined partitions as discussed in PR #1

mmadsthoisen committed 3 years ago
Unverified
de3f968b0813638ed98d21970082d6c71f394bc9

Sort combined partitions as discussed in PR #1

mmadsthoisen committed 3 years ago
Unverified
942ce839080e66042ae3623ba2d55aa29584e2d5

Correct error as pointed out in pull request #1

mmadsthoisen committed 3 years ago
Unverified
c4ca177d00163a969bfb98552ca6f023659fc82b

Remove unintended print statement

mmadsthoisen committed 3 years ago

README

The README file for this repository.

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.