Professional Documents
Culture Documents
Assignment On Introduction To Algorithm: Submitted by
Assignment On Introduction To Algorithm: Submitted by
Assignment On Introduction To Algorithm: Submitted by
Submitted by:
Name: Md. Rakib Jabed
ID: 1505038
Session: 2015-16
Dept. of CSE, BRUR
Submitted to:
Name: Md. Abul Kalam Azad
Assistant Professor
Dept. of CSE, BRUR
Submitted on
Classic Bin Packing Problem
Bin Packing Problem:
The bin packing problem is considered to be one of the combinatorial minimization problems.
We receive a sequence of n items L= {1, 2,…, n} with sizes s1, s2, . . . , sn and each item has a
fixed size in (0, 1]. Now one needs to find a partition of the items into sets of size 1 (called bins)
so that the number of sets in the partition is minimized and the sum of the sizes of the pieces
assigned to any bin may not exceed its capacity. We say that an item that belongs to a given bin
(set) is packed into this bin. A bin is empty if no item is packed into it, otherwise it is used. Since
the goal is to minimize the number of bins used. Bin packing is NP -hard, thus finding an exact
solution for any given input can be done currently only in exponential time. Since it`s is an NP-
hard problem and the polynomial time optimization algorithm cannot be found unless P=NP. A
more reasonable approach would be finding an approximation algorithm m that runs in low-order
polynomial time and for all instances I, m APPROX (I) is close to m OPT(I). m APPROX (I)
represents the approximate solution for the given instance I. m OPT(I) represents the optimal
solution for the instance I. m OPT(I) can also be represented as m*(I) which means the same.
These problems are optimization problems with many practical applications related to real life as
stock cutting, scheduling tasks, layouts on computer chips, packing goods into crates, assigning
commercials to station breaks on television, loading trucks with a given size or weight limit and
storage problems. The list is endless and extremely diverse.
Our primary goal is to fit items into the bins such that the number of bins used is minimal. For
this purpose, there are several algorithms developed which provides an approximate solution
bounded by ‘r’ (here ‘r’ is the approximation ratio). These algorithms can be broadly classified
into two categories
Online algorithm:
These algorithms are for bin packing problems where items arrive one at a time in unknown
order), each must be put in a bin, before considering the next item.
0.5 0.9
0.2
0.1
0.6 0.7
0.4
0.1
0.3
0.2
Total bin = 5
Capacity-1
0.5 0.1
0.2
Total bin=5
Best fit also can be implemented in O (n log n) time, by using a balanced binary tree storing bins
ordered by remaining capacity.
If M is the optimal number of bins, then Best Fit never uses more than 1.7M bins. So Best Fit is
same as First Fit and better than Next Fit in terms of upper bound on number of bins.
Capacity-1
0.5 0.1
0.2
0.7
0.2
0.5
0.5
0.4
0.6
Total bin=5
0.2 0.8
0.3
0.7 0.5
0.1
0.4
Total bin=4
0.4 0.5
0.1
0.2
0.7 0.2
0.6
0.5
0.5
Total bin =4
Capacity-1
0.4 0.5
0.1
0.2
0.7 0.2
0.6
0.5
0.5
Total bin =4
The decision problem: do all of the objects fit into the bins? there is enough free space to hold
the objects or not?
The optimization problem: minimizing the number of bins wasted or the amount of wasted bin
capacity.
In this category of bin packing problem the aim is packing different sized objects (most
commonly rectangles) into fixed sized, two-dimensional bins, using as few of the bins as
possible. In the two-dimensional version, both sides vary. This problem may be thought of as
placing rectangles on a flat surface. The classic video game Tetris relates to the problem of
rectangle bin-packing.
The objective is to minimize the sum of the capacities of the bins used.
There is another version of this problem where all information of the items are known. But we
cannot preview the types of the bins before packing. Also in each bin the size is not less than the
size of the largest item.
Knapsack problem:
Given a number of items, we are required to select a subset to carry it in a fixed capacity of a bin
(knapsack) items differ by their value. The aim is to load items which maximize the overall
reward without exceeding the capacity.
Ideas- first sort the data for each of the current maximum value can be found the greatest degree
of matching, if not found, their equipment into a backpack.
#include <iostream>
#include<algorithm>
int a[100005];
int main()
cin >> t;
while (t--)
int a[100001];
sum = 0;
sort(a, a + n);
if (a[i] == 0) continue;
if (a[j] == 0) continue;
a[j] = 0;
break;
a[i] = 0;
sum++;
return 0;
}
Sample input:
1
10
80
70 15 30 35 10 80 20 35 10 30
Sample output:
6