Vous êtes sur la page 1sur 34

Abdul Munif

Divide-and-conquer paradigm
Fibonacci Sequences
Matrix multiplications
UVa 679 - Dropping Balls

1. Divide the problem (instance) into

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

work dividing and


combining

Find an element in a sorted array:


1. Divide: Check middle element.
2. Conquer: recursively search 1 subarray.
3. Combine: Trivial.
Example: Find 9
3

12 15

Find an element in a sorted array:


1. Divide: Check middle element.
2. Conquer: recursively search 1 subarray.
3. Combine: Trivial.
Example: Find 9
3

12 15

Find an element in a sorted array:


1. Divide: Check middle element.
2. Conquer: recursively search 1 subarray.
3. Combine: Trivial.
Example: Find 9
3

12 15

Find an element in a sorted array:


1. Divide: Check middle element.
2. Conquer: recursively search 1 subarray.
3. Combine: Trivial.
Example: Find 9
3

12 15

Find an element in a sorted array:


1. Divide: Check middle element.
2. Conquer: recursively search 1 subarray.
3. Combine: Trivial.
Example: Find 9
3

12 15

Find an element in a sorted array:


1. Divide: Check middle element.
2. Conquer: recursively search 1 subarray.
3. Combine: Trivial.
Example: Find 9
3

12 15

T (n) 1 T (n/ 2) (1)


#subproblems

subproblems
size

work dividing and


combining

Problem: Compute a , where n N .


Naive algorithm: (n).

Problem: Compute a , where n N .


Naive algorithm: (n).
Divide and conquer algorithm:
n /2

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 / 5 rounded to the nearest integer.


Recursive squaring: (lg n) time.

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

Input: A aij , B bij


i, j 1, 2,..., n .
Output: C cij A B
c11 c12 c1n a11 a12 a1n b11 b12
c
a
b
c

c
a

a
b22
2n
21
22
2 n 21
21 22



cn1 cn1 cnn an1 an 2 a11 bn1 bn 2
n

cij aik bkj


k 1

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 )

Idea: n x n matrix = 2 x 2 matrix of (n/2)x(n/2) submatrix:

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

work dividing and


combining

n CASE 1 T (n) (n )

Multiply 2x2 matrices with only 7 recursive mults.


P1 a ( f h)
P2 (a b) h

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

1. Divide: Partition A and B into (n/2)x(n/2)

submatrices. Form terms to be multiplied


using + and -.
2. Conquer: Perform 7 multiplications of
(n/2)x(n/2) submatrices recursively.
3. Combine: Form C using + and on
(n/2)x(n/2) submatrices.

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.

1. Write a pseudocode for powering n &

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?

A number of K balls are dropped one by one from the root


of a fully binary tree structure FBT. Each time the ball
being dropped first visits a non-terminal node.
It then keeps moving down, either follows the path of the
left subtree, or follows the path of the right subtree, until
it stops at one of the leaf nodes of FBT. To determine a
ball's moving direction a flag is set up in every nonterminal node with two values, either false or true.
Initially, all of the flags are false.
When visiting a non-terminal node if the flag's current
value at this node is false, then the ball will first switch
this flag's value, i.e., from the false to the true, and then
follow the left subtree of this node to keep moving down.
Otherwise, it will also switch this flag's value, i.e., from the
true to the false, but will follow the right subtree of this
node to keep moving down. Furthermore, all nodes of FBT
are sequentially numbered, starting at 1 with nodes on
depth 1, and then those on depth 2, and so on. Nodes on
any depth are numbered from left to right.

Vous aimerez peut-être aussi