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:
Ép kiểu trong Java (Type casting)
Java Program to Solve any Linear Equations
Hướng dẫn Java Design Pattern – DAO
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Java Program to Implement Stack using Linked List
Comparing Strings in Java
The Registration API becomes RESTful
Java Program to Implement Selection Sort
Dynamic Proxies in Java
Reversing a Linked List in Java
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Hướng dẫn Java Design Pattern – Abstract Factory
The XOR Operator in Java
Serialization và Deserialization trong java
How to Count Duplicate Elements in Arraylist
Generic Constructors in Java
Java Program to Implement Hash Tables
Java Program to Implement D-ary-Heap
Java Program to Represent Graph Using Adjacency List
Lớp TreeMap trong Java
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Introduction to Spring Data MongoDB
HandlerAdapters in Spring MVC
Java Program to Solve Knapsack Problem Using Dynamic Programming
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Validate email address exists or not by Java Code
Custom Thread Pools In Java 8 Parallel Streams
Java – Reader to InputStream
Java Program to Implement Multi-Threaded Version of Binary Search Tree
The Modulo Operator in Java
Java Program to Implement RenderingHints API
Injecting Prototype Beans into a Singleton Instance in Spring