Functions
Functions
Functions
Chapter 4: Recurrences
Solving Recurrences
A recurrence is an equation that describes a
function in terms of itself by using smaller inputs
The expression:
c n 1
T ( n)
2T n cn n 1
2
Describes the running time for a function contains
recursion.
2
Solving Recurrences
Examples:
T(n) = 2T(n/2) + (n) T(n) = (n lg n)
T(n) = 2T(n/2) + n T(n) = (n lg n)
T(n) = 2T(n/2) + 17 + n T(n) = (n lg n)
3
Recurrence Examples
0 n0 0 n0
T ( n) T ( n)
n T (n 1) n0
c T (n 1) n 0
c n 1 c n 1
T ( n)
T ( n)
n n
2T c n 1 aT cn n 1
2 b
4
Substitution Method
The substitution method
“making a good guess method”
Guess the form of the answer, then
use induction to find the constants and show
that solution works
5
Substitution Method
T(n) = 2T(n/2) + n = O(n lg n)
Thus, we need to show that T(n) c n lg n with
an appropriate choice of c
Inductive hypothesis: assume
T(n/2) c (n/2) lg (n/2)
Substitute back into recurrence to show that
T(n) c n lg n follows, when c 1
T(n) = 2 T(n/2) + n
2 (c (n/2) lg (n/2)) + n
= cn lg(n/2) + n
= cn lg n – cn lg 2 + n
= cn lg n – cn + n
cn lg n for c 1
= O(n lg n) for c 1
6
Iteration Method
Iteration method:
8
0 n0
T ( n)
c T (n 1) n 0
T(n) = c + T(n-1) = c + c + T(n-2)
= 2c + T(n-2) = 2c + c + T(n-3)
= 3c + T(n-3) … kc + T(n-k)
= ck + T(n-k)
So far for n k we have
T(n) = ck + T(n-k)
i
=
i n k 1
T (n k ) for n k
i 1
i T ( 0) i 0 n
i 1 2
n 1
T ( n) n O (n 2 )
2
10
c n 1
n
T (n) 2T
c n 1
2
T(n) = 2 T(n/2) + c 1
= 2(2 T(n/2/2) + c) + c 2
= 22 T(n/22) + 2c + c
= 22(2 T(n/22/2) + c) + (22-1)c 3
= 23 T(n/23) + 4c + 3c
= 23 T(n/23) + (23-1)c
= 23(2 T(n/23/2) + c) + 7c 4
= 24 T(n/24) + (24-1)c
= …
= 2k T(n/2k) + (2k - 1)c k
11
c n 1
n
T (n) 2T
c n 1
2
So far for n k we have
T(n) = 2k T(n/2k) + (2k - 1)c
12