A4 Solution
A4 Solution
1. (20) Matrix chain multiplication problem: For each of the following suggested greedy ideas,
provide a counter example where the greedy solution does not work:
(a) First multiply the matrices Mi and Mi+1 that minimize the product ri−1 ri ri+1 , and con-
tinue in the same way;
(b) First multiply the matrices Mi and Mi+1 that maximize the product ri−1 ri ri+1 , and con-
tinue in the same way.
Note (1) that when applying a greedy strategy, the idea is to be carried out through the
entire process until an ordering of the multiplications has been found (in other words, you
don’t just apply it once). (2) In general, to show a greedy idea does not work, we provide a
greedy solution for a problem, then another solution (which does not have to be the optimal
solution) such that the greedy solution is worse.
(a) Consider M2×1 × M1×2 × M2×3 :
2. (10) We have unlimited number of bins each of capacity 1, and n objects of sizes s1 , s2 , ...,
sn , where 0 < si ≤ 1. Our job is to pack these objects into bins using the fewest bins possible
(optimization). The decision version can be stated as follows: given n objects and k, is there
a packing using no more than k bins. Show how to solve one version if you know how to solve
the other version, i.e., show how to solve the decision version by solving the optimization
version and show how to solve the optimization version by solving the decision version.
Optimization to Decision: Solve optimization, with m bins solution. If m ≤ k, yes to decision,
else, no to decision.
Decision to Optimization: try decision version with k = 1, k = 2, ... The k for the first
yes answer is the answer.
3. (10) In class, we showed that the greedy algorithm to solve the fractional knapsack problem
optimally by selecting objects in nonincreasing value to weight ratios does not work for the
0-1 knapsack problem (meaning it does not always generate an optimal solution). Show that
this greedy method not only does not work, it yields arbitrarily bad solutions, that is, given
any 0 < ǫ < 1, construct an example where the ratio of the greedy solution to the optimal
solution is < ǫ. (Hint: only two objects are needed).
Objects 1 2
V alues 2ǫ 3
W eights ǫ 3
value to weight ratio 2 1
The greedy solution’s value is 2ǫ and the optimal solution has value 3. The ratio is 2ǫ/3 < ǫ.
4. (10) Construct an example with only three matrices where the worst multiplication order
does n times as many element-wise multiplications as the best order for any n. That is, for
any given n, find a chain of matrices such that if the worst order has x multiplications and
the best order requires y multiplications, then x/y ≥ n.
Consider M1×n × Mn×1 × M1×n (three matrices with dimensions 1 by n, n by 1, and 1 by n).
Optimal: ((M1×n × Mn×1 ) × M1×n ), requiring 1 × n × 1 + 1 × 1 × n = 2n multiplications.
Worst one: (M1×n × (Mn×1 × M1×n )), requiring n × 1 × n + 1 × n × n = 2n2 multiplications.
The ratio is n.
5. (10) A person wishes to cross a desert carrying only a single water bottle. He has a map that
marks all the watering holes along the way. Assuming he can walk k miles on one bottle of
water, design an efficient algorithm for determining where he should refill his water bottle in
order to make as few stops as possible. Argue why your algorithm is correct.
Always fill the water bottle at the farthest watering hole that is ≤ k miles.
Let A be an optimal solution, if its first one is not the same as our greedy one, we can replace
its first one with our greedy one and we still have an optimal solution because its first one
has a distance no bigger than our greedy one.
6. (10) A weighted DAG (directed acyclic graph) is a directed graph with no cycles and each
edge has a weight. Show how to reduce the problem of finding a LIS discussed in class to the
problem of finding a longest path in a weighted DAG.
Given an instance of LIS: distinct integers x1 , x2 , ..., xn , we create an instance of a longest
path problem in a DAG as follows:
DAG = (V, E) where V = {x1 , x2 , · · · , xn } and (xi , xj ) is an edge if
7. (30) (a) Let P1 , P2 , ..., Pn be a set of n programs that are to be stored on a memory chip of
capacity L. Program Pi requires ai amount of space. Note that L and ai ’s are all integers.
We assume that a1 + a2 + ... + an > L. The problem is to select a maximum subset Q of the
programs for storage on the chip. A maximum subset is one with the maximum number of
programs in it. Give a greedy algorithm that always finds a maximum subset Q such that
P
Pi ∈Q ai ≤ L. You also have to prove that your algorithm always finds an optimal solution.
Note that a program is either stored on the chip or not stored at all (in other words, you do
not store part of a program).
(b) Suppose that the objective now is to determine a subset of programs that maximizes the
amount of space used, that is, minimizes the amount of unused space. One greedy approach
for this case is as follows: As long as there is space left, always pick the largest remaining
program that can fit into the space. Prove or disprove that this greedy strategy yields an
optimal solution.
(c) We can formulate the problem in (b) into a decision problem as follows: given n programs
of sizes a1 , a2 , ..., an , with chip capacity L, and integer k, is there a packing of programs
onto the chip such that the unused space is k or less. Show that this decision problem is
NP-complete.
(a) Smallest program 1st: if an optimal solution does not include the smallest, replace any
one with the smallest and it is still optimal.
(b) Disapprove: Program sizes are 2, 2, and 3, with L = 4. Greedy = 3; Optimal = 2+2 = 4.
(c)
(1) Show it is in NP: Given a certificate ai1 , ai2 , ..., aij , it is easy to check in polynomial time
whether L − (ai1 + ai2 + · · · + aij ) ≤ k.
(2) We show that Subset sum ≤p this problem. Given an instance of the subset sum, x1 , x2 ,
..., xn , and b, we let ai = xi , 1 ≤ i ≤ n, L = b, and k = 0. Then the problem has a yes
solution iff the subset sum has a yes solution.