Sie sind auf Seite 1von 5

Algorithms

Solutions to Exercise 2

Hwa-You Hsu
December 1, 2003

Exercise 16.4-4
30%
Let S be a finite set and let S1, S2, , Sk be a partition of S into nonempty disjoint
subsets. Define the structure (S, l) by the condition that l = {A:|ASi| 1 for i = 1,
2, , k}. Show that (S, l) is a matroid. That is, the set if all sets A that contain at most
one member in each block of the partition determines the independent sets of a
matroid.
Solution
Recall the definition of matroid:
A matroid is a ordered pair M = (R, I) satisfying the definitions:
1. R is a finite nonempty set.
2. I is hereditary: I is a nonempty family of subsets of R, called the independent
subsets of R, such that if BI and AB, then AI, obviously, I
3.

M satisfies the exchange property: If AI, BI and |A|<|B|, then xB-A such
that A{x}I

To prove that (S, l) is a matroid, consider the following steps:


step 1. Since S1, S2, , Sk are nonempty disjoint finite sets, S, union of S1, S2, , Sk is
also a finite nonempty set.
step 2. Suppose that Xl and YX, |XSi|1 for all i
Assume |YSj| > 1 for some Sj,
let Yj = YSj , since Yj is a subset of X
then |XSj| > 1. Contradiction! |YSi|1 for all i
step 3. Suppose that Xl and Yl, |X| < |Y|
If |XSi| = 0 for all Si, its trivial to find a x that xY-X s.t. X{x}I
If |XSi| = 1 for some of the Si, then |X|1.
Given |X|<|Y|, we have |Y|2.
If |YSi| = 0 for all Si, any x, xY-X satisfies {x}XI
If | YSi | = 1 for some Si, let YSi = {y}, then any z, zY-{y}-X satisfies
exchange property
Thus, (S, l) is a matroid.

Problem 16-1 Coin Exchange


40%
Consider the problem of making change for n cents using the fewest number of coins.
Assume that each coins value is an integer.
a. Describe a greedy algorithm to make change consisting of quarters, dimes, nickels,
and pennies. Prove that your algorithm yields an optimal solution.
b. Suppose that the available coins are in the denominations that are powers of c, i.e.,
the denominations are c0, c1, ,ck for some integers c > 1 and k 1. Show that
the greedy algorithm always yields an optimal solution.
c. Give a set of coin denomination for which the greedy algorithm does not yield an
optimal solution. Your set should include a penny so that there is a solution for
every value of n.
d. Give an O(nk)-time algorithm that makes change for any set of k different coin
denominations, assuming that one of the coins is a penny.
Solution
a. The algorithms works by choosing the coin with the highest value, subtract that
value from the amount to change, and repeat until the amount to change is 0. To
prove this greedy algorithm yields an optimal solution, we must show optimal
substructure and the greedy choice property.
Optimal substructure:
Assume to make changes for n cents, the first coin chosen by the greedy algorithm
is a c1 coin with value of n1. Let C be the optimal solution to change n cents.
Then C {c1} is an optimal solution to change n-n1 cents. Otherwise, let the
optimal solution be B. Then B U{c1} contains fewer coins than C and yet both
makes up n cents, so C cannot be the optimal solution making change for n cents,
which is a contradiction.
Greedy choice property:
If our first choice is a penny, then the amount to change is at most 4 cents.
Optimal choice cannot make the change without using a penny, since any other
coin makes too much change.
If our first choice is a nickel, then the amount to change is between 5 cents and 9
cents. If the optimal choice uses at least 5 pennies, we can turn it to a nickel and
use fewer coins, contracting that it is an optimal solution. Otherwise, the optimal
choice can only change 4 cents, not enough to make the change.
If our first choice is a dime, then the amount to change is between 10 cents and 24
cents. If the optimal choice uses at least 5 pennies, we can turn it to a nickel and
use fewer coins. Otherwise, if it uses 2 nickels, we can turn it to a dime. Otherwise,
it can only make change for at most 9 cents, not enough to make the change.
If our first choice is a quarter, then the amount to change is at least 25 cents. If the

optimal choice uses at least 5 pennies, we can turn it to a nickel and use fewer
coins. Otherwise, if the optimal solution uses at most 2 dimes, it must use enough
pennies to make up at least 25 cents, and we can turn it to a quarter and use fewer
coins. Otherwise, the optimal solution uses at least 3 dimes, we can turn it to a
quarter and a nickel and use fewer coins.
Therefore the greedy choice property is satisfied.
b. The proof is similar to a), we will show optimal substructure here.
Suppose we have n cents to change, cinci+1, 1ik. If the optimal solution
replaces a ci coins (1ac) and [optimal denomination of (n - a ci)], by picking
ac ci-1 coins and [optimal denomination of (n - a ci)]. But obviously, the numbers
of coins to pick are increased, which is not a optimal solution. By contradiction,
the optimal substructure property is proved.
c. Using a set of coins consisting of 8-cent, 6-cent and 1-cent to denominate 12 cents.
Greedy algorithm will give the solution (8, 1, 1, 1), total 5 nickels, but the optimal
solution is (6, 6).
d. As shown in part a), the problem has the optimal substructure property. So we
formulate a Dynamic Programming recursion. Let n[i, j] represent the minimum
number of coins required to make change for j cents using coins with
denomination no greater than ci (c0 = 1). Then
n[0, j] = j
n[i, j] = min( n[i-1, j], n[i, j-ci] + 1) (either do not use coin ci or use the minimum
number of coins to make change for j- ci cents plus 1 for ci ).
We can calculate the value of n[i, j] in O(nk) time.
To reconstruct the values of each coin in the change set by checking
whether n[i, j] = n[i-1, j] or n[i, j] = n[i, j-ci] + 1 in O(k+n) time.

Exercise 17.4-3
30%
Suppose that instead of contracting a table by halving its size when its load factor
drops below 1/4, we contract it by multiplying its size by 2/3 when its load factor
drops below 1/3. Using the potential function
(T ) = 2 num [T ] size [T ] ,

show that the amortized cost of a TABLE-DELETE that uses this strategy is bounded
above by a constant.
Solution
Suppose that i-th operation is TABLE_DELETE, consider the value of load factor:
i= (number of entries in table after iteration i) / (size of table after iteration i) =
numi/sizei
case 1. Ifi-1 = 1/2, i1/2

ci = ci + i - i-1 = 1 + ( sizei 2numi ) - ( 2numi-1 - sizei-1 )


= 1 + ( sizei-1 2(numi-1 -1)) - ( 2numi-1 - sizei-1 ) = 3 + 2 sizei-1 - 4 numi-1
= 3 + 2 sizei-1 - 4i-1 sizei-1-1+2 sizei-1-4/2 sizei-1 = 3
case 2. If 1/3i-1<1/2, i1/2 ,i-th operation would not cause a contraction

ci = ci + i - i-1 = 1 + ( sizei 2numi ) - (sizei-1 - 2numi-1)


= 1 + (sizei-1 2(numi-1-1)) -(sizei-1 - 2numi-1) = 3
case 3. Ifi-1=1/3, i1/2 thus i-th operation would cause a contraction

ci = ci + i - i-1 = numi +1 + ( sizei 2numi ) - (sizei-1 - 2numi-1)


= numi-1 -1 + 1 +( 2/3 sizei-1 2(numi-1 -1) ) - (sizei-1 - 2numi-1)
= numi-1 1/3 sizei-1 + 2 = 2
Thus the amortized cost of TABLE-DELETE is bounded by 3.

Das könnte Ihnen auch gefallen