Académique Documents
Professionnel Documents
Culture Documents
Divide-and-conquer paradigm
Fibonacci Sequences
Matrix multiplications
UVa 679 - Dropping Balls
subproblems.
2. Conquer the subproblems by solving
them recursively
3. Combine subproblem solutions.
MERGE SORT
1. Divide: Trivial.
2. Conquer: Recursively sort 2 subarrays.
3. Combine: Linear-time merge.
MERGE SORT
1. Divide: Trivial.
2. Conquer: Recursively sort 2 subarrays.
3. Combine: Linear-time merge.
T (n) 2T (n / 2) (n)
#subproblems
subproblems
size
12 15
12 15
12 15
12 15
12 15
12 15
subproblems
size
n /2
a a
if n is even;
a ( n 1)/2 ( n 1)/2
a
a if n is odd.
a
n
Recursive definition:
0
Fn 1
F F
n2
n 1
if n 0;
if n 1;
if n 2.
0 1 1 2 3 5 8 13 21 34
Bottom-up:
Compute F0 , F1 , F2 ,..., Fn in order, forming
each number by summing the two previous.
Running time: (n).
Naive recursive squaring:
n
Fn 1
Theorem:
Fn
Fn 1 1
Fn 1 1 0
Fn 1 Fn 1 1
Theorem:
Fn Fn 1 1 0
Algorithm: Recursive squaring.
Time: (log n)
Proof of theorem. (using induction on n.)
F2
Base (n 1) :
F1
F1 1 1
F0 1 0
Inductive step (n 2) :
Fn 1
F
n
Fn Fn
Fn 1 Fn 1
1 1
1 0
1 1
1
0
Fn 1 1 1
Fn 2 1 0
n 1
1 1
1 0
c
a
a
b22
2n
21
22
2 n 21
21 22
cn1 cn1 cnn an1 an 2 a11 bn1 bn 2
n
b1n
b2 n
b11
for i 1 to n
do for j 1 to n
do cij 0
for k 1 to n
do cij cij aik bkj
for i 1 to n
do for j 1 to n
do cij 0
for k 1 to n
do cij cij aik bkj
3
Running time = (n )
r s a b e
t u c d g
C A B
f
h
r ae bg
s af bh 8 mults of (n / 2) (n / 2) submatrices
t ce dg 4 adds of (n / 2) (n / 2) submatrices
u cf dh
T (n) 8 T (n / 2) (n )
#subproblems
logb a
log 2 8
subproblems
size
n CASE 1 T (n) (n )
r P5 P4 P2 P6
P3 (c d ) e
s P1 P2
P4 d ( g e)
t P3 P4
P5 (a d ) (e h)
P6 (b d ) ( g h)
P7 (a c) (e f )
u P5 P1 P3 P7
T (n) 7T (n / 2) (n )
n
log n a
log 2 7
2.81
lg 7
CASE 1 T (n) (n )
The number 2.81 may not seem much smaller than 3, but
because the different is in the exponent, the impact on
running time is significant. In fact, Strassens algorithm
beats the ordinary algorithm on todays machine for n 32 or
so.
implements in a program
2. Implements divide and conquer algorithm for
matrix multiplication
3. Implements Strassens algorithm using C/C++
How to modify the Strassens algorithm to multiply
mn?