1
$\begingroup$

Here is the problem. We have N jars. Each jar can have any number of beans. These beans have to be distributed among C children. The resulting distribution should serve two goals Now

  • the number of beans that each child gets must be the same (+/- 1 when total number of beans is not a multiple of C)

  • each child to get all the beans from minimum number of distinct jars. Why? It is possible that beans in a jar may be contaminated. We want as few children to get infected as possible. E.g. if we have two jars with N beans each and two children, each child gets N beans. we would want child one to get N beans from jar 1 and child two to get beans from jar 2. As opposed to a solution, wherein child 1 gets half beans from jar 1 and half beans from jar 2. In former solution, if jar 1 is contaminated, only child 1 is infected. In latter solution, both children are infected.

So if

C = 5
beans = {B0:12, B1:12, B2:12, B3:12, B4:12} // Jar:<Bean-Count>

Distribution should be

{C1:{B0:12}, C1:{B1:12}, C2:{B2:12}, C3:{B3:12}, C4:{B4:12}}

Because each child has beans from exactly one jar. Following distribution will be less preferred

{C0:{B0:6, B1:6}, C1:{B0:6, B1:6}, C2:{B2:12}, C3:{B3:12}, C4:{B4:12}}

What will be the algorithm for achieving the distribution strategy with above goals in mind?

$\endgroup$
5
  • $\begingroup$ Welcome to CS.SE! 1. Can you make "each child to get all the beans from minimum number of distinct jars" more precise? Suppose there is one solution where C1 gets from 2 jars and C2 from 2 jars; and another solution where C1 gets from 1 jar and C3 from 3 jars. Which one should be returned? Does it matter? Each child might get beans from a different number of jars. Are you taking the max of those numbers? The sum? Something else? 2. What approaches have you considered so far? And what is your question? You state a problem, but I'm not sure what your question about it is. $\endgroup$
    – D.W.
    Commented May 26, 2017 at 14:55
  • $\begingroup$ Generally speaking, we're happy to help you understand the concepts but just solving exercises for you is unlikely to achieve that. You might find this page helpful in improving your question. $\endgroup$
    – D.W.
    Commented May 26, 2017 at 14:56
  • $\begingroup$ Thanks D.W. Hope its more clear now. I have tried a greedy approach but that doesn't work in all cases. I am wondering if this is a NP-hard problem and can be translated into one of the existing NP-hard problem. $\endgroup$
    – hobgoblin
    Commented May 29, 2017 at 5:47
  • $\begingroup$ Are you more interested in a practical solution, or in theoretical worst-case asymptotic running times? If it's a practical problem, roughly how large will N and C be? Also, you still haven't told us how you want to combine the number of beans to get a specific objective function. Based on the explanation, I'm guessing that it makes the most sense to compute the number of children who get a bean from each jar (giving you N numbers, one per jar), and average that across all the jars. $\endgroup$
    – D.W.
    Commented May 29, 2017 at 6:30
  • $\begingroup$ N and C will not be very high (< 50). Any solution works as long as it is not of exponential complexity. There are two objectives here. First one is hard requirement. For the second objective, objective function can be as what you stated. Though we don't need to average it as number of jars is constant. $\endgroup$
    – hobgoblin
    Commented May 29, 2017 at 7:21

1 Answer 1

1
$\begingroup$

If this is a practical problem, it can be solved using integer linear programming (ILP).

We'll introduce three kinds of integer variables:

  • $x_{i,j}$ counts the number of beans from jar $i$ that are given to child $j$.

  • $y_{i,j}$ is 1 if child $j$ gets any beans from jar $i$, or 0 otherwise.

  • $z_i$ is the number of children who get at least one bean from jar $i$.

Then we'll add some constraints to enforce the requirements in the question:

  • $\lfloor B/C \rfloor \le \sum_i x_{i,j} \le \lceil B/C \rceil$ where $B$ is the total number of beans across all the jars. This ensures that each child receives the same number of beans.

  • $0 \le x_{i,j} \le B_i y_{i,j}$, where $B_i$ is the number of beans in jar $i$, and $0 \le y_{i,j} \le x_{i,j}$. This ensures that $x_{i,j}$ and $y_{i,j}$ are consistent with each other.

  • $\sum_j x_{i,j} \le B_i$. This ensures that you don't give out more beans from jar $i$ than are available.

  • $z_i = \sum_j y_{i,j}$.

Finally, the objective function is the average of the $z_i$'s, i.e., $\frac{1}{N} \sum_i z_i$. We feed this to an integer linear programming solver and ask it to minimize the objective function, subject to the constraints above.

The worst-case running time of ILP solvers is exponential time, but in practice they are sometimes much more efficient than you might think. If you have $N,C < 50$, I suspect this might be efficient enough to work in practice. The only way to find out for sure is to try it.

$\endgroup$
2
  • $\begingroup$ I see. I suppose there is no solution which is not exponential complexity for this? $\endgroup$
    – hobgoblin
    Commented May 30, 2017 at 13:59
  • $\begingroup$ @hobgoblin, I don't know. This is the best I can think of right now, but it's not a proof that there is no better algorithm. $\endgroup$
    – D.W.
    Commented May 30, 2017 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.