<p>The dynamic programming algorithm  is O(n^{2k}) where k is the number of distinct items and n is the total number of items. This can be very slow irrespective of the implementation. Typically, when solving an NP-hard problem, heuristics are required for speed.</p>

<p>I suggest you consider Next Fit Decreasing Height (NFDH) and First Fit Decreasing Height (FFDH) from Coffman et al. They are 2-optimal and 17/10-optimal, respectively, and they run in O(n log n) time.</p>

<p>I recommend you first try  NFDH: sort in decreasing order, store the result in a linked list, then repeatedly try to insert the items starting from the beginning (largest values first) until you have filled the bin or there is no more items that can be inserted. Then go to the next bin and so on.</p>

<p><strong>References</strong>: </p>

<p>Owen Kaser, Daniel Lemire, <a href="http://arxiv.org/abs/cs.DS/0703109" rel="noreferrer">Tag-Cloud Drawing: Algorithms for Cloud Visualization</a>, Tagging and Metadata for Social Information Organization (WWW 2007), 2007. (See Section 5.1 for a related discussion.)</p>

<p>E. G. Coffman, Jr., M. R. Garey, D. S. Johnson, and R. E. Tarjan. Performance bounds for level-oriented two-dimensional packing algorithms. SIAM J. Comput., 9(4):808–826, 1980.</p>
Bin Packing - Brute force recursive solution - How to make it faster
<p>I have an array which contains a list of different sizes of materials : <code>{4,3,4,1,7,8}</code> . However, the bin can accomodate materials upto size 10. I need to find out the minimum number of bins needed to pack all the elements in the array.</p>

<p>For the above array, you can pack in 3 bins and divide them as follows: <code>{4,4,1}</code>, <code>{3,7}</code> , <code>{8}</code> . There are other possible arrangements that also fit into three stock pipes, but it cannot be done with fewer</p>

<p>I am trying to solve this problem through recursion in order to understand it better.</p>

<p>I am using <a href="http://algo2.iti.kit.edu/appol/les7.pdf" rel="nofollow">this</a> DP formulation (page 20 of the pdf file)</p>

<blockquote>
  <p>Consider an input (n1;:::;nk) with n = ∑nj items<br>
  Determine set of k-tuples (subsets of the input) that can be packed into a single bin<br>
  That is, all tuples (q1;:::;qk) for which OPT(q1;:::;qk) = 1<br>
  Denote this set by Q For each k-tuple q , we have OPT(q) = 1<br>
  Calculate remaining values by using the recurrence :  OPT(i1;:::;ik) = 1 +
  minOPT(i1 - q1;:::;ik - qk)</p>
</blockquote>

<p>I have made the code, and it works fine for small data set. But if increase the size of my array to more than 6 elements, it becomes extremely slow -- <strong>takes about 25 seconds to solve an array containing 8 elements</strong> Can you tell me if theres anything wrong with the algorithm? I dont need an alternative solution --- <strong>just need to know why this is so slow, and how it can be improved</strong></p>

<p>Here is the code I have written in C++ :</p>

<pre><code>void recCutStock(Vector&lt;int&gt; &amp; requests,  int numStocks)
{
 if (requests.size() == 0)
 {

     if(numStocks &lt;= minSize)
    {
        minSize = numStocks;

    }
//   cout&lt;&lt;"GOT A RESULT : "&lt;&lt;numStocks&lt;&lt;endl;
        return ;
 }
 else
 {
    if(numStocks+1 &lt; minSize) //minSize is a global variable initialized with a big val
    {
     Vector&lt;int&gt; temp ; Vector&lt;Vector&lt;int&gt; &gt; posBins;
     getBins(requests, temp, 0 , posBins); // 2-d array(stored in posBins) containing all possible single bin formations

    for(int i =0; i &lt; posBins.size(); i++)
    {
        Vector&lt;int&gt; subResult;
        reqMinusPos(requests, subResult,  posBins[i]);  // subtracts the initial request array from the subArray
        //displayArr(subResult);


            recCutStock(subResult,  numStocks+1);


    }
    }
    else return;
 }

}
</code></pre>

<p>The getBins function is as follows : </p>

<pre><code>void getBins(Vector&lt;int&gt; &amp; requests, Vector&lt;int&gt; current, int index, Vector&lt;Vector&lt;int&gt; &gt; &amp; bins)

{

if (index == requests.size())

{

if(sum(current,requests) &lt;= stockLength &amp;&amp; sum(current, requests)&gt;0 )

        {
                bins.add(current);
            //  printBins(current,requests);
            }
            return ;
        }
        else
        {

        getBins(requests, current, index+1 , bins);
                current.add(index);
            getBins(requests, current, index+1 , bins);
        }
}
</code></pre>