GitXplorerGitXplorer
f

numberpartitioning-cs

public
2 stars
0 forks
0 issues

Commits

List of commits on branch master.
Unverified
96823967a5b81ec8f5fa54c07bad628f09f6d549

Add an API for partioning generic elements with given weights

ffuglede committed 3 years ago
Unverified
53af54b554aefcfa3b318e3a98151d42c2fd5ec2

Bump version number

ffuglede committed 3 years ago
Unverified
56baeefb461e7a2593df655cb0cd4ca1f68565c3

Allow doubles as input numbers

ffuglede committed 3 years ago
Unverified
4e0ffee9b402915098593964408d2e690e08d944

Add xmldoc to main methods

ffuglede committed 3 years ago
Unverified
d0de18476a26392d80679f0811e6df6a0fe8fabd

Merge branch 'master' of github.com:fuglede/numberpartitioning-cs

ffuglede committed 3 years ago
Unverified
c046fcb7f4b6bc6453251f6a9f49a8458915d5e0

Add README.md

ffuglede committed 3 years ago

README

The README file for this repository.

Partition problem solvers in .NET

This repository includes some pure C# solvers for the multiway number partitioning optimization problem; in particular, the standard greedy algorithm, and the Karmarkar--Karp algorithm.

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 algorithms only aim 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 the public NuGet Gallery.

Example

Suppose we want to split the collect [4, 6, 7, 5, 8] into three parts. We can achieve that as follows:

var numbers = new[] { 4, 6, 7, 5, 8 };
var (partition, sizes) = KarmarkarKarp.Heuristic(numbers, 3);

Here, partition becomes [[4], [0, 3], [1, 2]], the indices corresponding to the elements [[8], [4, 7], [5, 6]], and sizes are the sums of the elements in 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:

var numbers = new[] { 5, 5, 5, 4, 4, 3, 3, 1 };
var (partition, sizes) = KarmarkarKarp.Heuristic(numbers, 3);

Here, sizes is [9, 10, 11] but it is possible to achieve a solution in which the sums of each part is 10.