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.
//This is a sample program to illustrate the Bin-Packing algorithm using next fit heuristics
import java.util.Scanner;
public class Bin_Packing_Algorithm
{
public static void binPacking(int []a, int size, int n)
{
int binCount=1;
int s = size;
for(int i=0; i<n; i++)
{
if(s - a[i] > 0)
{
s -= a[i];
continue;
}
else
{
binCount++;
s = size;
i--;
}
}
System.out.println("Number of bins required: "+binCount);
}
public static void main(String args[])
{
System.out.println("BIN - PACKING Algorithm");
System.out.println("Enter the number of items in Set: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println("Enter "+n+" items:");
int []a = new int[n];
for(int i=0; i<n; i++)
a[i] = sc.nextInt();
System.out.println("Enter the bin size: ");
int size = sc.nextInt();
binPacking(a, size, n);
sc.close();
}
}
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
Related posts:
Spring’s RequestBody and ResponseBody Annotations
Converting a List to String in Java
Debugging Reactive Streams in Java
Java Program to Implement Hash Tables with Linear Probing
Java Program to Implement Threaded Binary Tree
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Java Program to Implement Rolling Hash
Java Program to Implement Adjacency Matrix
Stack Memory and Heap Space in Java
Java Program to Implement Efficient O(log n) Fibonacci generator
Java Program to Implement Min Hash
Apache Commons Collections SetUtils
Spring Security with Maven
Autoboxing và Unboxing trong Java
Lớp HashMap trong Java
Hướng dẫn Java Design Pattern – Interpreter
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Spring Security 5 for Reactive Applications
How to Change the Default Port in Spring Boot
Java Program to Implement AttributeList API
Java Program to implement Priority Queue
Java Program to Perform Arithmetic Operations on Numbers of Size
Java Program to Implement Treap
Converting Between an Array and a Set in Java
Deploy a Spring Boot WAR into a Tomcat Server
A Guide to the Java ExecutorService
Guide to java.util.concurrent.Locks
Java Program to Implement EnumMap API
The HttpMediaTypeNotAcceptableException in Spring MVC
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
A Guide to JPA with Spring
Java Program to Implement Heap