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.

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.

advertisement
  1. #include<stdio.h>
  2. void binPacking(int *a, int size, int n) {
  3.     int binCount = 1, i;
  4.     int s = size;
  5.     for (i = 0; i < n; i++) {
  6.         if (s - *(a + i) > 0) {
  7.             s -= *(a + i);
  8.             continue;
  9.         } else {
  10.             binCount++;
  11.             s = size;
  12.             i--;
  13.         }
  14.     }
  15.  
  16.     printf("Number of bins required: %d", binCount);
  17. }
  18.  
  19. int main(int argc, char **argv) {
  20.     printf("Enter the number of items in Set: ");
  21.     int n;    
  22.     int a[n], i;
  23.     int size;
  24.     scanf("%d", &n);
  25.     printf("Enter %d items:", n);
  26.  
  27.     for (i = 0; i < n; i++)
  28.         scanf("%d", &a[i]);
  29.     printf("Enter the bin size: ");
  30.     scanf("%d", &size);
  31.     binPacking(a, size, n);
  32.     return 0;
  33. }

Output:

$ gcc BinPacking.c
$ ./a.out
 
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

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.