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 Configuration with Jasypt
Multipart Upload with HttpClient 4
Java Program to Implement Gale Shapley Algorithm
Java Program to Perform Searching Based on Locality of Reference
Initialize a HashMap in Java
Java Program to Check the Connectivity of Graph Using DFS
Hướng dẫn Java Design Pattern – State
Convert String to Byte Array and Reverse in Java
Using a Mutex Object in Java
Understanding Memory Leaks in Java
Interface trong Java 8 – Default method và Static method
Java Program to Implement Max-Flow Min-Cut Theorem
Java Program to Implement Quick sort
Spring Boot - Apache Kafka
Guava CharMatcher
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
The Java 8 Stream API Tutorial
Lập trình hướng đối tượng (OOPs) trong java
Spring Security OAuth Login with WebFlux
Spring 5 Functional Bean Registration
OAuth 2.0 Resource Server With Spring Security 5
Java – Rename or Move a File
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Different Ways to Capture Java Heap Dumps
Java Program to Represent Graph Using Incidence Matrix
Introduction to Thread Pools in Java
Java – Get Random Item/Element From a List
Introduction to Spring Security Expressions
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Luồng Daemon (Daemon Thread) trong Java
Java Program to Perform Sorting Using B-Tree
Setting Up Swagger 2 with a Spring REST API