University of Virginia Computer ScienceCS216: Program and Data Representation, Spring 2006 |
(none)21 February 2006 |

First attempt:Input:set ofnpositive integers,I= {w_{0}, ...,w_{n-1}}, maximum weightW

Output:a subsetSofIsuch that the sum of the elements ofS≤Wand there is no subset ofIwhose sum is greater than the sum ofSand ≤W.

Prove this algorithm doesn't work. (Hint: is addingS(= best subset with sum ≤w)w

Initially:

S(0) = { }

Elements ofIare positive integers, so no subset of them has weight ≤ 0.Recursion:

S(i) =best =(note that the dynamic programming implementation would turn this around to avoid the multiple computations ofS(i- 1) for e in I: ifi-e>= 0: if sum(S(i-e)) +e> sum(best): best = S(i - e) + [ e ] return bestS(k)).

S(= best subset with sum ≤w,k)wusing only elements {i_{0}, ...,i_{k - 1}}

Initially:

S(0,k) = { } for allk

andS(w, 0) = { } for allwJustification:

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:

- Induction — show that the greedy algorithm is always best according to some criterion at every step, and that by being best according to that criterion after the last step, we know there is no better result.
- Exchange — show that changing any of the decisions made by the greedy algorithm cannot produce a better result. If we can show this for all decisions it makes, we know the result is best.

**Problem:**

Brute force algorithm:Input:a connected, undirected graphG= <V,E> (a set of vertices and weighted edges). Each edge is a triple, <from,to,weight> wherefromandtoare vertices inVandweightis a positive integer.

Output:a subsetSofEthat connects all vertices inVwhere there is no subset ofEthat connects all the vertices with total weight less than the total weight ofS. (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]