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:
Introduction to Spring Boot CLI
Xây dựng ứng dụng Client-Server với Socket trong Java
StringBuilder vs StringBuffer in Java
Difference Between Wait and Sleep in Java
ArrayList trong java
Hướng dẫn Java Design Pattern – Strategy
A Guide to EnumMap
Java Program to Implement Doubly Linked List
Java 8 Collectors toMap
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Java Program to Implement Triply Linked List
Java Program to Perform Searching Using Self-Organizing Lists
Using the Map.Entry Java Class
Spring Boot - Internationalization
Spring Data MongoDB Transactions
Semaphore trong Java
Guide to CopyOnWriteArrayList
Java Program to Implement ConcurrentSkipListMap API
Java Program to Implement Ternary Heap
Java Program to Emulate N Dice Roller
Guide to UUID in Java
Java Program to implement Bi Directional Map
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Getting Started with GraphQL and Spring Boot
Java Program to Implement Efficient O(log n) Fibonacci generator
Kết hợp Java Reflection và Java Annotations
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Java Program to Find a Good Feedback Edge Set in a Graph
Java Program to Implement SimpeBindings API
Java Program to Implement Extended Euclid Algorithm
Java Program to Implement Ternary Search Tree