HW4_Solution - 1 CHAPTER 5 Problem 1 Since we can find the...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1. CHAPTER 5 Problem 1: Since we can find 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 find 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 Median-value (A11, 71], B[1, 71] , 71) if 71 : 1 then return min(A[n], B[ n;]) else if A171/2] > B[71/2] Median-value(A[1, 71/2],B [,71/2 71], 71/2) else if Jinn/2] < B[n/2 ] Median-value(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 divide-and-conquer 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 Card-Majority(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 Card-Majority(1,n). The correctness of the algorithm is captured by the following lemma: Lemma 1: The set S returned by Card-Majority(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, first 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—(Zlfll‘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 Card-Majority(t,m) and Card-Majority(m + 1, 2“), and hence is found in the subsequent check. For the running time, we notice that the running times satisfies 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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern