Assignment On Introduction To Algorithm: Submitted by

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Assignment on Introduction to Algorithm

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 bin packing Algorithms

• Offline Bin packing Algorithms

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.

1.Next fit algorithm:


This algorithm is one of the most basic online algorithms. It requires only O(n) time and O(1)
extra space to process n times. The number of bins used by this algorithm is bounded by twice of
optimal. Consider two adjacent bins. The sum of items in these two bins must be>c; otherwise,
next fit would have put all the items of second bin into the first. The same holds for all other
bins. Thus, at most half the space is wasted. And so next fit uses at most 2M bins if M is optimal.
When processing next item, check if it fits in the same bin as the last item. Use a new bin only if
it does not.
Example for next fit algorithm-
Example items- 0.3, 0.6, 0.2, 0.1, 0.5, 0.7, 0.2, 0.4, 0.1, 0.9
Bin capacity- 1

0.5 0.9
0.2
0.1
0.6 0.7

0.4
0.1
0.3
0.2

Total bin = 5

2.First fit algorithm:


First fit algorithm is another approach of bin packing problem. It requires O(n2) time. But first fit
can be implemented in O( n log n) using self-balancing binary search tree. If M is the optimal
number of bins, then first fit never uses more than 1.7 bins. So first fit is better than next fit
algorithm.
When processing the next item, scan the previous bins in order and place the item in the first bin
that fits. Start a new bin only if it does not fit in any of the existing bins.

Example of first fit algorithm:


Items- 0.5 0.7 0.5 0.2 0.4 0.2 0.5 0.1 0.6

Capacity-1

0.5 0.1

0.2

0.7 0.2 0.6


0.5
0.4 0.5

Total bin=5

3.Best fit algorithm:


In this algorithm new item is placed in a bin where it fits the tightest. If it does not fit in any bin,
then start a new bin. The idea is to places the next item in the *tightest* spot. That is, put it in the
bin so that the smallest empty space is left.

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.

Example of best fit algorithm:


Items- 0.5 0.7 0.5 0.2 0.4 0.2 0.5 0.1 0.6

Capacity-1
0.5 0.1

0.2
0.7

0.2
0.5
0.5

0.4

0.6

Total bin=5

4.Worst fit algorithm:


Worst Fit packs the input item according to the following rule: while trying to pack item ai, the
worst fit algorithm assigns the item to the bin whose empty space is maximum. If the item ai is
unable to fit in any of the opened bins, then a new bin is opened to pack that item ai.
Example of worst fit:
Items- 0.4 0.7 0.1 0.3 0.8 0.2 0.5
Capacity-1

0.2 0.8

0.3

0.7 0.5

0.1

0.4

Total bin=4

Offline Bin Packing algorithms:


Major problems with all online algorithms is that it is hard to pack large items, especially when
they occur late in the input.
In offline algorithms where we know the sizes of all items in advance, a natural solution is to
first sort item by size, from largest to smallest, and then apply the following heuristics:
• First fit decreasing
• Best fit decreasing
First fit decreasing algorithm:
First fit decreasing is an enhanced algorithm with improved performance ratio and better
approximation. In fact it is an offline algorithm where the items are first sorted in non-increasing
order as per their size and then processes items as first fit.

Example of FFD algorithm:


Items- 0.7 0.6 0.5 0.5 0.5 0.4 0.2 0.2 0.1
Capacity- 1

0.4 0.5
0.1

0.2

0.7 0.2

0.6

0.5

0.5

Total bin =4

Best fit decreasing algorithm:


In this approach we need to sort the items by size. Then need to run best fit algorithm.

Example of best fit decreasing:


Items- 0.7 0.6 0.5 0.5 0.5 0.4 0.2 0.2 0.1

Capacity-1
0.4 0.5
0.1

0.2

0.7 0.2

0.6

0.5

0.5

Total bin =4

Variants of bin packing problem:


Dyckhoff was the first who proposed a classification of these problems and gives a structure to
organize this group of problems. This classification is based on four criteria: the first is the
dimensionality: it’s used to identify the size of the problem: one, two, three dimensional
problems. the second criterion provides information on the type of assignment: whether you want
to place all the items into a number of bins or you have a fixed number of bins and you have to
make an optimal selection from the small items.
the third criterion is the assortment of objects(bins): is there only one object, or are there several
of the same size and shape or are there different. The last criterion is the assortment of small
items: are they different or identical?

One dimension Bin packing problem:


In one dimensional bin-packing problem, there is a finite collection of objects which is packed
into one or more bins without exceeding the capacity. Each object in one dimensional bin-
packing had fixed width and variable height or variable width and fixed height.

This problem may be thought as either a decision problem or an optimization problem.

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.

The two-dimension Bin packing problem:

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.

Variable sized bin packing problem:


In this version of the problem the bins do not all have the same capacity .There are an infinite
number of bins of each capacity; and each piece must be assigned in turn, without knowledge of
the next pieces.

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.

Cutting stock problem:


Cutting Stock Problem is a problem of to determine how to cut the desired product from
unlimited or limited pieces of stock of various lengths. The materials can be paper, textiles,
cellophane, or metallic foil. The stocks of large widths of those materials are called raws. Later,
they will be cut into smaller rolls which are called finals. The finals have to be at least equal to
the demand and must satisfy all of the constraints such as, number of knives, different raws,
limited raws etc. In order to find to most economical way of cutting the existing raws into the
desired finals, the cutting patterns are generated for each raw.
The optimal solution is to find the number of the patterns used to cut the raws into finals that can
satisfy all of the demands and the constraints such that raws used are minimized.

Bin packing uva problem:


A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the
same length l and each item i has length li ≤ l. We look for a minimal number of bins q such that
• each bin contains at most 2 items,
• each item is packed in one of the q bins,
• the sum of the lengths of the items packed in a bin does not exceed l.
You are requested, given the integer values n, l, l1, . . . , ln, to compute the optimal number of
bins q.

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>

using namespace std;

int a[100005];

int main()

int t, n, max, sum;

cin >> t;

while (t--)

int a[100001];

sum = 0;

cin >> n >> max;

for (int i = 0; i < n; i++) cin >> a[i];

sort(a, a + n);

for (int i = n - 1; i >= 0; i--)

if (a[i] == 0) continue;

for (int j = i - 1; j >= 0; j--)

if (a[j] == 0) continue;

if (a[i] + a[j] <= max)

a[j] = 0;

break;

a[i] = 0;

sum++;

cout << sum << endl;

if (t) cout << endl;

return 0;

}
Sample input:
1
10
80
70 15 30 35 10 80 20 35 10 30
Sample output:
6

You might also like