University of Virginia Computer Science CS216: Program and Data Representation, Spring 2006 |
(none) 21 February 2006 |
Input: set of n positive integers, I = {w0, ..., wn-1}, maximum weight WFirst attempt:Output: a subset S of I such that the sum of the elements of S ≤ W and there is no subset of I whose sum is greater than the sum of S and ≤ W.
S(w) = best subset with sum ≤ wProve this algorithm doesn't work. (Hint: is adding e to S(w) always valid?) Second attempt:
Initially: S(0) = { }
Elements of I are positive integers, so no subset of them has weight ≤ 0.Recursion:
S(i) =best = S (i - 1) for e in I: if i - e >= 0: if sum(S (i - e)) + e > sum(best): best = S(i - e) + [ e ] return best(note that the dynamic programming implementation would turn this around to avoid the multiple computations of S(k)).
S(w, k) = best subset with sum ≤ w using only elements {i0, ..., ik - 1}
Initially: S(0, k) = { } for all k
and S(w, 0) = { } for all wJustification:
Recursion:
S(w, k) =
We can prove an algorithm is not a correct solution to the problem by finding some input on which it does not produce the correct result. For optimization problems, we sometimes say "optimal" instead of "correct". A non-optimal algorithm produces an answer on all inputs, but it does not produce the best answer on some input.
We can prove an algorithm is optimal by showing that it always finds a solution that is at least as good as the best solution. Note that for many optimization problems there are many solutions that are equally good, as we saw with the Phylogenetic trees in PS2. So, to be optimal the algorithm needs to find one of these best solutions.
Common strategies for constructing such a proof are:
Problem:
Input: a connected, undirected graph G = < V, E > (a set of vertices and weighted edges). Each edge is a triple, < from, to, weight > where from and to are vertices in V and weight is a positive integer.Brute force algorithm:Output: a subset S of E that connects all vertices in V where there is no subset of E that connects all the vertices with total weight less than the total weight of S. (Total weight is the sum of the weights of edges.)
Running time:
Correctness:
Running time:
Correctness:
Running time:
[an error occurred while processing this directive]