Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins

«
»
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.

  1. //This is a java program to implement first fit decreasing for 1D objects using M bins
  2. import java.util.Scanner;
  3.  
  4. public class First_Fit_Decreasing_Bin_Packing
  5. {
  6. 	public static void main(String args[])
  7. 	{
  8. 		System.out
  9. 				.println("BIN - PACKING Algorithm 1D Objects(First Fit Decreasing)");
  10. 		System.out.println("Enter the number of items in Set: ");
  11. 		Scanner sc = new Scanner(System.in);
  12. 		int n = sc.nextInt();
  13. 		System.out.println("Enter " + n + " items:");
  14. 		int[] a = new int[n];
  15. 		for (int i = 0; i < n; i++)
  16. 			a[i] = sc.nextInt();
  17. 		System.out.println("Enter the bin size: ");
  18. 		int size = sc.nextInt();
  19. 		int[] sequence = sort(a);
  20. 		binPacking(sequence, size, n);
  21. 		sc.close();
  22. 	}
  23.  
  24. 	public static void binPacking(int[] a, int size, int n)
  25. 	{
  26. 		int binCount = 0;
  27. 		int[] binValues = new int[n];
  28. 		for (int i = 0; i < binValues.length; i++)
  29. 			binValues[i] = size;
  30.  
  31. 		for (int i = 0; i < n; i++)
  32. 			for (int j = 0; j < binValues.length; j++)
  33. 			{
  34. 				if (binValues[j] - a[i] >= 0)
  35. 				{
  36. 					binValues[j] -= a[i];
  37. 					break;
  38. 				}
  39. 			}
  40.  
  41. 		for (int i = 0; i < binValues.length; i++)
  42. 			if (binValues[i] != size)
  43. 				binCount++;
  44.  
  45. 		System.out
  46. 				.println("Number of bins required using first fit decreasing algorithm is:"
  47. 						+ binCount);
  48. 	}
  49.  
  50. 	static int[] sort(int[] sequence)
  51. 	{
  52. 		// Bubble Sort descending order
  53. 		for (int i = 0; i < sequence.length; i++)
  54. 			for (int j = 0; j < sequence.length - 1; j++)
  55. 				if (sequence[j] < sequence[j + 1])
  56. 				{
  57. 					sequence[j] = sequence[j] + sequence[j + 1];
  58. 					sequence[j + 1] = sequence[j] - sequence[j + 1];
  59. 					sequence[j] = sequence[j] - sequence[j + 1];
  60. 				}
  61. 		return sequence;
  62. 	}
  63. }

Output:

advertisement
$ 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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.