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:
Java Program to Implement Pagoda
Hướng dẫn Java Design Pattern – Iterator
Spring @RequestParam Annotation
Java Program to Perform Uniform Binary Search
RestTemplate Post Request with JSON
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Spring Boot - Interceptor
Setting Up Swagger 2 with a Spring REST API
Giới thiệu luồng vào ra (I/O) trong Java
The Basics of Java Security
Introduction to Spliterator in Java
Sort a HashMap in Java
Introduction to Spring Data JPA
Chuyển đổi từ HashMap sang ArrayList
Java Program to Implement Floyd Cycle Algorithm
Spring Boot - Web Socket
Java Program to Implement IdentityHashMap API
HashMap trong Java hoạt động như thế nào?
Java Program to Implement Insertion Sort
Java Program to Implement Pollard Rho Algorithm
Working with Tree Model Nodes in Jackson
Một số nguyên tắc, định luật trong lập trình
@DynamicUpdate with Spring Data JPA
Java NIO2 Path API
Collect a Java Stream to an Immutable Collection
Java Program to Implement Network Flow Problem
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Using Custom Banners in Spring Boot
Get the workstation name or IP
Spring WebClient Requests with Parameters
Spring Data JPA and Null Parameters
Java Program to Implement Gauss Jordan Elimination