Cs141 Home/Hwk2Soln

Cs141 Home | Cs141 Home | recent changes | Preferences

Showing revision 1

cs141 Written Homework 2, solutions

  1. Give a careful proof that ni=1 i log(i) = Θ(n2 log n).
  2. ANSWER: Each term in the sum is at most n log n and there are n terms, so the sum is at most n2 log n. Applying the definition of big-O with c = 1 and n0 = 1, we see easily that this is O(n2log n).

    The largest n/2 terms in the sum are at least (n/2) log (n/2), so the sum is at least (n/2)2 log(n/2) = (n2/4)( log(n) - log(2)).

    Since log(2) ≤ log(n)/2 for n larger than some n0 (how large depends on the base of the logarithm), the sum is at least (n2/4) log(n)/2 = n2/8 log(n) for n ≥ n0. Thus, taking c=(1/8) and applying the definition of Ω, the sum is Ω(n2 / log n).

  3. (a) Give an example input where it is possible to make correct change but the greedy algorithm fails to do so (the algorithm gives back insufficient change).

    ANSWER: Take denominations 4 and 5 and desired total 8.

    (b) Suppose the denominations are American coins: c[1] = 1, c[2] = 5, c[3] = 10, c[4] = 25. Is it the case that, for these denominations, for any positive integer T, the greedy algorithm makes change totaling T using the minimum possible number of coins?

    ANSWER: Yes, it is the case.

    In this case we think of the greedy algorithm as the following recursive procedure:

    greedy(T)
    1. If T == 0, return: {} (the empty set of coins)
    2. If T > 25, return: {quarter} ∪ greedy(T-25)
    3. If T > 10, return: {dime} ∪ greedy(T-10)
    4. If T > 5, return: {nickel} ∪ greedy(T-5)
    5. return: {penny} ∪ greedy(T-1)

    We will prove by induction on T that the greedy algorithm returns the unique minimum-size set of coins totalling T.

    The base case is T=0. In this case, the claim is clearly true.

    Next consider any T>0. We will show that the claim is true, assuming by induction that the claim is true for any T' < T.

    Let S be a minimum-size set of coins totalling T. We wil show by induction that greedy(T) = S.

    Let greedy(T) = {X} union greedy(T') be the set produced by greedy algorithm, where X is quarter, dime, nickel, or penny (depending on T) and T' is T-value(X).

    CASE 0: Suppose S also has an X in it (where X is the coin added by greedy, above). Since S-{X} must be an optimal way of making change for T', by induction, we know that greedy(T')=S-{X}. Thus,

    greedy(T) = {X} ∪ greedy(T') = {X} ∪ (S-{X}) = S.
    That is,
    greedy(T) = S.

    So, if S has an X in it too, then greedy(T) produces S, and we are done.

    Otherwise, S has no X in it. We consider the possible cases, and show that none of them can happen.

    CASE 1: T=1,2,3,4 (so X is a penny), and S has no penny in it.

    This cannot happen, since S cannot make change for T < 5 without pennies.

    CASE 2: T=5,6,7,8,9 (so X is a nickel), and S has no nickel in it.

    In this case S must be making change worth 5,6,7,8, or 9 with only pennies. (S clearly cannot use coins of value more than T, so dimes and quarters are out). So in this case, S would have to have at least 5 pennies. But S is a minimum-size set totalling T, so it cannot have 5 pennies (because they could be replaced by a nickel to get a smaller set). So this case cannot happen.

    CASE 3: T is at least 10 and at most 24 (so X is a dime), and S has no dime in it.

    In this case, S must be making change worth at least 10 with only nickels and pennies. In any way of doing so, S would have to have two or more coins of total worth 10. (Check this.) Replacing these coins by a dime would yield a set smaller than S but with the same total. This would contradict our assumption that S is optimal. So this case cannot happen.

    CASE 4: T is at least 25 (so X is a quarter), and S has no quarter in it.

    In this case, S would have to make change worth at least 25 with only dimes, nickels, and pennies.

    S cannot have three or more dimes --- they could be replaced by a quarter and a nickel.

    S cannot have two dimes and a nickel --- they could be replaced by a quarter.

    So S has less than 25 cents worth of dimes and nickels, and the rest in pennies, totalling 25. But then the dimes and nickels, together with some pennies, could be replaced by a quarter to give a smaller set. So this cannot happen either.

    summary: We've shown that whatever coin that greedy adds, the optimal set S must also have the same coin. We conclude by induction that the greedy set returns the same set as S.

  4. ... L(n) satisfies the recurrence relation L(n) = L(n/4)+L(n/4) + 2, L(1) = 1. Describe the recurrence tree for L(n). How many levels does it have? What is the depth? How many children does each node have?

    ANSWER: it has #levels = depth = log4 n, because at each level, n decreases by a factor of 4.

    What is the value of L(n)? (Give a careful proof.)

    ANSWER: Using the method from class, the value of L(n) is the number of nodes in the recursion tree, times 2. The recursion tree is a complete binary tree of depth d = log4 n, so it has 2d-1 = Θ(2log4 n) = Θ(nlog4 2) = Θ(n1/2) nodes. So L(n) = Θ(n1/2).

    What is the area used by a drawing of this kind? Explain your reasoning.

    ANSWER: the area is the width times the height, each of which is L(n) = Θ(n1/2). So the area is Θ(n). You can see this directly from the diagram, since for each of the n leaves there is a 2x2 box in the grid for that leaf.


Cs141 Home | Cs141 Home | recent changes | Preferences
This page is read-only | View other revisions | View current revision
Edited February 14, 2005 12:44 am by Neal (diff)
Search: