0% found this document useful (0 votes)
74 views15 pages

Lecture 28 - Money Counting and Bin Packing Problem

The document discusses two optimization problems: counting money and bin packing. For counting money, it describes a greedy algorithm that takes the largest bills/coins without overshooting the amount. This works optimally for US currency but not always. For bin packing, it presents three online algorithms (Next Fit, First Fit, Best Fit) and notes offline sorting approaches work better.

Uploaded by

Engineer Zain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views15 pages

Lecture 28 - Money Counting and Bin Packing Problem

The document discusses two optimization problems: counting money and bin packing. For counting money, it describes a greedy algorithm that takes the largest bills/coins without overshooting the amount. This works optimally for US currency but not always. For bin packing, it presents three online algorithms (Next Fit, First Fit, Best Fit) and notes offline sorting approaches work better.

Uploaded by

Engineer Zain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Greedy Algorithms:

Counting Money and Bin


Packing Problem

Instructor: Dr. Zuhair Zafar

CS-854 Advanced Algorithm Analysis

Lecture # 28
Counting Money Problem

2
Counting Money Problem
Suppose you want to count out a certain amount of money,
using the fewest possible bills and coins

A greedy algorithm that could do this would be:


At each step, take largest possible bill/coin that
does not overshoot

Example: To make $6.39, you can choose:


⚫ a $5 bill
⚫ a $1 bill, to make $6
⚫ a 25¢ coin, to make $6.25
⚫ A 10¢ coin, to make $6.35
⚫ four 1¢ coins, to make $6.39

For US money, the greedy algorithm always 3


gives optimum solution
Counting Money Problem
Greedy algorithm (C, N)
1. sort coins so C1  C2  . . .  Ck
2. S = ;
3. Change = 0
4. i=1 \\ Check for next coin
5. while Change  N do \\ all most valuable coins
6. if Change + Ci ≤ N then
7. Change = Change + Ci
8. S = S  {Ci}
9. else i = i+1
4
Counting Money Problem
In Pakistan, our currency notes are
C1 = 5000, C2 = 1000, C3 = 500, C4 = 100,
C5 = 50, C6 = 20 , C7 = 10

Applying above greedy algorithm to N = 13,660, we get


S = {C1, C1, C2, C2, C2, C3 , C4 , C5 , C7}

Does this algorithm always find an optimal solution?


For Pakistani currency the greedy algorithm always gives
optimum solution.
5
Counting Money Problem
A failure of the greedy algorithm:

In some (fictional) monetary system, “kron” comes in 1 kron,


7 kron, and 10 kron coins.

Applying greedy algorithm to count out 15 krons, we would


get
⚫ A 10 kron piece
⚫ Five 1 kron pieces, for a total of 15 krons
⚫ This requires six coins

A better solution would be to use two 7 kron pieces and one 1


kron piece, requiring only three coins

The greedy algorithm results in a solution, but not in an


optimum solution. 6
Bin Packing Problem

7
Approximate Bin Packing
Problem
We are given n items of sizes 𝑆1, 𝑆2, … , 𝑆𝑛. All sizes satisfy
0 < 𝑆𝑖 ≤ 1.

The problem is to pack these items in the fewest number of


bins, given that each bin has unit capacity.

Suppose we have item list with sizes 0.2, 0.5, 0.4, 0.7, 0.1,
0.3, 0.8. Optimal packing of these sizes is

0.3 0.5
0.8
0.1
0.7
0.4 8
0.2
B1 B2 B3
Approximate Bin Packing
Problem
Algorithm 1: Next Fit
When processing any item, we check to see whether
it fits in the same bin as the last item; if it does it is
placed there; otherwise a new bin is created.
void NextFit ( )
{ read item1;
while ( read item2 ) {
if ( item2 can be packed in the same bin as item1 )
place item2 in the bin;
else
create a new bin for item2;
item1 = item2;
} /* end-while */
}
9
Approximate Bin Packing
Problem
Next Fit for 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8 is

0.1
0.5 0.8
0.7
0.4 0.3
0.2
B1 B2 B3 B4 B5
10
Approximate Bin Packing
Problem
Algorithm 2: First Fit
⚫ We scan the bins in order and place the new item in the
first bin that is large enough to hold it. A new bin is created
only when the result of previous placements have left no
other alternative.
void FirstFit ( )
{ while ( read item ) {
scan for the first bin that is large enough for item;
if ( found )
place item in that bin;
else
create a new bin for item;
} /* end-while */
}

11
Approximate Bin Packing
Problem
First Fit for 0.2, 0.5, 0.4, 0.7, 0.1, 0.3,
0.8 is

0.1
0.3
0.5 0.8
0.7
0.4
0.2
B1 B2 B3 B4
12
Approximate Bin Packing
Problem
Algorithm 3: Best Fit
⚫ Instead of placing a new item in the first spot that is found.
It is placed in the tightest spot among all bins

Best Fit for 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8 is

0.3
0.1

0.5 0.8
0.7
0.4
0.2
B1 B2 B3 B4 13

T = O( N log N )
Approximate Bin Packing
Problem
Offline Algorithms
⚫ The major problem with all the on-line
algorithms is that it is hard to pack the
large items, especially when they occur
late in the input.
⚫ The natural way around this is to sort the
items, placing the largest items first.

⚫ Then apply first fit or best fit, i.e.


⚫ Best Fit Decreasing 14
⚫ First Fit Decreasing
Approximate Bin Packing
Problem
Offline Algorithm
⚫ Best Fit Decreasing for 0.8, 0.7, 0.5, 0.4,
0.3, 0.2, 0.1 is

0.2 0.1
0.3
0.4
0.8 0.7 0.5
15
B1 B2 B3

You might also like