Professional Documents
Culture Documents
7.3 Divide-and-Conquer Algorithm and Recurrence Relations
7.3 Divide-and-Conquer Algorithm and Recurrence Relations
to compare the min of the two sub-lists. So the rec. rel. is f(n)=2 f(n/2)+2
for even n.
Example 4 Fast Multiplication of Integers Divide-and-Conquer
technique will be used. Let a and b be 2n-bit integers. Split each integer into
two blocks, each block with n-bits. Suppose that a and b are integers with
binary expansions of length 2n.
Let
a
=
(a2n-1a2n-2a1a0)2,
b
=
(b2n-1b2n-2b1b0)2
and
where A1=(a2n-1an+1an)2,
A0=(an-1a1a0)2, B1=(b2n-1bn+1bn)2, B0=(bn-1b1b0)2. So we can write
This means that the multiplication of two 2n-bit integers can be carried out
using multiplication of three n-bit integers plus some shifts, subtractions and
additions. So if f(n) is the total number of bit operations needed to multiply
two n-bit integers, then f(2n)=3f(n)+Cn, where Cn is number of shifts,
subtractions and additions which is needed to carry out multiplication of
three n-bit integers: 3f(n).
Example 5 Fast Matrix Multiplication One can easily calculate that
multiplying two nxn matrices needs
multiplications and
.
.
Since
, we have
.
Applying this equation for f(n) can be used to estimate the size of functions
which satisfy the divide-and-conquer recursive relations.
Theorem 1
Let f be an increasing function that satisfies the rec. rel. f(n)=af(n/b)+c,
whenever n is divisible by b, where a, b N , b>1 and cR, c > 0. Then
f(n) is
if a>1 and
if a=1.
Furthermore, when
, k N, then f(n)=C1
C1=f(1)+c/(a-1) and C2= -c/(a-1).
+C2, where
. If
a=1, then
If n is not power of b, then we have
increasing,
Therefore, f(n) is
For
a>1,
if a=1.
first
let
N.
k
,
and
the power of b, then
because
Then
because
. If n is not
, for kN. Since f(n) is increasing,
. Therefore, f(n) is
if a>1.
Example 8 Estimate the number of comparisons needed to find the max and
the min elements of the list with n elements.
Solution: From example 2 we know that f(n)=2f(n/2)+2 for even n, if f(n) is
the number of comparisons needed to find a max and min in a list of size n.
Applying Theorem 1 with a=2, b=2, c=2, it is clear that f(n) is
=O(n).
Following is the more general version of Theorem 1 and it is also called
Master Theorem of complexity analysis of many divide-and-conquer
algorithms.
Theorem 2 MASTER THEOREM
Let f be an increasing function that satisfies the rec. rel.
, whenever
, k N, where a, b N , b>1
and c, dR, c > 0, d is nonnegative. Then f(n) is
if
and
if
if