0

Given a list of integers NUMS and an integer k (buckets), we must allocate every integer from NUMS to some bucket such that maximum of sum of integers across all buckets is minimised.

A simple algorithm which first sorts NUMS and then allocates integer to smallest bucket so far doesn't work. Eg. 5,5,4,4,4 for k=2. Output is 13 : {5,4}, {5,4,4}, where as correct output is 12 : {5,5}, {4,4,4}.

However, if the input integer array consists of distinct integers, will the above algorithm produce correct output? Looking for rigours analysis to understand why/why not.

Ajax
  • 163
  • 7

3 Answers3

2

No. Here is a counterexample (you seem to be sorting integers in decreasing order): $60, 50, 40, 29, 20$ and $2$ buckets. Your algorithm creates one bucket with sum $109$ and one bucket with sum $90$, while there exists a solution having a bucket with sum $99$ and the other with sum $100$.

Besides, if you algorithm worked then it could be used to solve the partition problem in polynomial-time, showing that $\mathsf{P}=\mathsf{NP}$. Indeed, given a set of integers $S$, there is a way to bucket the numbers in $S$ into $2$ buckets with maximum sum $\frac{1}{2} \sum_{x \in S} S$ if and only if the answer to the corresponding partition instance is "yes". Notice that partition remains NP-hard when the integers in $S$ are distinct.

For constant $k$ you can solve your problem in pseudopolynomial time using dynamic programming. Let $x_1, \dots, x_n$ be the integers in your instance, let $M = \max_{i=1,\dots, n} x_i$, and $\vec{s} = (s_1, s_2, \dots, s_{k-1}) \in \{0, 1, \dots, M\}^{k-1}$. Call $OPT[i, \vec{s}]$ the minimum possible sum of the elements in the $k$-th bucket when only the first $i$ elements are considered and the sum of the elements in $j$-th bucket with $j < k$ can be at most $s_j$.

Also let $(\vec{s}_{-j}, s'_j)$ denote the vector $\vec{s}$ in which the $j$-th component has been replaced by $s'_j$.

For $i = 0$ and all $\vec{s}$, $OPT[i, \vec{s}] = 0$.

For $i \ge 1$: $$ OPT[i, \vec{s}] = \min\left\{OPT[i-1, \vec{s}] + x_i, \min_{\substack{j=1,\dots,k-1 \\ x_i \le s_j}} OPT[i-1, (\vec{s}_{-j}, s_j - x_i) ] \right\}, $$ where the minimum over an empty set is $+\infty$.

The value of the optimal solution is: $$ \min \{ x \mid x \in \{ 0, 1, \dots, M \} \wedge OPT[n, (x, x, \dots, x)] \le x \}. $$

The optimal solution itself can be recovered by retracing the optimal choices of the minimum backwards.

Steven
  • 29,724
  • 2
  • 29
  • 49
1

Consider NUMS = [9, 7, 5, 4, 3] and k=2.

Greedy algorithm would return [9, 4] and [7, 5, 3] which is not optimal, as [9, 5] and [7, 4, 3] is an optimal solution.

Nathaniel
  • 18,309
  • 2
  • 30
  • 58
0

I think that this is equivalent to the problem of scheduling jobs on multiple identical processors in a way that minimizes makespan (when the last job finishes).

This has been shown to be an NP-Hard problem:

Minimizing the maximum completion time ($P||{\displaystyle C_{\max }}C_{\max }$) is NP-hard even for identical machines, by reduction from the partition problem.
Many exact and approximation algorithms are known. …
Identical-machines scheduling — Minimizing the maximum completion time

That means that many of the greatest mathematicians in the world have, for many decades, failed to find a good algorithm for your problem.

You might look at some of the suggested algorithms.

Ray Butterworth
  • 298
  • 1
  • 2
  • 12