
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement the Bin Packing Algorithm
The bin packing problem is a special type of cutting stock problem. In the bin packing problem, objects of different volumes must be packed into a finite number of containers or bins each of volume V in a way that minimizes the number of bins used. In computational complexity theory, it is a combinational NP-hard problem.
When the number of bins is restricted to 1 and each item is characterized by both a volume and a value, the problem of maximizing the value of items that can fit in the bin is known as the knapsack problem.
Algorithm
Begin Binpacking(pointer, size, no of sets) Declare bincount, m, i Initialize bincount = 1, m=size For i = 0 to number of sets if (m - *(a + i) > 0) do m = m - *(a + i) Continue Else Increase bincount m = size; Decrement i Print number of bins required End
Example Code
#include<iostream> using namespace std; void binPacking(int *a, int size, int n) { int binCount = 1; int m = size; for (int i = 0; i < n; i++) { if (m - *(a + i) > 0) { m -= *(a + i); continue; } else { binCount++; m = size; i--; } } cout << "Number of bins required: " << binCount; } int main(int argc, char **argv) { cout << "Enter the number of items in Set: "; int n; cin >> n; cout << "Enter " << n << " items:"; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; cout << "Enter the bin size: "; int size; cin >> size; binPacking(a, size, n); }
Output
Enter the number of items in Set: 3 Enter 3 items:4 6 7 Enter the bin size: 26 Number of bins required: 1
- Related Articles
- C++ Program to Implement the RSA Algorithm
- Bin Packing Problem (Minimize number of used Bins) in C++?
- C++ Program to Implement Kadane’s Algorithm
- C++ Program to Implement Johnson’s Algorithm
- C++ Program to Implement The Edmonds-Karp Algorithm
- C++ Program to Implement Coppersmith Freivald’s Algorithm
- C++ Program to Implement Modular Exponentiation Algorithm
- C++ Program to Implement Nearest Neighbour Algorithm
- C++ Program to Implement Expression Tree Algorithm
- C++ Program to Implement Interpolation Search Algorithm
- C++ Program to Implement Extended Euclidean Algorithm
- C program to implement Euclid’ s algorithm
- C++ Program to Implement Levenshtein Distance Computing Algorithm
- C++ Program to Implement Dijkstra’s Algorithm Using Set
- C++ Program to Implement Bitap Algorithm for String Matching

Advertisements