Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is an NP-hard problem. In other words, it's not possible to find an optimal solution without exploring all combinations, and the number of combinations is n^M (where M is the size of you array, and n the number of beans). It's a problem very similar to <em><a href="http://en.wikipedia.org/wiki/Cluster_analysis" rel="nofollow">clustering</a></em>, which is also NP-hard. </p> <p>If your data set is small enough to deal with, a brute force algorithm is best (explore all combinations).</p> <p>However, if your data set is big, you'll want a polynomial-time algorithm that won't get you the optimal solution, but a good approximation. In that case, I suggest you use something similar to <em><a href="http://en.wikipedia.org/wiki/K-means_clustering" rel="nofollow">K-Means</a></em>...</p> <p>Step 1. Calculate the expected sum per bin. Let <em>A</em> be your array, then the expected sum per bin is <em>SumBin = SUM(A) / n</em> (the sum of all elements in your array over the number of bins). </p> <p>Step 2. Put all elements of your array in some collection (e.g. another array) that we'll call <em>The Bag</em> (this is just a conceptual, so you understand the next steps).</p> <p>Step 3. Partition <em>The Bag</em> into <em>n</em> groups (preferably randomly, so that each element ends up in some bin <em>i</em> with probability 1/<em>n</em>). At this point, your bins have all the elements, and <em>The Bag</em> is empty.</p> <p>Step 4. Calculate the sum for each bin. If result is the same as last iteration, <strong>exit</strong>. (this is the <em>expectation</em> step of <em>K-Means</em>)</p> <p>Step 5. For each bin <em>i</em>, if its sum is greater than <em>SumBin</em>, pick the first element greater than <em>SumBin</em> and put it back in <em>The Bag</em>; if its sum is less than <em>SumBin</em>, pick the first element less than <em>SumBin</em> and put back in <em>The Bag</em>. This is the gradient descent step (aka <em>maximization</em> step) of <em>K-Means</em>.</p> <p>Step 6. Go to step 3.</p> <p>This algorithm is just an approximation, but it's fast and guaranteed to converge.</p> <p>If you are skeptical about a randomized algorithm like the above, after the first iteration when you are back to step 3, instead of assigning elements randomly, you can do so optimally by running the <em><a href="http://en.wikipedia.org/wiki/Assignment_problem" rel="nofollow">Hungarian algorithm</a></em>, but I am not sure that will guarantee better over-all results. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload