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:
Giới thiệu java.io.tmpdir
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Java Program to Solve Tower of Hanoi Problem using Stacks
A Custom Media Type for a Spring REST API
Login For a Spring Web App – Error Handling and Localization
Marker Interface trong Java
Receive email using POP3
Java Program to Implement Bellman-Ford Algorithm
Introduction to Spring Cloud OpenFeign
Tránh lỗi NullPointerException trong Java như thế nào?
Logging a Reactive Sequence
Java Program to Implement Disjoint Sets
Java Program to Implement Uniform-Cost Search
Java Program to Implement Control Table
Custom JUnit 4 Test Runners
Java Program to Implement Stack
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Java Program to Find Nearest Neighbor for Dynamic Data Set
Remove HTML tags from a file to extract only the TEXT
Composition, Aggregation, and Association in Java
Instance Profile Credentials using Spring Cloud
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Simplify the DAO with Spring and Java Generics
Servlet 3 Async Support with Spring MVC and Spring Security
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Check If Two Lists are Equal in Java
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Spring Boot - Exception Handling
Converting Java Date to OffsetDateTime
Rate Limiting in Spring Cloud Netflix Zuul
A Guide to EnumMap
Handling URL Encoded Form Data in Spring REST
 
