CMPT 405 — Design and Analysis of Algorithms
Outline Solutions to Exercises on Divide and Coquer
1.
You are interested in analyzing some hardtoobtain data from two separate databases. Each database
contains n numerical values so there are
2
n
values total and you may assume that no two values are the
same. You would like to determine the median of this set of
2
n
values, which we will dene here to be the
n
th smallest value.
However, the only way you can access these values is through queries to the databases. In a single query,
you can specify a value
k
to one of the two databases, and the chosen database will return
k
th smallest
value that it contains. Since queries are expensive, you would like to compute the median using as few
queries as possible.
Give an algorithm that nds the median using at most
O
(log
n
)
queries.
Say
A
and
B
are the two databases and
A
(
i
)
,B
(
i
)
are
i
th smallest elements of
A,B
.
First, let us compare the medians of the two databases. Let
k
=
c
1
2
n
C
, then
A
(
k
)
and
B
(
k
)
are the medians of
the two databases. Suppose
A
(
k
)
< B
(
k
)
(the case when
A
(
k
)
> B
(
k
)
would be the same with interchange of
the role of
A
and
B
). The one can see that
B
(
k
)
is greater than the first
k
elements of
A
. Also
B
(
k
)
is always
greater than the first
k

1
element of
B
. Therefore
B
(
k
)
is at least
2
k
th element in the combined database.
Since
2
k
≥
n
, all elements that are greater than
B
(
k
)
are greater than the median and we can eliminate the
second part of the
B
database. Let
B
p
be the half of
B
(i.e. the first
k
elements of
B
).
Similarly, the first
f
1
2
n
F
elements of
A
are less than
B
(
k
)
, and thus, are less than the last
n

k
+ 1
elements of
B
. Also they are less than the last
c
1
2
n
C
elements of
A
. So they are less than at least
n

k
+ 1 +
c
1
2
n
C
=
n
+ 1
elements of the combine database. It means that they are less than the median and we can eliminate them as
well. Let
A
p
be the remaining part of
A
(i.e. the
b
f
1
2
n
F
+ 1;
n
B
segment of
A
).
Now we eleminate
f
1
2
n
F
elements that are less than the median, and the same number of elements that are greater
than the median. It is clear that the median of the remaining elements is the same as the median of the original
set. We can find a median in the remaining set using recursion for
A
p
and
B
p
. Note that we cannot delete
elements from the databases. However, we can access
i
th smallest elements of
A
p
and
B
p
: the
i
th smallest
element of
A
p
is
i
+
f
1
2
n
F
th smallest element of
A
, and the
i
th smallest element of
B
p
is the
i
th smallest
element of
B
.
Formally, the algorithm is the following. We write recursive function median
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.
 Fall '09
 Bulatov
 Algorithms, Databases, Recursion, Divide and conquer algorithm, significant inversions, internal local minimum

Click to edit the document details