Users Online

· Guests Online: 107

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C++ Program to Implement the Bin Packing Algorithm

C++ Program to Implement the Bin Packing Algorithm

 

 

This is a C++ Program to implement Bin packing algorithm. This is a sample program to illustrate the Bin-Packing algorithm using next fit heuristics. In the bin packing problem, objects of different volumes must be packed into a finite number of bins or containers each of volume V in a way that minimizes the number of bins used. In computational complexity theory, it is a combinatorial NP-hard problem.

 

There are many variations of this problem, such as 2D packing, linear packing, packing by weight, packing by cost, and so on. They have many applications, such as filling up containers, loading trucks with weight capacity constraints, creating file backups in media and technology mapping in Field-programmable gate array semiconductor chip design.

The bin packing problem can also be seen as a special case of the cutting stock problem. When the number of bins is restricted to 1 and each item is characterised by both a volume and a value, the problem of maximising the value of items that can fit in the bin is known as the knapsack problem.

Here is source code of the C++ Program to Implement the Bin Packing Algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. void binPacking(int *a, int size, int n)
  6. {
  7.     int binCount = 1;
  8.     int s = size;
  9.     for (int i = 0; i < n; i++)
  10.     {
  11.         if (s - *(a + i) > 0)
  12.         {
  13.             s -= *(a + i);
  14.             continue;
  15.         }
  16.         else
  17.         {
  18.             binCount++;
  19.             s = size;
  20.             i--;
  21.         }
  22.     }
  23.  
  24.     cout << "Number of bins required: " << binCount;
  25. }
  26.  
  27. int main(int argc, char **argv)
  28. {
  29.     cout << "BIN - PACKING Algorithm\n";
  30.     cout << "Enter the number of items in Set: ";
  31.     int n;
  32.     cin >> n;
  33.     cout << "Enter " << n << " items:";
  34.     int a[n];
  35.     for (int i = 0; i < n; i++)
  36.         cin >> a[i];
  37.     cout << "Enter the bin size: ";
  38.     int size;
  39.     cin >> size;
  40.     binPacking(a, size, n);
  41. }

Output:

$ g++ BinPacking.cpp
$ a.out
 
BIN - PACKING Algorithm
Enter the number of items in Set: 5
Enter 5 items:12 23 34 45 56
Enter the bin size: 70
Number of bins required: 3

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.81 seconds
10,267,339 unique visits