Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7712845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:43:44+00:00 2026-06-01T01:43:44+00:00

Here is a excise from The book Algorithm Design Manual . In the bin-packing

  • 0

Here is a excise from The book “Algorithm Design Manual“.

In the bin-packing problem, we are given n metal objects, each weighing between zero and one kilogram. Our goal is to find the smallest number of bins that will hold the n objects, with each bin holding one kilogram at most.

The best-fit heuristic for bin packing is as follows. Consider the
objects in the order in which they are given. For each object, place
it into the partially filled bin with the smallest amount of extra
room after the object is inserted
. If no such bin exists, start a new
bin. Design an algorithm that implements the best-fit heuristic
(taking as input the n weights w1,w2,…,wn and outputting the number
of bins used) in O(nlogn) time.

Ok, this excise seems not hard. My initial understanding is that for the best-fit heuristic approach, I just every time look for a bin with minimum available space and try to put the object in. If the object does not fit to the bin with minimum space, I create a new bin.

I can build a BST to store the bins and each time when an object is put into a bin, I can delete that bin from the tree, update the bin’s available space and re-insert the bin to the tree. This will mains O(logN) for every object placement.

However, I noticed the bold and italic part of the excise “For each object, place it into the partially filled bin with the smallest amount of extra room after the object is inserted“.

So this means I am not looking for a bin which has minimum available space, instead, I am looking for one that if I place the current object in, the resulting available space (after placing the object) will be minimum.

For example, if bin1’s current space is 0.5, bin2 is 0.7. So currently, bin1 is the minimum one. But if the current object is 0.6, then the object can’t be placed into bin1, instead of creating a new bin, I have to find bin2 to put the object as bin2 – object = 0.7 – 0.5 = 0.2 which is then minimum.

Am I right? Does the bold part of the statement really mean like what I thought? Or is it just as simple as “find a bin with minimum space, if can place the object, then place; if can’t, then create a new bin”?

Thanks

Edit: adding part of my java code for my new understanding of the bold part.

public void arrangeBin(float[] w) {
   BST bst = new BST();
   bst.root = new Node();

   int binCount = 0;
   for (int i = 0;i < w.length;i++) {
      float weight = w[i];
      Node node = bst.root;
      float minDiff = 1;
      Node minNode = null;
      while(node!=null) {
         if (node.space > weight) {
             float diff = node.space - weight;
             if (minDiff > diff) {
                 minDiff = diff;
                 minNode = node;
                 node = node.left;
             }
         }
         else 
             node = node.right;
      }
      if (minNode == null) {
         binCount++;
         Node node = new Node();
         node.space -= weight;
         bst.insert(node);
      }
      else {
         minNode.space -= weight;
         bst.delete(minNode);
         bst.insert(minNode);
      }
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:43:46+00:00Added an answer on June 1, 2026 at 1:43 am

    The bold statement really does mean what you think it does.

    The idea would be to find the fullest bin the current object will fit into, hence minimising the amount of wasted space. If the object doesn’t fit in any bins then a new bin needs to be created

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a problem I ran into recently. I have attributes strings of the form
Here is my SQL SELECT items.name, items.id, items.price, COUNT(cart_items.itemId) AS quantity FROM `cart_items` LEFT
Firstly here is the problem: A positive integer is called a palindrome if its
Here's what I have so far: var bestReason = from p in successfulReasons group
Everyday I struggle with algorithm questions and try to ask here which I can't
Excuse my limited knoweldge here. In the past I have used Steve Sanderson's method
Please excuse me if I'm simple here, I want to create a simple widget
(Excuse any ignorance of mine here - I'm not a seasoned Oracle user.) I'm
Here's one I have always wondered about... Please excuse my naivety, but - How
Here's the scenario: I'm an SSIS virgin, but I found an excuse to start

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.