Академический Документы
Профессиональный Документы
Культура Документы
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
Guess
T (n) O(n log n)
Assume
T ( n / 2 ) c n / 2 log n / 2
9
10
Recursion-tree method
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