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 Boot - Build Systems
Hướng dẫn Java Design Pattern – Proxy
Chuyển đổi Array sang ArrayList và ngược lại
Comparing Dates in Java
Java Program to Check for balanced parenthesis by using Stacks
Spring Boot - Quick Start
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Implement Min Heap
Working with Kotlin and JPA
Java Program to Implement Quick Sort with Given Complexity Constraint
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Getting Started with GraphQL and Spring Boot
Apache Commons Collections Bag
Interface trong Java 8 – Default method và Static method
Remove HTML tags from a file to extract only the TEXT
The StackOverflowError in Java
Spring Cloud Series – The Gateway Pattern
Send an email using the SMTP protocol
REST Web service: Upload và Download file với Jersey 2.x
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Java Program to Find the Mode in a Data Set
Cachable Static Assets with Spring MVC
Error Handling for REST with Spring
Immutable Objects in Java
Period and Duration in Java
Control the Session with Spring Security
Spring Boot - Apache Kafka
Java Program to Implement PrinterStateReasons API
Java Program to Construct an Expression Tree for an Postfix Expression
Java 8 Stream findFirst() vs. findAny()
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Map Interface trong java