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:
A Guide to LinkedHashMap in Java
Java Program to Implement Min Heap
Java Program to Implement ArrayDeque API
Java Program to Find a Good Feedback Vertex Set
Spring Cloud AWS – Messaging Support
Java Program to Compute Determinant of a Matrix
Java Program to Perform Partial Key Search in a K-D Tree
@DynamicUpdate with Spring Data JPA
Removing all Nulls from a List in Java
StringBuilder vs StringBuffer in Java
Cơ chế Upcasting và Downcasting trong java
Guide to java.util.concurrent.BlockingQueue
Spring Boot - Google OAuth2 Sign-In
Spring Security Basic Authentication
Java Program to Implement Segment Tree
Spring Boot - Service Components
Biến trong java
Spring Boot - Twilio
Các nguyên lý thiết kế hướng đối tượng – SOLID
Spring Boot: Customize the Jackson ObjectMapper
Java Program to Implement Pagoda
Spring Webflux with Kotlin
Java Program to Implement Network Flow Problem
Compact Strings in Java 9
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Serialize Only Fields that meet a Custom Criteria with Jackson
Java Program to Find Nearest Neighbor for Dynamic Data Set
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Java Program to Solve any Linear Equations
Java Program to Implement Radix Sort
Comparing Objects in Java
Lớp Collectors trong Java 8