Java Program to Implement the Bin Packing Algorithm

This is the java implementation of classic Bin-Packing algorithm. 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.

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

  1. //This is a sample program to illustrate the Bin-Packing algorithm using next fit heuristics
  2. import java.util.Scanner;
  3.  
  4. public class Bin_Packing_Algorithm 
  5. {
  6.     public static void binPacking(int []a, int size, int n)
  7.     {
  8.         int binCount=1;
  9.         int s = size;
  10.         for(int i=0; i<n; i++)
  11.         {
  12.             if(s - a[i] > 0)
  13.             {
  14.                 s -= a[i];
  15.                 continue;
  16.             }
  17.             else
  18.             {
  19.                 binCount++;
  20.                 s = size;
  21.                 i--;
  22.             }
  23.         }
  24.  
  25.         System.out.println("Number of bins required: "+binCount);
  26.     }
  27.  
  28.     public static void main(String args[])
  29.     {
  30.         System.out.println("BIN - PACKING Algorithm");
  31.         System.out.println("Enter the number of items in Set: ");
  32.         Scanner sc = new Scanner(System.in);
  33.         int n = sc.nextInt();
  34.         System.out.println("Enter "+n+" items:");
  35.         int []a = new int[n];
  36.         for(int i=0; i<n; i++)
  37.             a[i] = sc.nextInt();
  38.         System.out.println("Enter the bin size: ");
  39.         int size = sc.nextInt();
  40.         binPacking(a, size, n);
  41.         sc.close();
  42.     }
  43. }

Output:

$ javac Bin_Packing_Algorithm.java
$ java Bin_Packing_Algorithm
BIN - PACKING Algorithm
Enter the number of items in Set: 
8
Enter 8 items:
4 5 8 3 4 5 1 6
Enter the bin size: 
10
 
Number of bins required: 5

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java 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 & discussions at Telegram SanfoundryClasses.