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:
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Spring MVC Async vs Spring WebFlux
Java Program to Use rand and srand Functions
Java Program to Check whether Directed Graph is Connected using DFS
Java Program to implement Bit Matrix
Logging a Reactive Sequence
Comparing Objects in Java
Java Program to Implement Hash Tables with Quadratic Probing
Java Program to Find Number of Articulation points in a Graph
Java Program to Implement Quick Sort Using Randomization
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Java Program to Implement Caesar Cypher
Java Program to Implement Sieve Of Atkin
Spring REST with a Zuul Proxy
Copy a List to Another List in Java
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Assert an Exception is Thrown in JUnit 4 and 5
Java Program to Perform Search in a BST
Guide to DelayQueue
Java Program to Find a Good Feedback Vertex Set
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Intro to Inversion of Control and Dependency Injection with Spring
Java Program to Implement Solovay Strassen Primality Test Algorithm
Jackson Date
Java Program to Find Transpose of a Graph Matrix
Java Program to Implement Splay Tree
Extract network card address
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Spring Boot Tutorial – Bootstrap a Simple Application
Java – Try with Resources
Java Program to Implement Stack API
Testing in Spring Boot