CS 4010 Sample Mid-Term Exam: Solutions

Transcription

CS 4010 Sample Mid-Term Exam: Solutions
CS 4010
Sample Mid-Term Exam: Solutions
1. Apply the loop invariant approach to insertion-sort-reverse, which sorts a list of
integers in reverse order. Use this approach to show the complexity of the
algorithm.
This is identical to a homework problem, except that we reverse the sorting.
2. Give a polynomial time algorithm that takes three strings, A, B and C, as input,
and returns the longest sequence S that is a subsequence of A, B, and C.
The solution to the problem of the shortest common subsequence for three strings
is essentially identical to the solution with 2 strings. We use, as in the case with 2
strings, matrices for cost (T(i,j,k)) and recovering the string b(i,j,k). By treating
the T(i,j,k)'s as array entries and updating the table in the appropriate way we can
get the following O(n3) time algorithm for filling in the “cost” matrix. (Note: we
also generate a b matrix, that has arrows indicating: ( ) a match (and cost
increased by 1); (↑←): a non-match.
For i=0 to m do T(i,0,0)=0
For j=0 to n do T(0,j,0)=0
For k=0 to o do T(0,0,k)=0
For i=1 to m do
For j= 1 to n do
For k= 1 to o do
if A(i) = B(j) = C(k) then T(i,j,k)=T(i-1,j-1,k-1) + 1
else T(i,j,k)= MAX( T(i, j-1, k), T(i-1, j, k), T(I, j, k-1))
Algorithm for printing the string from the b matrix.
3. Consider the following 2Clique problem:
INPUT: An undirected graph G and an integer k.
OUTPUT: 1 if G has two vertex disjoint cliques of size k, and 0 otherwise.
Show that this problem is NP-hard. Use the fact that the clique problem is NPcomplete. The input to the clique problem is an undirected graph H and an integer
j. The output should be 1 if H contains a clique of size j and 0 otherwise. Note that
a clique is a mutually adjacent collection of vertices. Two cliques are disjoint if
they do not share any vertices in common.
NP-Hardness: To reduce the clique problem to the 2clique problem, create a
by adding a k-vertex clique to G; do not connect any of the vertices
new graph
in the new clique to any of the vertices of the original graph. Call the procedure
for 2clique on
; a 1 will be returned if and only if G has a clique of size k.
4. In this problem you have n chickens with weights
grams that you wish
to pack into packages. Each package must contain at least L grams of chicken so
you don't get sued for false advertising. The goal is to do this by maximise the
number of packages that you fill to L or more grams.
Consider the obvious greedy algorithm that considers the chickens in an arbitrary
order, and adds the chickens to the same package until that package is full. Show
that the performance ratio of this algorithm is 2. That is, that this algorithm fills at
least 1/2 as many bags as optimal.
We assume that we have a set B of bags, and a set C of chickens. We output a set
F of filled bags. Our algorithm greedily chooses the first available chicken from C,
and puts it in a bag, until the bag has weight at least L. We will initialise F←{}.
Algorithm:
Greedy-pack(B, C, F, L)
// Weight of bag Bi =wi //
F←{}
//Set chickens:= // C←{b1,…,bn}
Do for bag i=1,…,m until C= ∅, //(all n chickens are packed);//
Greedy-pack-aux(Bi , C, L)
Return F;
Greedy-pack-aux(Bi , C, F, L)
//For bag Bi, insert first chicken bk from C;//
C←C- bk
Bi ← Bi + wi
If Bi ≤ L, Greedy-pack-aux(Bi, C, F, L) //add another chicken to bag//
Else do
F←F ∪ Bi
wi = 0
Greedy-pack-aux(Bi+1, C, F, L)
Now we show that the greedy method fills no worse than half as many bags as the
optimal method. To do this we show that the greedy method never puts weight more than
2 times the weight in any bag that the optimal method does. Assume that the greedy
method puts weight 2L in a bag. This can happen only if the last chicken added weighs
more than L, say L+ δ, to give total weight ≤2L+ δ. In that case, the chicken weighing
L+ δ must be put in a corresponding bag in the optimal approach. The ratio of weights is
L+ δ/2 L+ δ ≤ ½
So if k* is the optimal number of bags, k*≈ (Σ bi) /L.
We will fill k bags, where
k ≥ (Σ bi) /2L
≥ ½ k*.