Вы находитесь на странице: 1из 17

Recurrences

Recurrences..
• when an algorithm contains a recursive call to
itself, its running time can often be described by a
recurrence.
• A recurrence is an equation or inequality that
describes a function in terms of its value on
smaller inputs.
• For example, the worst-case running time T (n) of
the MERGE-SORT procedure could be described
by the recurrence

2
There are three methods for solving recurrences
(1) Substitution method
We guess a bound and then use mathematical induction
to prove our guess correct.
(2) Recursion-tree method
converts the recurrence into a tree whose nodes
represent the costs incurred at various levels of the
recursion; we use techniques for bounding summations
to solve the recurrence.
(3) Master method
provides bounds for recurrences of the form
T (n) = aT (n/b) + f (n)

3
Master method
The master method provides a “cookbook”
method for solving recurrences of the form

where a ≥ 1 and b > 1 are constants and f


(n) is an asymptotically positive function.

The master method requires memorization


of three cases, but then the solution of many
recurrences can be determined quite easily,
often without pencil and paper.
4
5
Examples : Master Method / Theorem
Substitution method
The substitution method for solving recurrences
entails two steps:
1. Guess the form of the solution.
2. Use mathematical induction to find the
constants and show that the solution works.
• The name comes from the substitution of the
guessed answer for the function when the
inductive hypothesis is applied to smaller values.
• This method is powerful, but it obviously can be
applied only in cases when it is easy to guess the
form of the answer.
8
T ( n )  2T (  n / 2  )  n

T (1 )  1
(We may omit the initial condition later.)

Guess
T (n)  O(n log n)
Assume
T (  n / 2  )  c  n / 2  log  n / 2 

9
10
Recursion-tree method

Convert the recurrence into a tree:


– Each node represents the cost incurred at various levels
of recursion

– Sum up the costs of all levels

11
W(n) = 2W(n/2) + n2

12
• Subproblem size at level i is: n/2i
• Subproblem size hits 1 when 1 = n/2i  i = lgn
• Cost of the problem at level i = (n/2i)2 No. of nodes at level i = 2i
• Total cost:
lg n 1 2 lg n 1 i  i
n 1 1 1
W (n)   i  2 W (1)  n     n  n     O(n) n 2
lg n 2 2
 O ( n)  2 n 2
i 0 2 i 0  2  i 0  2  1 1
2
 W(n) = O(n2)

13
T (n)  3T ( n / 4)  (n 2 )

14
15
2 log 4 n 1
3 2 3 2 3
T (n)  cn 2  cn    cn  ...    cn 2  (nlog 3 )
4

16  16   16 

 
log 4 n 1 i
3 2
    cn   n log 4
3

i 0  16 
(3 / 16)log n  1 2
4

 cn  (nlog 3 ). 4

(3 / 16)  1

16
 
log 4 n 1 i
3 2
T ( n)     cn   n log 4 3

i 0  16 

 
 i
3 2
    cn   nlog 3 4

i  0  16 


1
1  (3 / 16)
cn 2   n log  4 3

16 2
 cn  (nlog 3 ) 4

13
 O(n 2 )
17

Вам также может понравиться