This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1. CHAPTER 5 Problem 1: Since we can ﬁnd the k — th value in each database by spec
ify a value k, we consider the two databases as two sorted arrays(Querying the
database by specifying k is equivalent to get the k — 15h value in the sorted ar
ray) and our problem is equivalent to ﬁnd the median value in two sorted ar
rays. We denote A[1, 71], B11, 71] as the sorted array in increasing order and AM as
the k — th element in the array. For brevity of our computation we assume that
n : 25. First we compare Jinn/2] to B[n/2]. Without loss of generality, assume
that A[71/2] < B[71/2], then the elements A11],...,A[71/2] are smaller than n ele
ments, that is,A[71/2],...,A[71] and B[71/2], ...,B[71]. Thus A[1],...,A[n/2] can’t be
the median of the two databases. Similarly, B[n/2], ..., B171] can’t be the median of
the two databases either. Note that 771 is the median value of A and B if and only if
771 is the median ofA1n/2], ..., A171] and B[1], ..., B[n/2] (We delete the same number
of numbers that are bigger than 771 and smaller than 771), that is, the 71/2 smallest
number of A[71/2], ..., A[71] and B[1], ...,B[71/2]. Hence the resulting algorithm is: Algorithm 1 Medianvalue (A11, 71], B[1, 71] , 71) if 71 : 1 then return min(A[n], B[ n;]) else if A171/2] > B[71/2] Medianvalue(A[1, 71/2],B [,71/2 71], 71/2)
else if Jinn/2] < B[n/2 ]
Medianvalue(A[71/2, 71], B[1, 71/2] , 71/2) end if For running time, assume that the time for the comparison of two numbers is
constant, namely, c, then T(71) : T(71/2) + c and thus T01) : clog 71. Hence our
algorithm is O(log 71). Problem 3: First off, we notice that we could easily solve this problem in (9(712)
invocations of the equivalence tester, simply by comparing each pair of cards. But
of course, we want a faster solution. The crucial observation here for a divideandconquer approach is that if we
divide the cards into two piles of size 71/ 2 each, and some card is equivalent at least
71/2 other cards, then it must be equivalent to at least 71/4 of the cards in one of
the piles. Hence we can use a recursive invocation to get (at most two) candidates,
and then test in linear time whether they appear 71/ 2 times overall. The resulting
recursive function is : Algorithm 1 CardMajority(l, 7) if .1 : 7 then return 1.;
else Date: Oct 21, 2006. 2 HW5 SOLUTION Let W Z 1511;
Let S : Card 7 Maj0r2'ty(l,m) U Card 7 Majority(m + 1, 2“)
for each 2' e 5' do Compare 2 with each card 3' : l, ..., 2“ H2 is equivalent to at most I—Tg‘i1or those 2" — 11—1— 1 cards, remove it from 3. end for
return S.
end if
Initially, we invoke CardMajority(1,n). The correctness of the algorithm is
captured by the following lemma: Lemma 1: The set S returned by CardMajority(l,r) is exactly the set of all
cards that appear at least 1 + 1%] times. Proof: We prove this by induction on ’1" — :1. 1f 2" : t, then the correctness is
obvious, as we return just the one card, which appears once. For larger values of 2" — .1, ﬁrst notice that we never return any cards that do not
appear as often as we claim in the lemma. That’s because we explicitly remove
form 3 in step 8 of the algorithm. Notice that this also proves that the set S
returned contains either 0 or 1 cards in each case, because no two distinct cards
can be equivalent to more than half of the cards each. So we only need to prove that indeed, we do return a card if it is equivalent to at
leat 1 + 1%] cards(including itself). Suppose that 2 is such a card. Then 2 must be
T—(Zlﬂl‘l equivalent to at least 1+ 17717711 among the cards :1, ...,m, or to at least 1+ [— cards among 722 + 1, ...,2“. For if it were not, then if could be equivalent to at most
a total of ["1741 + [#1 < 1%] cards between t, ..., 2“, a contradiction, Hence,
by Induction Hypothesis, this card 2 must be in at least one of CardMajority(t,m)
and CardMajority(m + 1, 2“), and hence is found in the subsequent check. For the running time, we notice that the running times satisﬁes T(k) : 2T(k/2)+ @(k), which we know to have solution T(k) : @(k log k). In particular, the running
time is @(n log 22) for the full problem. ...
View
Full Document
 Fall '06
 Shamsian
 Algorithms

Click to edit the document details