# 2Soln - Computer Science C73 Scarborough Campus University...

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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Computer Science C73 October 24, 2007 Scarborough Campus University of Toronto Solutions for Homework Assignment #2 Answer to Question 1. Let D 1 and D 2 be the two databases, each containing n elements. Recall that the median of 2 n (distinct) elements has exactly n elements larger than it, and exactly n − 1 elements smaller than it. Let Query ( t,D i ) return the element ranked t-th in the database D i . Let m = ⌊ n/ 2 ⌋ . By applying Query ( m,D 1 ) and Query ( m,D 2 ) we can find the values of the m-th elements, a 1 and a 2 , of D 1 and D 2 , respectively. Now we claim that by comparing a 1 to a 2 , we can eliminate as potential candidates for the overall median half of the elements of each database. To see why, suppose a 1 < a 2 (the case a 2 < a 1 is symmetric). Then, none of the first m elements in D 1 (i.e., the elements ranked 1 ..m in D 1 ) can be the overall median, because there are more than n larger elements in D 1 and D 2 — namely, the elements ranked m + 1 ..n in D 1 and the elements ranked m..n in D 2 . Similarly, none of the last m elements in D 2 (i.e., the elements ranked n − m + 1 ..n in D 2 can be the overall median, because there are more than n − 1 smaller elements in D 1 and D 2 — namely, the elements ranked 1 ..m in D 1 and the elements ranked 1 ..n − m in D 2 . Thus, with two queries we can eliminate from consideration half the elements of each database. Continuing in the same way, after ⌈ log 2 n ⌉ steps we will have eliminated from consideration all but one element in each database. At this point, we can retrieve the remaining two elements from the databases and return the minimum as the median. This divide-and-conquer algorithm is shown below as the procedure DB-Median ( f 1 ,f 2 ,k ). The inter- pretation of the parameters is that the search for the overall median of the elements in D 1 and D 2 has been narrowed to the elements ranked f 1 ..f 1 + k − 1 in D 1 and the elements ranked f 2 ..f 2 + k − 1 in D 2 . To find the overall median of the elements in the two databases, we call DB-Median (1 , 1 ,n )....
View Full Document

{[ snackBarMessage ]}

### Page1 / 3

2Soln - Computer Science C73 Scarborough Campus University...

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

View Full Document
Ask a homework question - tutors are online