- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Bin Packing Problem (Minimize number of used Bins) in C++?

In case of given m elements of different weights and bins each of capacity C, assign each element to a bin so that number of total implemented bins is minimized. Assumption should be that all elements have weights less than bin capacity.

## Applications

Placing data on multiple disks.

Loading of containers like trucks.

Packing advertisements in fixed length radio/TV station breaks.

Job scheduling.

## Example

Input: weight[] = {4, 1, 8, 1, 4, 2} Bin Capacity c = 10 Output: 2 We require at least 2 bins to accommodate all elements First bin consists {4, 4, 2} and second bin {8, 2}

## Lower Bound

We can always calculate a lower bound on minimum number of bins required using ceil() function. The lower bound can be given below −

Bins with minimum number >= ceil ((Total Weight) / (Bin Capacity))

In the above example, lower bound for first example is “ceil(4 + 1 + 8 + 2 + 4 + 1)/10” = 2

The following approximate algorithms are implemented for this problem.

## Online Algorithms

These algorithms are implemented for Bin Packing problems where elements arrive one at a time (in unknown order), each must be put in a bin, before considering the next element.

Next Fit −

When processing next element, verify if it fits in the same bin as the last element. Implement a new bin only if it does not.

## Following is C++ application for this algorithm.

// C++ program to calculate number of bins required implementing next fit algorithm. #include <bits/stdc++.h> using namespace std; // We have to return number of bins needed implementing next fit online algorithm int nextFit(int weight1[], int m, int C){ // Result (Count of bins) and remaining capacity in current bin are initialized. int res = 0, bin_rem = C; // We have to place elements one by one for (int i = 0; i < m; i++) { // If this element can't fit in current bin if (weight1[i] > bin_rem) { res++; // Use a new bin bin_rem = C - weight1[i]; } else bin_rem -= weight1[i]; } return res; } // Driver program int main(){ int weight1[] = { 3, 6, 5, 8, 2, 4, 9 }; int C = 10; int m = sizeof(weight1) / sizeof(weight1[0]); cout<< "Number of bins required in Next Fit : " <<nextFit(weight1, m, C); return 0; }

## Output

Number of bins required in Next Fit : 4

Next Fit is treated as a simple algorithm. It needs only O(m) time and O(1) extra space to process m elements.

First Fit−

When processing the next element, examine or scan the previous bins in order and place the element in the first bin that fits.If element does not fit in any of the existing bins at that time we start a new bin only.

## Example

// C++ program to find number of bins needed implementing First Fit algorithm. #include <bits/stdc++.h> using namespace std; // We have to return number of bins needed implementing first fit online algorithm int firstFit(int weight1[], int m, int C){ // We have to initialize result (Count of bins) int res = 0; // We have to create an array to store remaining space in bins there can be maximum n bins int bin_rem[m]; // We have to place elements one by one for (int i = 0; i < m; i++) { // We have to find the first bin that can accommodate weight1[i] int j; for (j = 0; j < res; j++) { if (bin_rem[j] >= weight1[i]) { bin_rem[j] = bin_rem[j] - weight1[i]; break; } } // If no bin could accommodate weight1[i] if (j == res) { bin_rem[res] = C - weight1[i]; res++; } } return res; } // Driver program int main(){ int weight1[] = { 2, 5, 4, 7, 1, 3, 8 }; int C = 10; int m = sizeof(weight1) / sizeof(weight1[0]); cout<< "Number of bins required in First Fit : " <<firstFit(weight1, m, C); return 0; }

## Output

Number of bins required in First Fit : 4

The above implementation of First Fit consumes O(m2) time, but First Fit can be used in O(m log m) time implementing Self-Balancing Binary Search Trees.

Best Fit −

The concept is to places the next item in the tightest position. That means put it in the bin so that at least emptyspace is left.

## Example

// C++ program to calculate number of bins required implementing Best fit algorithm. #include <bits/stdc++.h> using namespace std; // We have to returns number of bins required implementing best fit online algorithm int bestFit(int weight1[], int m, int C){ // We have to initialize result (Count of bins) int res = 0; // We have to create an array to store remaining space in bins there can be maximum n bins int bin_rem[m]; // Place elements one by one for (int i = 0; i < m; i++){ // Find the best bin that can accomodate weight1[i] int j; // We have to initialize minimum space left and index of best bin int min = C + 1, bi = 0; for (j = 0; j < res; j++){ if (bin_rem[j] >= weight1[i] && bin_rem[j] - weight1[i] < min) { bi = j; min = bin_rem[j] - weight1[i]; } } // We create a new bin,if no bin could accommodate weight1[i], if (min == C + 1) { bin_rem[res] = C - weight1[i]; res++; } else // Assign the element to best bin bin_rem[bi] -= weight1[i]; } return res; } // Driver program int main(){ int weight1[] = { 2, 5, 4, 7, 1, 3, 8 }; int C = 10; int m = sizeof(weight1) / sizeof(weight1[0]); cout<< "Number of bins required in Best Fit : " <<bestFit(weight1, m, C); return 0; }

## Output

Number of bins required in Best Fit : 4

Best Fit can also be applied in O(m log m) time implementing Self-Balancing Binary Search Trees.

## Offline Algorithms

In case of the offline version, we have all elements upfront.A problem with online algorithms is that packing large elements is difficult, especially if they occur late in the sequence. We can overcome this by sorting the input sequence, and placing the large elements first.

First Fit Decreasing −

## Example

// C++ program to locate number of bins needed implementing First Fit Decreasing algorithm. #include <bits/stdc++.h> using namespace std; /* We copy firstFit() from above */ int firstFit(int weight1[], int m, int C){ // We have to initialize result (Count of bins) int res = 0; // We have to create an array to store remaining space in bins there can be maximum n bins int bin_rem[m]; // We have to place elements one by one for (int i = 0; i < m; i++) { // We have to find the first bin that can accommodate weight1[i] int j; for (j = 0; j < res; j++) { if (bin_rem[j] >= weight1[i]) { bin_rem[j] = bin_rem[j] - weight1[i]; break; } } // If no bin could accommodate weight1[i] if (j == res) { bin_rem[res] = C - weight1[i]; res++; } } return res; } // We have to returns number of bins required implementing first fit decreasing offline algorithm int firstFitDec(int weight1[], int m, int C){ // At first we sort all weights in decreasing order sort(weight1, weight1 + m, std::greater<int>()); // Now we call first fit for sorted items return firstFit(weight1, m, C); } // Driver program int main(){ int weight1[] = { 2, 5, 4, 7, 1, 3, 8 }; int C = 10; int m = sizeof(weight1) / sizeof(weight1[0]); cout<< "Number of bins required in First Fit " << "Decreasing : " << firstFitDec(weight1, m, C); return 0; }

## Output

Number of bins required in First Fit Decreasing : 3

First Fit decreasing generates the best result for the sample input as elements are sorted first.

First Fit Decreasing can also be used in O(m log m) time implementing Self-Balancing Binary Search Trees.

- Related Questions & Answers
- C++ Program to Implement the Bin Packing Algorithm
- Minimize the total number of teddies to be distributed in C++
- C Program for Number of stopping station problem
- Minimum Number of Jumps Problem
- Minimum Number of Platforms Problem
- bin() in Python
- Packing and Unpacking Arguments in Python?
- bin() in Python program
- What is bin and obj folder in C#?
- Minimize the sum of roots of a given polynomial in C++
- Python Program for Number of stopping station problem
- Partition Problem in C++
- Why should C++ programmers minimize use of 'new'?
- Minimize Rounding Error to Meet Target in C++
- Minimize Max Distance to Gas Station in C++