Vous êtes sur la page 1sur 2

Chapter 4: Recurrences

Recurrences
Recurrence Trees
Master Method
Recurrences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Examples of Recurrences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
More Examples of Recurrences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Recursion Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Recursion Tree Continued . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Master Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Master Method Continued . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1
Recurrences
A recurrence describes a function in terms of its values on smaller inputs.
The general form of a recurrence is:
T(n) = aT(s(n)) + f(n)
where we assume T(1) (1).
T(n) = aT(s(n)) + f(n) can be interpreted as:
a = Number of subproblems
s(n) = Size of the subproblems
f(n) = Time to divide into subproblems and combine results
CS 5633 Analysis of Algorithms Chapter 4: Slide 2
Examples of Recurrences
Merge-Sort: T(n) = 2T(n/2) + c n
2 subproblems
n/2 = subproblem size
(n) time to merge results
Insertion-Sort: T(n) = T(n 1) + c n
I.e., rst sort n 1 elts., then insert nth elt.
1 subproblem
n 1 = subproblem size
(n) time to insert nth element
CS 5633 Analysis of Algorithms Chapter 4: Slide 3
More Examples of Recurrences
Bit-Multiply: T(n) = 3T(n/2) + c n
3 subproblems (3 multiplications)
n/2 = subproblem size
(n) time to add/sub. results
Strassens Alg.: T(n) = 7T(n/2) + c n
2
Matrices are n n
7 subproblems
n/2 n/2 = subproblem size
(n
2
) time to form submatrices and add/sub. results
CS 5633 Analysis of Algorithms Chapter 4: Slide 4
2
Recursion Trees
Recursion tree for T(n) = 2T(n/2) + n
T(n)
n
T

n
2

n
2

@
@
@

n
n/2 n/2

@
@
@ @
T

n
4

n
4

n
4

n
4

A
A

A
A
CS 5633 Analysis of Algorithms Chapter 4: Slide 5
Recursion Tree Continued
n
n/2 n/2

@
@
@ @
n/4 n/4 n/4 n/4

A
A
A

A
A
A

A
A
A
A
A
A
A
A
.
.
.
log
2
n
6
?
n
n
n
.
.
.
Total: (nlg n)
-
-
-
CS 5633 Analysis of Algorithms Chapter 4: Slide 6
3
Master Method
For recurrences of the form:
T(n) = aT(n/b) + f(n)
T(n) (f(n)) follows immediately.
T(n) (n
log
b
a
) because the recursion tree has a
log
b
n
= n
log
b
a
leaves.
Height = log
b
n.
Branching factor = a.
CS 5633 Analysis of Algorithms Chapter 4: Slide 7
Master Method Continued
For recurrences of the form:
T(n) = aT(n/b) + f(n)
T(n) (f(n))
if f(n) is poly. larger than n
log
b
a
(and is regular)
T(n) (n
log
b
a
)
if f(n) is poly. smaller than n
log
b
a
T(n) (n
log
b
a
lg n)
if f(n) (n
log
b
a
)
CS 5633 Analysis of Algorithms Chapter 4: Slide 8
4

Vous aimerez peut-être aussi