This is a Java Program to implement First fit Decreasing Bin Packing algorithm. The First Fit Decreasing (FFD) strategy, operates by first sorting the items to be inserted in decreasing order by their sizes, and then inserting each item into the first bin in the list with sufficient remaining space.
Here is the source code of the Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
//This is a java program to implement first fit decreasing for 1D objects using M bins
import java.util.Scanner;
public class First_Fit_Decreasing_Bin_Packing
{
public static void main(String args[])
{
System.out
.println("BIN - PACKING Algorithm 1D Objects(First Fit Decreasing)");
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();
int[] sequence = sort(a);
binPacking(sequence, size, n);
sc.close();
}
public static void binPacking(int[] a, int size, int n)
{
int binCount = 0;
int[] binValues = new int[n];
for (int i = 0; i < binValues.length; i++)
binValues[i] = size;
for (int i = 0; i < n; i++)
for (int j = 0; j < binValues.length; j++)
{
if (binValues[j] - a[i] >= 0)
{
binValues[j] -= a[i];
break;
}
}
for (int i = 0; i < binValues.length; i++)
if (binValues[i] != size)
binCount++;
System.out
.println("Number of bins required using first fit decreasing algorithm is:"
+ binCount);
}
static int[] sort(int[] sequence)
{
// Bubble Sort descending order
for (int i = 0; i < sequence.length; i++)
for (int j = 0; j < sequence.length - 1; j++)
if (sequence[j] < sequence[j + 1])
{
sequence[j] = sequence[j] + sequence[j + 1];
sequence[j + 1] = sequence[j] - sequence[j + 1];
sequence[j] = sequence[j] - sequence[j + 1];
}
return sequence;
}
}
Output:
$ javac First_Fit_Decreasing_Bin_Packing.java $ java First_Fit_Decreasing_Bin_Packing BIN - PACKING Algorithm for 1D Objects(First Fit Decreasing) Enter the number of items in Set: 9 Enter 9 items: 4 1 2 5 3 2 3 6 3 Enter the bin size: 6 Number of bins required using first fit decreasing algorithm is:5
Related posts:
Jackson – Unmarshall to Collection/Array
Spring Boot - OAuth2 with JWT
Lập trình đa luồng với CompletableFuture trong Java 8
Custom Thread Pools In Java 8 Parallel Streams
Java Copy Constructor
Java Program to Compute DFT Coefficients Directly
Convert Character Array to String in Java
Spring Boot - File Handling
The Registration API becomes RESTful
Posting with HttpClient
Primitive Type Streams in Java 8
Java Program to Describe the Representation of Graph using Incidence Matrix
Binary Numbers in Java
Java Program to Perform Stooge Sort
Hướng dẫn Java Design Pattern – Service Locator
Java Program to Implement Treap
Các nguyên lý thiết kế hướng đối tượng – SOLID
Java Program to Implement Hash Tree
Java Program to Perform Finite State Automaton based Search
Java Program to Implement the MD5 Algorithm
Spring Boot - Database Handling
Lập trình đa luồng trong Java (Java Multi-threading)
HttpClient Basic Authentication
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Java Program to Check whether Undirected Graph is Connected using DFS
Java Program to Implement SynchronosQueue API
Spring @RequestMapping New Shortcut Annotations
REST Pagination in Spring
Java Program to Implement Naor-Reingold Pseudo Random Function
Java Program to Implement LinkedBlockingDeque API
Java Program to Implement Adjacency Matrix
Java 8 – Powerful Comparison with Lambdas