hw4_Solution

hw4_Solution - 1. chapter 5 Problem 1: Since we can find...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
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- th 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 ,n ] ,B [1 ,n ] as the sorted array in increasing order and A [ k ] as the k- th element in the array. For brevity of our computation we assume that n = 2 s . First we compare A [ n/ 2] to B [ n/ 2]. Without loss of generality, assume that A [ n/ 2] < B [ n/ 2], then the elements A [1] ,...,A [ n/ 2] are smaller than n ele- ments, that is, A [ n/ 2] ,...,A [ n ] and B [ n/ 2] ,...,B [ n ]. Thus A [1] ,...,A [ n/ 2] cant be the median of the two databases. Similarly, B [ n/ 2] ,...,B [ n ] cant be the median of the two databases either. Note that m is the median value of A and B if and only if m is the median of A [ n/ 2] ,...,A [ n ] and B [1] ,...,B [ n/ 2](We delete the same number of numbers that are bigger than m and smaller than m ), that is, the n/ 2 smallest number of A [ n/ 2] ,...,A [ n ] and B [1] ,...,B [ n/ 2]. Hence the resulting algorithm is: Algorithm 1 Median-value( A [1 ,n ] ,B [1 ,n ] ,n ) if n = 1 then return min(A[n],B[n]); else if A [ n/ 2] > B [ n/ 2] Median-value( A [1 ,n/ 2] ,B [ n/ 2 ,n ] ,n/...
View Full Document

This note was uploaded on 02/20/2009 for the course CS 570 taught by Professor Shahriarshamsian during the Spring '08 term at USC.

Page1 / 3

hw4_Solution - 1. chapter 5 Problem 1: Since we can find...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online