Distributing marbles into buckets for maximal colour sharing

207 views Asked by At

i've got a problem that feels very much like it's NP-hard but I would love some help proving it primarily.

Secondary to that, if an optimal polynomal time algorithm can be proposed that is even better, although even good "greedy" approximations are fine.

Given M marbles, B buckets and each bucket having capacity K, where M = BK , find some distribution of the marbles that maximizes the number of colours that are shared. colour A and colour B are considered to be shared if they appear together in the same bucket. That is, we can increase a share count for each unique pairing seen of some A and some B (A != B), provided they are seen in the same bucket. We want to optimise for this share count.

An example python function which might be used to score such an algorithm:

def score(aBuckets):
    seenPairs = set()
    for myBucket in aBuckets:
        for colourA in myBucket:
            for colourB in myBucket:
                if colourA != colourB:
                    seenPairs.add((colourA, colourB))
    return len(seenPairs)//2

The distribution of colours is known for a given problem, and can be sorted and added in any order you desire

The colours are provided as some array, where each element at an index represents a colour count. The sum of each element gives you M. We cannot make any assumptions about how this array is distributed, just that it is provided.

Example:

BucketId Colours
0 A, C
1 A, C
2 B, D
3 B, D

M = 8, B = 4, K = 2

Colour array: 2,2,2,2

Clearly, this is non-optimal since we may swap C in bucket 1, with D in bucket 2 and achieve a better outcome (more colours shared).

Any insight into this problem should be infinitely appreciated, Thanks :)

0

There are 0 answers