Beruflich Dokumente
Kultur Dokumente
Answer: If we call Mystery(1), then T will become 1. In general, whatever the value of T is, once
Mystery(1) is called, the value of T will increase by 1. If we call Mystery(x) for x > 1, then this
will cause the increase of T by 1, plus the increase caused by three recursive calls to Mystery(x 1).
If f (n) is the nal value of T as a function of n after a call to Mystery(n), or, in other words, f (n)
is the total increase of T the above imply:
Answer: Let f (n) be the number of xs Mystery(n) prints, when n is an integer n > 1. Since n
is a power of 3, this means n 3. For the base case, we see that f (3) = 2. For n > 3 we have
f (n) = 3f ( n3 ) + 2. We thus have to solve:
n
f (n) = 3f ( ) + 2 ,
with f (3) = 2.
3
Answer: Let f (n) be the number of xs Mystery(n) prints, when n is an integer n > 1. For the
base case, we see that f (2) = 1. For n > 2 we have f (n) = 4f (n 1) + 1. We thus have to solve:
f (n) = 4f (n 1) + 1
with
f (2) = 1.
f (n) = 4f (n 1) + 1
= 42 (4f (n 3) + 1) + 4 + 1
= 43 f (n 3) + 42 + 4 + 1
= ......
= 4k f (n k) + 4k1 + . . . + 42 + 4 + 1 guessing general term
= 4k f (n k) +
4k 1
41
4k 1
3
4n2 1
3
N +1 1
using xN + . . . + x2 + x + 1 = x
for x = 4 and N = k 1
x1
substitute k = n 2
because we know f (2) = 1
4n2 1
3
3 4n2 + 4n2 1
3
4 4n2 1
3
4n1 1
.
3
= 4n2 +
=
=
=
f (n) =
=
=
=
=
=
=
=
=
=
= 4k f (n k) +
= 4n2 f (2) +
=
n1
n1 1
= 23 +3
2
n1
= 33 2 1
n
= (3 1) /2
n
for x = n/3 the general form becomes
f (n) = 3f ( ) + 2
f ( n3 ) = 3f ( 3n2 ) + 2
3
n
= 3 3f ( 2 ) + 2 + 2
3
n
for x = n/32 the general form becomes
= 32 f ( 2 ) + 3 2 + 2
f ( 3n2 ) = 3f ( 3n3 ) + 2
3
n
= 32 3f ( 3 ) + 2 + 3 2 + 2
3
n
= 3 3 f ( 3 ) + 32 2 + 3 2 + 2
3
n
3
= 3 f ( 3 ) + 2 (32 + 3 + 1)
3
= ......
n
= 3k f ( k ) + 2 (3k1 + . . . 32 + 3 + 1) guessing general term
3
N +1
n
3k 1
using xN + . . . + x2 + x + 1 = x x11
k
= 3 f( k ) + 2
3
31
for x = 3 and N = k 1
= 3k f (
n
3k 1
)+2
3k
2
= 3k f (
n
) + 3k 1
3k
substitute 3nk = 3
because we know f (3) = 2
implying 3nk = 3 3k+1 = n
k + 1 = log3 n k = log3 n 1
Thus f (n) =
1 /3.
(You can verify that this formula gives f (2) = 1, f (3) = 5, etc, as it should.)
4n1
Answer: Let f (n) be the number of xs Mystery(n) prints, when n is a power of 2 with n > 1.
Thus the smallest number that Mystery is called with is Mystery(2). For the base case, we see that
f (2) = 2. For n > 2 we have f (n) = 2f ( n2 ) + n. We thus have to solve:
n
f (n) = 2f ( ) + n
2
with
f (2) = 2.
Answer:
a. 2 > 1, 3 > 1, 8 > 6, 8 > 1 and 6 > 1, thus the inversions are indices (1,5) for 2 > 1, indices (2,5) for
3 > 1, indices (3,4) for 8 > 6, indices (3,5) for 8 > 1, and indices (4,5) for 6 > 1.
b. We rst argue that the maximum number of inversions that an array with n elements can have
is n(n1)/2. This is because, by denition, an inversion involves two indices i and j with i < j,
thus the rst element with index 1 can result to at most (n 1) inversions with each one of the
indices 2, . . . , n, and in general, the i-th element with index i can result to at most (ni) inversions
n1
with each one of the indices (i+1), . . . , n. The above gives a max total of i=1
(ni) = n(n1)/2.
It is now easy to see that the array n, (n1), . . . , 2, 1 indeed has n1
i=1 (ni) = n(n1)/2 inversions,
thus the above array indeed has the maximum possible number of inversions.
c. Let A be an array of n distinct numbers. We want to count the inversions of A, ie the pairs of
indices i and j with i < j and A(i) > A(j). The diculty in counting the number of inversions
in time O(n log n) is that there could be O(n2 ) inversions. Therefore, our algorithm should be
accounting for many sets of inversions in large groups (without expllicitly enumerating the members
of the group.)
Let us assume that n is a power of 2, and let us partition the pairs to three classes.
The rst class consists of indices i < j n/2 with A(i) > A(j). These inversions will be computed
recursively. The second class consists of indices ( n2 + 1) i < j with A(i) > A(j). These inversions
will be also computed recursively. The third class consists of indices i < j, with 1 i n/2,
( n2 + 1) j n, and A(i) > A(j). It is now important to notice that the number of inversions of
the third class will not change if we permute the elements in the rst half of A arbitrarily, and if we
also permute the elements in the second half of A arbitrarily. In particular, it is convenient to have
the subarrays A(1), . . . , A( n2 ) and A( n2 + 1), . . . , A(n) sorted (as in mergesort.) This is because we
can now compute the number of inversions of the third class while merging (as in mergesort) the
rst half of A with the second half of A as follows: Suppose that we have found the rst k elements
of the sorted merged array, and all the inversions involved with these elements. Suppose also that,
of these k elements, k1 come from the rst part of A and k2 come from the second part of A. Thus,
we are now comparing the i = (k1 + 1)-st element of the rst part of A with the j = (k2 + 1)-st
element of A. If A(i) < A(j) then the (k + 1)-st element of the merged sorted array is A(i) and
there are no more inversions involved with i or A(i). This is because the only possible inversions
would have to involve a j > qj with A(i) > A(j ), which is impossible, since the second half of
A is sorted, thus A(i) < A(j) < A(j ) for all j > j. On the other hand, if A(i)
> A(j) then the
(k + 1)-st element of the merged sorted array is A(j) and there are n2 i + 1 inversions involved
with j or A(j). This is because all the pairs (i, j), (i + 1, j), . . ., (n/2, j) involve inversions, since
n
j 2 + 1, while the second part of A is sorted thus A(n/2) > . . . > A(i + 1) > A(i) > A(j).
The above algorithm is a straightforward modication of mergesort and runs in time O(n log n).
n
+c
2
n
+ 2c
T
22
n
T
+ 3c
23
.....
n
T
+ kc
2k
T (n) T
Problem 7: Mix and Max with very few comparisons: Ecient Design 15 points).
Let A be an array of n distinct numbers. It is clear that one can nd the maximum of these numbers
with n1 comparisons. Similarly, it is clear that one can nd the minimum of these numbers with
n1 comparisons. Thus, one can nd both the maximum and the minimum of these numbers with
2n2 comparisons. But one can do better! Show how to nd both the maximum and the minimum
of n distinct numbers with 3 n2 2 comparisons (you may assume that n is even.) Note: If you give
pseudocode, it should be very short and very clear.
Answer No 1: In STEP 1 below, using n/2 comparisons, we partition the n elements of A into
two sets B and C, such that the maximum of A belongs to the set B, the minimum of A belongs
to the set C, and each one of B and C
has exactly n/2 elements.
In STEP 2 below, we use n2 1 comparisons to nd the maximum of B, which is also the
maximum of A, and another n2 1 comparisons to nd the minimum of C, which is also the
minimum of A.
Thus, the total number of comparisons is n/2 from STEP 1 and 2 n2 1 from STEP 2, for a
total of
n
n
n
+2
1 = 3 2 .
2
2
2
STEP 1: For each successive pair of integers in A, we put the maximum of the pair in B and the
minimum of the pair in C. In particular:
for i = 1 to n/2
if A(2i 1) > A(2i) then begin
B(i) := A(2i 1);
C(i) := A(2i);
end
else begin
B(i) := A(2i);
C(i) := A(2i 1);
end
We now argue that B indeed contains the maximum of A, and C indeed contains the minimum
of A. To argue that B indeed contains the maximum of A, it suces to argue that C does not
contain the maximum of A. This is clear, since for each element of C there is one element of A
which is larger than C (if C(i) = A(2i) then A(2i 1) > A(2i) while if C(i) = A(2i 1) then
A(2i) > A(2i 1).) Similarly, we can argue
that
C indeed contains the minimum of A.
STEP2: The standard method of using n2 1 comparisons to nd the maximum of B, and another n2 1 comparisons to nd the minimum of C:
max := B(1); for i := 2 to n/2 if B(i) > max then max := B(i);
min := C(1); for i := 2 to n/2 if C(i) < min then min := C(i);
Answer:
Main Fact: If A(1) + A(n) > S then A(i) + A(n) > S for all i. (Thus we may discard A(n) as a
candidate in a pair A(i) + A(j) = S.)
Proof: The integers are distinct and sortd, thus A(i) > A(1) for all i > 1, thus A(i) + A(n) >
A(1) + A(n) > S.
Similarly, if A(1) + A(n) < S then A(1) + A(i) < S for all i. Thus we may discard A(1) as a
candidate in a pair A(i) + A(j) = S.
Algorithm: We have two pointers, initially placed on A(1) and A(n) respectivey. We will proceed
in at most (n 1) rounds until either the left pointer is on some i and the right pointer is on some j
with i < j and a(i) + a(j) = S (in which case we have found a desired pair), or the left pointer and
the right pointer coincide (in which case there is no pair with the desired property.) In each step, if
the left pointer is on i and the right pointer is on j, we compare a(i) + a(j) to S. If a(i) + a(j) > S
we move the right pointer to (j 1). If a(i) + a(j) < S we move the left pointer to (i + 1). If
a(i) + a(j) = S we have found a pair with the desired property.
T (n) = T (n 2) + 3
= T (n 4) + 3 2
= T (n 6) + 3 3
= ......
= T (n 2k) + 3 k
= T (n 2k) + 3 k
n
1
2
n
= 1+3 3
2
n
= 3 2 .
2
= T (2) + 3
T (n)
3T
n
4
3 3T
32 T
32 3T
=
=
stop when
x
42
for x =
n
+2
4
n
4
x
42
+2
T (n)
n
4
3 T
3k T
n
4k
n
4k
n
42
+2
3
4
+ 2n
+ 2n
3
+ ... + + 1
4
for x < 1
3log4 n2 T
n
3log4 n
T 42 log n
32
4 4
log n
4log4 n
3 4
3log4 n 2
T (16) log4 n
3log4 n
3
T 4 + 8n 16 log4 n 9
=
+ 8n 8 16
4
32
9
9
4log4 n2
3
4
x
3
+7
5log3 n1 7 + 7
5log3 n1 1
4
5log3 n 5log3 n 1
7
+
5
20
4
=
=
5 5log3 n 1
20
4
Assume also wlog that all elements of A are distinct. Let S be the array A correctly sorted, that is s1 < s2 < . . . < sn
n
k
2k
and n
i=1 {ai } = i=1 {si }. Let SL = i=1 {si } be the n/3 smallest elements of A. Let SM = i=k+1 {si } be the n/3
+7 +57+7
4 5log3 n 5log3 n 1
+
7
20
20
4
=
96 log4 16
9 3
53 T
32 log4 n
3log4 n
96
+ 8n 8 16
3
= 8n 3log4 n = O(n)
9
9
9
n
33
16
n
3
+57+7
+7 +7
52 5T
for x =
log4 n2
3
n
32
n
32
n
+ 52 7 + 5 7 + 7
33
n
+ 7 (5k1 + . . . + 5 + 1)
guessing general term
=
5k T
3k
n
5k 1
k1 + . . . + x + 1 = xk 1 , for x > 1
=
5k T
+
7
using
x
x1
3k
51
n
n
n
stop when
= 3 = 3k log3 = k k = log3 n log3 3 k = log3 n 1
3k
3
3
5log3 n1 1
=
5log3 n1 T (3) + 7
4
using xk1 + . . . + x + 1 =
1 4
+ 2n
52 T
n
n
n
= 4k log4
= k k = log4 n log4 16 k = log4 n 2
= 16
4k
16
16
k
34
1 34
5 5T
+7
+32
k1
n
3
5T
+ 2n
n
+ 2n
4
+32
n
43
n
+ 2n
4
n
n
n
2
+
3
+
3
+ 2n
33 T
43
42
4
n
3 2 3
+
1
+
2n
+
33 T
43
4
4
n
42
+ 2n
(b)
T (n) = 3T (2n/3) + c
7 log3 n
(5
1)
4
7 log3 5 log3 n
7
1) = (nlog3 5 1) = O(nlog3 5 )
(3
4
4
k
2
3
n=1n=
k
3
2
k = log1.5 n
log1.5 n
log1.5 3 log1.5n
) = O(1.5
) = O(nlog1.5 3 )
(c) Mergesort is O(n log n), bubblesort is O(n2 ), Stoogesort is O(nlog1.5 3 ) O(n2.725 ), no funding!
Hence T (n) = 74 (5log3 n 1) (verify also formula gives T (3) = 47 (5log3 3 1) = 74 (5 1) = 7.)
wi
xi <xk
1
2
and
wi
xi >xk
1
.
2
a. Show how to compute the weighted median of n elements in O(n log n) time using sorting.
b. Show how to compute the weighted median of n elements in O(n) time using a linear-time
median nding algorithm similar to KSELECT.
Answer:
(a) STEP 1: Use an O(n log n) sorting algorithm to sort the xi s. If the position of xi in sorted
order is j, then dene p(xi ) := j.
STEP 2: Scan the xi s in sorted oder, until you nd xk satisfying weighted median property:
m := 0; index := 0;
while m 12
index := index + 1;
m := m + wp(xindex ) ;
return( index );
(b) We solve a more general problem: For n distinct elements x1 , x2 , . . . , xn with positive weights
w1 , w2 , . . . , wn and a number ni=1 wi , we want the element xk satisfying
xi <xk
wi
and
xi >xk
wi (
n
wi ) .
i=1
EpsilonSplitter(x1 , x2 , . . . , xn , w1 , w2 , . . . , wn , )
STEP 1: Use KSELECT to nd the median s of x1 , . . . , xn .
STEP 2: Let A = {xi : xi s} and B := {xi : xi > s}.
STEP 3: compute w(A) := xi A wi ;
STEP 4: if w(A) > then EpsilonSplitter(xi A, wi : xi A, )
else EpsilonSplitter(xi B, wi : xi B, w(A));
in STEPS 1 through 3, we reduce the problem to one of half
Complexity: Using O(n) operations
the size. Thus T (n) = T n2 + cn which solve to O(n).
Chip A says
B is good
B is good
B is bad
B is bad
Chip B says
A is good
A is bad
A is good
A is bad
Conclusion
both are good, or both are bad
at least one is bad
at least one is bad
at least one is bad
(a) Consider the problem of nding a single good chip from among n chips, assuming that more
than n/2 of the chips are good. Show that n/2
pairwise tests are sucient to reduce the probelm
to one of nearly half the size.
(b) Show that the good chips can be identied with O(n) pairwise tests, assuming that more than
n/2 of the chips are good, as in part (a). Give and solve the recurrence that describes the number
of tests. (Avoiding code or pseudocode recommended.)
Abswer:
(a) Case 1: n is even, so n = 2k and there are at least (k + 1) good chips.
Suppose that we test the chips in k pairs.
If a pair answers {good, bad} or {bad, bad} then we know that the pair contains at least one bad
chip. Suppose there are k such pairs, and we discard all the chips in these pairs. By discarding
these pairs, we remove exactly 2k chips of which at least k are bad and at most k are good.
We are thus left with 2(k k ) chips, at least (k + 1) k of them are good, and when compaired
in pairs the answer of each pair is {good, good}. This means that, in each pair, either both chips
are good, or both chips are bad, and more than half of the pairs correspond to two good chips. We
may thus keep one chip from each pair, and be left with (k k ) chips such that more than half of
these chips are good. This reduces the problem of nding a good chip from 2k to at most k (note
als0 that (k k ) > 0 since there is at least one pair that outputs {good, good} among the rst k
comparisons, ie k < k.)
Case 2: n is odd, so n = 2k + 1 and there are at least (k + 1) good chips.
Suppose that we test the chips in k pairs, and one last odd chip is not tested.
For the k pairs, follow the method of Case 1, and suppose that we are left with (k k ) chips. If
(k k ) is even (it could also be 0, which is an even number) then for the next round we use these
(k k ) chips AND the last odd chip. If (k k ) is then for the next roud we use these (k k )
chips AND DO NOT use the last odd chip.
(b) The above method reduces the problem of nding a good chip to one of half the size, using n/2
comparisons. In the end we will be left with a single chip which is a good chip. The total number
of comparisons is T (n) = T (n/2) + n/2 which solves to T (n) n. Once a good chip is found, we
can use it in (n 1) pairwise tests with the remaining (n 1) chips, and determine the good and
the bad chips in the entire set.
T (n) = T (n/3) + 1
= T (n/32 ) + 1 + 1 = T (n/32 ) + 2
= T (n/33 ) + 1 + 2 = T (n/33 ) + 3
= T (n/3k ) + k
= T (n/3log3 n1 ) + log3 n 1
= T (3) + log3 n 1
= 1 + log3 n 1
= log3 n
Algorithm: If n = 2 then check the two cards. If they are equivalent, then output one of them. If
they are not equivalent, then output nil. If n > 2 then
Recurse on c1 , . . . , c n2 . The output should be, either nil, or a card ci with 1 i n2 .
Recurse on c n2 +1 , . . . , cn . The output should be, either nil, or a card cj with ( n2 + 1) cj n.
If the output of both the above recursive calls is nil then output nil and halt.
If the output of one of the recursive calls is nil and of the other recursive call is a card ca , then
compare ca with all the cards in c1 , . . . , cn and if the number of cards found equivalent to ca is
greater than n/2 then output ca and halt.
If the output of both recursive calls is not nil, ie the rst recursive call output a card ca and the
second recursive call output a card cb then
Compare ca with all the cards in c1 , . . . , cn and
if the number of cards equivalent to ca is greater than n/2 then output ca and halt
else compare cb with all the cards in c1 , . . . , cn and
if the number of cards equivalent to cb is greater than n/2 then output cb and halt
else output nil.
a((1)) > a((2)) < a((3)) > a((4)) < a((5)) . . . a((n 2)) > a((n 1)) < a((n)).
For example, on input 100, 20, 2, 5, 200, 50, 40, 201, 300
a wigly arrangement is 100, 20, 200, 2, 50, 5, 201, 40, 300
since 100 > 20 < 200 > 2 < 50 > 5 < 201 > 40 < 300.
Give an O(n) comparison algorithm that outputs a wigly arrangement of an unsorted input array
a(1), . . . , a(n) of n distimct integers, where n is odd. You may give a simple description of the
algorithm (no pseudocode.) You should include a short argument of correctness and running time.
Answer:
Using O(n) comparisons, nd the median m of a(1), . . . , a(n).
Scanning a(1), . . . , a(n) and using n comparisons, nd the set S1 of (n 1)/2 elements that are
smaller than m, and the set S2 of (n + 1)/2 elements that are greater than or equal to m. Clearly,
every element of S1 is smaller than every element of S2 .
Place the elements of S1 on the (n 1)/2 even numbered indices a(2), a(4), . . . , a(n 1).
Place the elements of S2 on the (n + 1)/2 odd numbered indices a(1), a(3), . . . , a(n).
Since every element of S1 is smaller than every element of S2 , the nal arrangement is wigly.
(b) In part (a) we gave a linear time algorithm to nd the mincost vertex cover of a weighted line.
In addition, the algorithm was computing the mincost vertex covers conatining and not containing
an end vertex of the line. In this part we will nd the mincost vertex cover of a weighted cycle by
reducing this problem to three separate mincost vertex cover problems on suitably dened weighted
lines, ans taking the smaller of three suitale solutions.
In particular, for the cycle {{1, 2}, {2, 3}, . . . , {(n 1), n}} {{n, 1}}, edge {n, 1}} must be
covered by any vertex cover. There are three cases: (i)Vertex 1 belongs to the vertex cover of the
cycle and vertex n does not belong to the vertex cover of the cycle. (ii)Vertex n belongs to the
vertex cover of the cycle and vertex 1 does not belong to the vertex cover of the cycle. (iii)Both
vertices 1 and n belong to the vertex cover.
For case (i) above, we consider the line graph {{2, 3}, . . . , {(n1), n}} and compute Ci,out (n1)
and OPTi,out (n 1). We let X := w(1) + OPTi,out (n 1). For case (ii) above, we consider the
line graph {{1, 2}, {2, 3}, . . . , {(n 2), (n 1)}} (with vertices considered from higher to lower)
and compute Cii,out (n 1) and OPTii,out (n 1). We let Y := w(n) + OPTii,out (n 1). For case
(iii) above, we consider the line graph {{2, 3}, . . . , {(n 2), (n 1)}} and compute Ciii (n 2) and
w(1) + w(n) + OPTiii (n 2).
If X = max{X, Y, Z} then the mincost vertex cover of the cycle is {1} Ci,out (n 1) and has
cost w(1) + OPTi,out (n 1). If Y = max{X, Y, Z} then the mincost vertex cover of the cycle is
{n} Cii,out (n 1) and has cost w(n) + OPTii,out (n 1). If Z = max{X, Y, Z} then the mincost
vertex cover of the cycle is {1, n} Ciii (n 2) and has cost w(1) + w(n) + OPTiii (n 2).
Cin (k) =
Therefore, a linear algorithm that computes OPT(n) = min{OPTout (n), OPTin (n)} is a
memoization of the above recursions that compute Cout (k) and Cin (k), for k := 2 to n.
Initialization: C2,out := {1}; OPTout (2) := w(1); C2,in := {2}; OPTin (2) = w(2);
Main Loop: for k := 2 to n begin
Cout (k) = Cin (k 1); OPT
out (k) = OPTin (k 1);
Cin (k) := {k} Cout (k 1)
if w(k) + OPTout (k 1) w(k) + OPTin (k 1) then
OPTin (k) := w(k) + OPTout (k 1)
else
end;
if OPTout (n) < OPTin (n) then
else
else
(k)
(k1)
(k)
(k1)
dij dik
(k1)
+ dij
ij kj
(b) Floyd-Warshall (W ) detecting negative cycle
1 n rows(W )
2a D(0) W
2b for i 1 to n
for j 1 to n
if ((i = j) or (i, j) E) then (0) (j) i
2c NegCycle := false; N 0
3 for k 1 to n
4
for i 1 to n
5
j 1 to n
6a
if (NegCycle=false)
begin
(k)
(k1)
dij dij
(k1)
(k1)
(k1)
6b
if dij
dik
+ dij
then
(k)
(k1)
ij ij
else
(k)
dij
(k)
(k1)
(k)
(k1)
dij dik
(b) Indicate how strongly connected components are identied using DFS in the reverse graph GT .
(k1)
+ dij
ij kj
6c
if (i = j) and
< 0 then {NegCycle := true;N i}
7 if (NegCycle = true) then begin
return(N
)
(n)
repeat x := N N (N ); return(N ) until (x = N )
end
(c) In the strongly connected component algorithm we explore GT treating vertices in reverse than
the order that they nished when DFS run on G. Was the use of GT necessary? If we did a second
DFS on G treating vertices in reverse than the order that they nished when DFS run for the rst
time on G, would the strongly connected components be identied?
"OTXFS'PSUIFHSBQIPGUIJTQSPCMFN
BTFDPOE%'4USFBUJOHWFSUJDFTJOUIFPSEFSUIBUUIFZGJOJTIFEJOUIF
GJSTU %'4 EPFT QSPEVDF USFF DPNQPOFOUT UIBU DPSSFTQPOE UP UIF TUSPOHMZ DPOOFDUFE DPNQPOFOUT PG UIF
HSBQI)PXFWFS
UIJTJTOPUUSVFJOHFOFSBM
BTTIPXOJOUIFHSBQIPGUIFDPVOUFSFYBNQMFCFMPX
"OTXFS*UJTOPUUSVF BTTIPXOJOUIFDPVOUFSFYBNQMFCFMPX
"OTXFS
8FEP%'4BOEBTTJHOPOFPGUXPOVNCFSTUPOPEFT
TBZBOE5IFSPPU JFXIFOXFBSFTUBSUJOHBOFX
DPOOFDUFEDPNQPOFOU
JTBTTJHOFEFJUIFSPS&WFSZOPEFUIBUJTOPUBSPPUOPEFJO%'4JTBTTJHOFEB
OVNCFSUIBUJTEJGGFS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
JTDPOOFDUFE*G:&4
UIFOUIFSFJTBTQBOOJOHUSFFXIPTFNPTUFYQFOTJWFFEHFIBTDPTUBUNPTUC
UIVTUIFWBMVFPGUIFCPUUMFOFDLTQBOOJOHUSFFJTBUNPTUC*G/0
UIFOUIFSFJTOPTQBOOJOHUSFFBMMPG
XIPTFFEHFTIBWFDPTUBUNPTUC
UIVTFWFSZTQBOOJOHUSFFIBTBUMFBTUPOFFEHFXIPTFDPTUJTNPSFUIBOC
UIVTUIFWBMVFPGUIFCPUUMFOFDLTQBOOJOHUSFFJTTUSJDUMZHSFBUFSUIBOC
( 7
&
JTBEJSFDUFEBDZDMJDHSBQI5IVT
JO0 ]7]]&]
XFDBOGJOEBUPQPMPHJDBMTPSUJOHPG(-FUW
W
WO
CFUIFWFSUJDFTJOUPQPMPHJDBMMZTPSUFEPSEFS
UIVTBMMFEHFTHPGSPNMPXFSOVNCFSFEWFSUJDFTUPIJHIFS
OVNCFSFEWFSUJDFT5IVT
UIFSFJTBQBUIPGMFOHUI O
JGBOEPOMZJGUIFSFJTBQBUIPGMFOHUI O
TUBSUJOHFOEJOH<TJD>BUWO8FDPVMEVTFEZOBNJDQSPHSBNNJOHUPGJOEUIFMFOHUIPGUIFMPOHFTUQBUI
TUBSUJOHJOWBOEFOEJOHJOWO BTJO2VJ[
#VUJOUIJTDBTF
XFBSFMPPLJOHGPSBQBUIPGMFOHUI O
BOEUIBUDBOPOMZCFUIFQBUIWWWOWO8FDBOUIVTPOMZDIFDLJGWJWJJTBOFEHFJO&
GPSBMMJ O
Last Name: ............................. First Name: .............................. Email: ..............................
CS 3510 A, Spring 2015, Homework 7, 3/13/15 Due 3/25/15 in class
Page 6/6
Problem 6, Review Question, Strong Majority, 10 points
Let a(1), a(2), . . . , a(n) be an array of n integers. We say that an integer x is a strong majority
element if and only if |{i : a(i) = x}| > n2 . Argue that the linear algorithm below correctly decides
if the input array has a strong majority element.
Count:=0;
for i := 1 to n
if (Count= 0) then Candidate := a(i);
if (a(i) = Candidate) then (Count := Count + 1) else (Count := Count 1);
m := 0;
if (Count > 0) then for i := 1 to n
if (a(i) = Candidate) then m := m + 1;
if m > n2 then return(Candidate) else return(Nil)
Answer:
-FU$06/5 J
CFUIFWBMVFPG$06/5BGUFSUIFFYFDVUJPOPGUIFJUIJUFSBUJPO
BOE$06/5
-FU$"/%*%"5& J
CFUIFWBMVFPG$"/%*%"5&BGUFSUIFFYFDVUJPOPGUIFJUIJUFSBUJPO
3FBMJ[FUIBU$"/%*%"5&DIBOHFTWBMVFPOMZBGUFS$06/5IBTCFDPNF[FSPJOUIFQSFWJPVTTUFQ
JF
$"/%*%"5& J
$"/%*%"5& J
$06/5 J
$BTF*G$06/5 J
GPSBMMJO
UIFO
BUUIFFOEPGUIFBMHPSJUIN
$"/%*%"5&B
BOEB
JT
TUSPOHNBKPSJUZ
XIJDIXJMMCFWFSJGJFEJOUIFMBTUUFTU
$BTF-FUJJJLCFUIFJUFSBUJPOTBUUIFFOEPGXIJDI$06/5 JK
KL
$"/%*%"5& J
B
JTUIFWBMVFPGFYBDUMZIBMGPGUIFFMFNFOUT\B
B
B J
^
4JNJMBSMZ
$"/%*%"5& JK
B JK
JTUIFWBMVFPGFYBDUMZIBMGPGUIFFMFNFOUT
\B JK
B JK
B JK
^
KL
$BTFB*GJLO
UIFOUIFSFJTOPTUSPOHNBKPSJUZFMFNFOUJOBOZPGUIFTVCBSSBZT B
B J
B J
B
B JL
BLBO
UIFOUIFSFJTOPTUSPOHNBKPSJUZFMFNFOUJOUIFFOUJSFBSSBZ
BOE$06/5DPSSFDUMZGJOJTIFTXJUIUIFWBMVF
$BTFC*GJLO
UIFOUIFSFJTOPTUSPOHNBKPSJUZFMFNFOUJOBOZPGUIFTVCBSSBZT B
B J
B J
B
B JL
BL
5IVT
UIFSFJTOPTUSPOHNBKPSJUZFMFNFOUJOUIFTVCBSSBZ
B
B JL
IBTBTUSPOHNBKPSJUZFMFNFOU
UIFOUIJTNVTU
OFDFTTBSJMZNBOJGFTUJUTFMGJOUIFTVCBSSBZ B JL
BO
5IFSFGPSF
JGUIFFOUJSFBSSBZIBTBTUSPOH
NBKPSJUZFMFNFOU
UIFOUIJTWBMVFXJMMOFDFTTBSJMZCFUIFGJOBMWBMVFPG$"/%*%"5&5IJTXJMMCFWFSJGJFEJO
UIFMBTUWFSJGJDBUJPOTUFQ /PUFUIBUUIFMBTUWFSJGJDBUJPOTUFQJTOFDFTTBSZ
CFDBVTFBOFMFNFOUUIBU
NBOJGFTUTBT$"/%*%"5&GSPNUIFMBTUQBSUPGUIFBSSBZ B JL
BO
JTOPUOFDFTTBSJMZBTUSPOHNBKPSJUZ
FMFNFOUPGUIFFOUJSFBSSBZ$PVOUFSFYBNQMF
UIFGJOBM$"/%*%"5&JT
CVUUIJTJT
OPUBTUSPOHNBKPSJUZFMFNFOUPGUIFFOUJSFBSSBZ
=
(|V
|
1).
which implies |V |+x
2
We may now set x = |V |2 and realize that for this value of x we have, indeed, |V |+x
2
= (|V |1).
= Hn
Fact 1: cost(C) =
Proof of Fact 1.
cost(C) =
eS
price(e).
cost(S)
SC
SC
eX
f irst(e) = S
price(e)
price(e) .
eX
Let e1 , e2 , . . . , ek be the elements of S in the order in which they were covered when sets were
added to C by the APPROX-SET-COVER algorithm. Now |S | = k n and realize that:
)
)
= cost(S
, since e1 was the rst element of S that was covered, hence, at that
price(e1 ) cost(S
k
|S |
price(e2 )
that point
cost(S )
k1 ,
price(ek )
k(k1)
SCOPT
cost(S )
1
price(e) =
k
|ECROSS | |EIN |
price(ei )
We alse have
i=1
cost(S)
k
cost(S )
i=1
Hn cost(S)
price(e)
cost(S )
|S |(i1)
eS
|S |1
cost(S ) .
cost(S )
k(i1) , for
|S |
cost(S ) .
SCOPT
k (i 1)
= cost(S )
, by Fact 2 below
Thus
|E|
|ECROSS |
2
Finally, since the maximum cut may, in the best case, involve all edges in E (but, clearly, no more)
k
cost(S )
i=1
k (i 1)
1
1
1
+
+ ... + + 1
k k1
2
1
1
1
= cost(S ) 1 + + . . . +
+
2
k1 k
1
1
1
+
cost(S ) 1 + + . . . +
2
n1 n
= cost(S )Hn
= cost(S )
SCOPT eS
price(e)
eX
in the double summation above
= cost(C) , byF act1below.
OPT |E|
|E|
OPT
2
2
Problem 1: 30 points.
Consider the function Mystery dened below.
Mystery(n)
if n > 1 then begin
print(x);
Mystery(n 1);
Mystery(n 1);
Mystery(n 1);
end
If we call Mystery(n), where n is an integer n > 1, how many xs (as a function of n) does
call Mystery(n) print? Justify your answer/show your work (ie give recurrence and solve by
substitution.)
Answer:
(10 points for writing correct recursion and base case, 20 points for correct solution to recursion.)
We have to solve T (n) = 3T (n 1) + 1 with T (2) = 1. We will solve it by substitution:
Problem 2: 30 points.
Consider the function Mystery dened below.
Mystery(n)
if n > 1 then begin
print(x);
Mystery( n2 );
Mystery( n2 );
Mystery( n2 );
Mystery( n2 );
end
If we call Mystery(n), where n is an integer n > 2 and n is a power of 2, how many xs, as a
polynomial function of n, does call Mystery(n) print? Justify your answer/show your work (ie give
recurrence and solve by substitution.)
Answer:
(10 points for writing correct recursion and base case, 20 points for correct solution to recursion.)
We have to solve T (n) = 4T ( n2 ) + 1 with T (4) = T (22 ) = 5 (since 4 = 22 is the smallest integer for
which we may call Mystery. We will solve it by substitution:
MAX-IND-SET-for-TREE G(V, E)
IS := ;
while exists node u with a single neighbor v; (a leaf)
let u1 , u2 , . . . , uk be all the leafs incident to v;
IS := IS {u1 , u2 , . . . , uk };
remove {u1 , v}, {u2 , v}, . . ., {uk , v} from E;
if v is connected to other vertices x1 , . . . , xk that are non leaves then
remove {v, x1 }, . . ., {v, xk } from E;
if we ar left with a set of isolated vertices Y , then add them all to the ind set IS := IS Y ;
T (n)
3T (n 1) + 1
3(3T (n 2) + 1) + 1
32 T (n 2) + 3 + 1
3 (3T (n 3) + 1) + 3 + 1
33 T (n 3) + 32 + 3 + 1
T (n)
3k T (n k) + (3k1 + . . . + 32 + 3 + 1)
3k 1
3k 1
=
3k T (n k) +
= 3k T (n k) +
31
2
stop when n k = 2 k = n 2
3n2 1
n2
=
3
T (2) +
2
3n2 1
=
3n2 +
2
=
2 3n2 + 3n2 + 1 /2
=
=
=
3 3n2 1 /2
n1
1 /2
n
4T ( ) + 1
2
n
4 4T ( 2 ) + 1 + 1
2
n
42 T ( 2 ) + 4 + 1
2
n
)+1 +4+1
23
n
=
4 T ( 3 ) + 42 + 4 + 1
2
n
=
4k T ( k ) + 4k1 + . . . + 42 + 4 + 1
2
n
4k 1
=
4k T ( k ) +
2
41
n
4k 1
=
4k T ( k ) +
2
3
n
n
stop when
= 22 2 = 2k k = log2 n 2
2k
2
4log2 n2 1
=
4log2 n2 T (4) +
3
log
n
354 2
4log2 n
=
+
1 /3
42
42
=
42 T (
3
=
=
16 4log2 n
1 /3
42
4log2 n 1 /3 = 22 log2 n 1 /3 = n2 1 /3 .
So T (n) = n2 1 /3 (which is also consistent with 5 = T (4) = 42 1 /3 = 5.)
Problem 3: 30 points.
Suppose that you are given an array A with n distinct unsorted integer entries, where n = 2m for
some integer m 1. Let S(1) be the set containing the smallest n/2 elements of A.
For example, if n = 16 = 24 and the array A is (100, 40, 120, 20, 50, 30, 85, 80, 150, 200, 60, 70, 2, 3, 101, 102),
then S(1) = {40, 20, 50, 30, 60, 70, 2, 3}.
In general, for positive integer k with 1 k m, let S(k) be the set containing the smallest n/2k
elements of A.
For example, if n = 16 = 24 and the array A is (100, 40, 120, 20, 50, 30, 85, 80, 150, 200, 60, 70, 2, 3, 101, 102),
S(1) = {40, 20, 50, 30, 60, 70, 2, 3}, S(2) = {20, 30, 2, 3}, S(3) = {2, 3} and S(4) = {2}.
(a, 10 points) Argue that for any integer m 1, if n = 2m , then on input an array A of n distinct
unsorted integers, the elements of S(1) can be identied using O(n) comparisons.
(b, 20 points) Argue that for any integer m 1, if n = 2m , then on input an array A of n distinct
unsorted integers, the elements of all the sets S(k), for all 1 k m, can be identied and listed
using O(n) comparisons.
Answer:
Main Fact: There is a constant c such that KSelect with input any array X which has N elements
looking for the K-th element, 1 K N runs using at most cN comparisons. Stressing here that
c is independent on N .
Corollary: There is a constant c such that on input any arrayX which has N elements can nd
the N/2 smallest elements of A using at most (c + 1)N comparisons. To see this, use at most cN
comparisons to nd the middle element, and use an additional scanning of X to nd all elements
that are smaller than the middle element.
(a) It follows from the above Corollary that we can nd S(1) using at most (c + 1)n comparisons.
(b) We may now recurse on S(1) and nd S(2) using at most (c + 1)n/2, and so on, recurse on
S(k) to nd S(k + 1) using at most (c + 1)n/2k comparisons. We may thus nd all of the sets S(k),
for all 1 k m = log2 n, with number of comparisons bounded by:
Problem 4: 10 points.
Let S1 , . . . , Sn be an array of n distinct integers, where n > 2 is a positive even integer.
The pseudocode below rst updates two arrays A1 , . . . , A n2 and B1 , . . . , B n2 .
Then, a variable Alice is updated using the array A,
and a variable Bob is updated using array B.
Characterize the values that variables Alice and Bob have, at the end of the execution of the
pseudocode. Give a short justication of your answer.
m
n
(c + 1) k
2
k=1
= (c + 1)n
m
1
k=1
= (c + 1)n
= (c + 1)n
2k
1
1
= 2(c + 1)n 1
n
2
begin
Ai := max{Si , S n2 +i };
Bi := min{Si , S n2 +i };
end;
Alice := A1 ;
Bob := B1 ;
for i := 1 step 1 to n2
if (Ai > Alice) then Alice := Ai ;
for i := 1 step 1 to n2
if (Bi < Bob) then Bob := Bi ;
Answer: Alice is the value of the maximum element of the array S1 , ..., Sn and Bob is the value
of the minimum element of the array S1 , ..., Sn . We argue this in two steps:
First notice that A1 , ..., A n2 contains the maximum element of the array S1 , ..., Sn and B1 , ..., B n2
contains the value of the minimum element of the array S1 , ..., Sn . This is obvious, since for some
i, the maximum value element must have been placed in Ai := max{Si , S n2 +i }, while for some i ,
the minimum value element must have been placed in Bi := min{Si , S n2 +i }.
Next notice that Alice takes the value of the maximum element of A1 , ..., A n2 using the standard
method of scanning the array once from left to right and updating the max as necessary, and BoB
takes the value of the minimum element of B1 , ..., B n2 also using the standard method of scanning
the array once from left to right and updating the max as necessary.
T (n)
=
=
=
=
=
=
=
2k+1
1
2
1
2k+1
1
2
for i := 1 to
1
n
T( ) + n2
4
1
1
n 2
n
T( 2) +
+ n2
4
4
T(
n
)+
43
n
42
1
2
1
1
n
4
1
2k+1
=
=
2n 2 2
1
2
1
1
2
1
n
n
n 2
n 2
)+
+ ... 2
+
+ n2
4k
4k1
4
4
1
n
1
1
1
T( k ) + n2
2 +
1 + 1
k1 + . . . +
4
2
42
42
4
1
1
n
1
1
2
T( k) + n
+ ... + 2 + + 1
4
2k1
2
2
1 21k
1
n
T( k ) + n2
4
1 12
1
n
1
T ( k ) + 2n 2 1 k
4
2
1
n
1
4
=4 k =
4k
4
n
4k
1
2
T (4) + 2n 2 1 1
n2
1
2
2 + 2n 4 =
stop when
+ n2
T(
1
2
1
4
n
1
2
= 1
2k
n2
.
1
T (n)
2T
n
4
2 2T
n
42
22 T
22 2T
23 T
2k T
n
42
1
n
4
+ 2n
+ n2
n
43
+ n2
1
2
n
42
1
+ 2n 2
1
n
+ 3n 2
43
1
n
2
+
kn
4k
So Mystery prints n 2 1 +
1
log2 n
2
log2 4
2
Nil
if L(j) = Nil for all k j E
max{1 + L(j) : k j E and L(j) = Nil} if L(j) = Nil for some k j E
n
log2 n
stop when
= 1 n = 4k n = 22k 2k = log2 n k =
4k
2
log2 n
log2 n 1
2
2
T (1) +
=
2
n
2
1
1
log2 n
=
n2 +
n2
2
1
log2 n
=
n2 1 +
.
2
L(k) =
log2 1
2
=1
= 4 .)
(b) Let G(V, E) be an undirected connected weighted graph, where the weights of all edges are
distinct and positive: we > 0, for all e = {u, v} E. Let T be the mincost spanning tree of G.
Consider a graph G (V, E) on the same set of vertices as G and with the same set of edges as G.
However, for some c > 0, the weights of edges of G are we = we + c. Is T still a mincost spanning
tree of G ? If yes, then give a proof. If no, then give a counter-example.
Answer: Yes.
Every spanning tree has (n 1) edges. Thus, if we add the same c to the costs of all edges, the
cost of every spanning tree will increase by the same amount, namely c(n 1).
Last Name: ............................. First Name: .............................. Email: ..............................
CS 3510 A, Springl 2015, Quiz 3, Sample
Page 2/4
Problem 2, Maxcost Spanning Tree, 25 points.
Let G(V, E) be an undirected connected weighted graph, where the weights of all edges are distinct
and positive: we > 0, for all e = {u, v} E. Give an ecient algorithm that nds a spanning tree
of maximum cost (ie the sum of the weights of the edges of the spanning tree is maximized, over
all spanning trees of the graph.) Justify correctness and running time.
Answer:
STEP 1: Construct G on the same set of vertices and edges as G (thus the same set of spanning
trees), but with new costs of edges we = we .
STEP 2: T = MST in G using Kruskal or Prim (note: they work for arbitraty costs on the edges.)
STEP 3: Output T as maxcost spanning tree of G.
Correctness follows because Kruskal and Prims algorithms work for arbitraty costs on the edges,
these costs can be positive or negative.
Eciency follows because the main computational task in the above algorithm is one call of an
MST algorithm (in STEP 2).
2
1
6
A
5
G
H
3
6
A
2
E
H
3
6
Last Name: ............................. First Name: .............................. Email: ..............................
CS 3510 A, Springl 2015, Quiz 3, Sample
Page 4/4
Problem 4, Approximation Algorithm, 25 points.
Let G(V, E) be a directed unweighted graph, |V | = n. Let be a total ordering of V , that is, each
vertex v V is assigned a unique integer (v) in the range 1 to n.
Let BACK() be the number of edges directed from higher order vertices to lower order vertices:
BACK() = |{u v E : (u) > (v)}|.
Let OPT=max{BACK() : is a total ordering of V }.
Now let be an arbitrary total ordering of V , and let be the reverse ordering,
ie ((v) = 1 (v) = n), ((v) = 2 (v) = n 1), . . ., ((v) = n (v) = 1).
Argue that max{BACK(), BACK( )} OPT
2 .
Answer:
Let BACK = max{BACK(), BACK( )}.
Let FORWARD = |E| BACK, the rest of the edges.
By choice, BACK > FORWARD, thus BACK |E|
2 .
But |E| is the total number of edges of the graph, thus OPT |E|.
The last two bounds combined give
BACK
OPT
|E|
2
2
2
1
2
E
F
1
D
5
G
3
H
3
6
d(A)=0
d(B)=?
B
2
1
2
E
d(E)=?
F
1
5
d(C)=? 6
C
4
d(D)=?
D
G_1
G_2
d(F)=?
e_1
d(G)=?
Problem 1: 10 points.
Consider the function Mystery dened below.
Mystery(n)
if n > 1 then begin
print(xxx);
Mystery(n/2);
end
If we call Mystery(n), where n is an integer power of 3 and n > 1, how many xs (as an exact
function of n) does call Mystery(n) print? Justify your answer/show your work (solve recurrences
using the substitution method, do not use O() notation.)
Answer:5IFTNBMMFTUOUIBUJTBQPXFSPGJTO$MFBSMZ
.ZTUFSZ
QSJOUTYhTBOEDBMMT
.ZTUFSZ
.ZTUFSZ
*OUVSO
.ZTUFSZ
EPFTOPUHPJOUPUIFNBJOMPPQ
IFODFJUEPFTOPUQSJOUBOZ
YhTBOEJUEPFTOPUDBVTFBOZGVSUIFSSFDVSTJWFDBMMT5IVT.ZTUFSZ
8IFOOLXJUIL
.ZTUFSZ O
QSJOUTYhTBOEDBMMT.ZTUFSZ O
8FUIVTIBWFUPTPMWF5 O
5 O
XIFSFOJTBQPTJUJWFQPXFSPG
XJUI5
8FTPMWFUIJTSFDVSSFODFCZTVCTUJUVUJPO
XIFSFUIFHFOFSBMGPSNJT5 Y
5 Y
e_2
d(H)=?
T_1
T_2
e_k
CZFMFNFOUBSZDBMDVMBUJPOT
CZTVCTUJUVUJOHYO ?
UPUIFHFOFSBMGPSN
CZFMFNFOUBSZDBMDVMBUJPOT
CZHVFTTJOHUIFHFOFSBMUFSN
LOPXJOHUIBU5
XBOULTVDIUIBUO ?L
UIVT
1
A
CZTVCTUJUVUJOHYOUPUIFHFOFSBMGPSN
)DOVH&RXQWHUH[DPSOH
10
20
1
A
10
F
B
20
10
E
2
Last Name: ............................. First Name: .............................. Email: ..............................
CS 3510 A, Spring 2015, Practice Final Exam
Page 3/10
a((1)) > a((2)) < a((3)) > a((4)) < a((5)) . . . a((n 2)) > a((n 1)) < a((n)).
For example, on input 100, 20, 2, 5, 200, 50, 40, 201, 300
a wigly arrangement is 100, 20, 200, 2, 50, 5, 201, 40, 300
since 100 > 20 < 200 > 2 < 50 > 5 < 201 > 40 < 300.
Give an O(n) comparison algorithm that outputs a wigly arrangement of an unsorted input array
a(1), . . . , a(n) of n distimct integers, where n is odd. You may give a simple description of the
algorithm (no pseudocode.) You should include a short argument of correctness and running time.
Answer:4FFTPMVUJPOUP)81SPCMFN
Problem 7: 10 points.
Let G(V, E) be a line with weights on its vertices. The MaxIS is the problem of nding an
independent set of G whose sum of the weights of the vertices is maximized over all independent
sets of G. Give an example to show that the following heaviest rst greedy algorithm does not
always nd an independent set of maximum total weight.
Start with S equal to the empty set.
While some node remains in G
Pick a node vi of maximum weight
Add vi to S
Delete vi and its neighbors from G
End While
Return S
Answer:
. JOJUJBMMZUIFNBUDIJOHDPOUBJOTOPFEHFT
XIJMF&
MFUF\V
W^&CFBOBSCJUSBSZFEHF
..\F^ BEEUIFFEHFFUPUIFNBUDIJOH
$MBJN5IFBCPWFBMHPSJUINPCWJPVTMZSVOTJOQPMZOPNJBMUJNF
$MBJN$JTBWFSUFYDPWFS5IJTJTFBTZUPBSHVFCZDPOUSBEJDUJPO*OQBSUJDVMBS
JGJOUIFFOEPGUIF
BMHPSJUIN
UIFSFFYJTUTBOFEHFF\V
W^ &TVDIUIBUOFJUIFSVOPSWCFMPOHUP$
UIFOXIZXBTUIJTFEHF
OPUDPOTJEFSFEJOUIFXIJMFMPPQ
$MBJN*G015JTUIFTJ[FPGUIFTNBMMFTUWFSUFYPG(
UIFO]$]015
5PTFFUIJT
GJSTUOPUFUIBUGPSFWFSZFEHFJOF\V
W^.
BUMFBTUPOFPGVPSWNVTUCFMPOHUPUIF
TNBMMFTUWFSUFYDPWFS5IFOSFBMJ[FUIBU.DPOTJTUTPGFEHFTUIBUEPOPUTIBSFBOZPGUIFJSFOEQPJOUT
5IVT
015].]0OUIFPUIFSIBOE
]$]].]5IVT].]015