Professional Documents
Culture Documents
Chapter 2
1 if n=1
T(n) cT(n-1)+1 if n >
= 1
Recurrences
• The expression:
⎧
c n=
⎪
T (n) = 1
⎛
⎪ 2T ⎜ n ⎞ + n>
⎨ ⎪ ⎝ 2⎠ 1
cn
is a recurrence.
⎩
• Recurrence: an equation that describes a function
in terms of its value on smaller functions
Divide and Conquer
Divide: Break the problem into smaller sub-problems
Combine
Divide and Conquer: Sorting
How should we split the data?
B:
Merge
i J
L: 1 3 5 8 R: 2 4 6 7
B:
Merge
i J
L: 1 3 5 8 R: 2 4 6 7
B:
Merge
i J
L: 1 3 5 8 R: 2 4 6 7
B: 1
Merge
i J
L: 1 3 5 8 R: 2 4 6 7
B: 1
Merge
i J
L: 1 3 5 8 R: 2 4 6 7
B: 1 2
Merge
i J
L: 1 3 5 8 R: 2 4 6 7
B: 1 2
Merge
i J
L: 1 3 5 8 R: 2 4 6 7
B: 1 2 3
Merge
i J
L: 1 3 5 8 R: 2 4 6 7
B: 1 2 3
Merge
i J
L: 1 3 5 8 R: 2 4 6 7
B: 1 2 3 4
Merge
i J
L: 1 3 5 8 R: 2 4 6 7
B: 1 2 3 4
Merge
i J
L: 1 3 5 8 R: 2 4 6 7
B: 1 2 3 4 5
Merge
i J
L: 1 3 5 8 R: 2 4 6 7
B: 1 2 3 4 5
Merge
i J
L: 1 3 5 8 R: 2 4 6 7
B: 1 2 3 4 5 6
Merge i J
L: 1 3 5 8 R: 2 4 6 7
B: 1 2 3 4 5 6
Merge i J
L: 1 3 5 8 R: 2 4 6 7
B: 1 2 3 4 5 6 7
Merge i J
L: 1 3 5 8 R: 2 4 6 7
B: 1 2 3 4 5 6 7
Merge i J
L: 1 3 5 8 R: 2 4 6 7
B: 1 2 3 4 5 6 7 8
Merge
Does the algorithm terminate?
Merge
Is it correct?
Loop invariant: At the end of each iteration of the for loop of
lines 4-10 the B[1..k] contains the smallest k elements from L
and R in sorted order.
Merge
Is it correct?
Loop invariant: At the beginning of the for loop of lines 4-10 the
first k-1 elements of B are the smallest k-1 elements from L and
R in sorted order.
Merge
Running time?
?
?
?
?
?
?
?
?
?
Merge
Running time?
1
1
1
n+1
n
n
n
n
n
1
c if n is small
T ( n)
2T (n / 2) D(n) C (n) otherwise
c if n is small
T ( n)
2T (n / 2) D(n) C (n) otherwise
c if n is small
T ( n)
2T (n / 2) cn otherwise
Which is?
Merge-Sort
T ( n)
c if n is small
2T (n / 2) cn otherwise
cn
T(n/2) T(n/2)
Merge-Sort
T ( n)
c if n is small
2T (n / 2) cn otherwise
cn
cn/2 cn/2
cn/2 cn/2
cn/2 cn/2
c c c c c … c c c c c c
Merge-Sort
T ( n)
c if n is small
2T (n / 2) cn otherwise
cn cn
cn/2 cn/2 cn
c c c c c … c c c c c c cn
Merge-Sort
T ( n)
c if n is small
2T (n / 2) cn otherwise
cn cn
cn/2 cn/2 cn
c c c c c … c c c c c c cn
Depth?
Merge-Sort
We can calculate the depth, by determining when the
recursion gets to down to a small problem size, e.g. 1
d log 2 n
c if n is small
T ( n)
Merge-Sort 2T (n / 2) cn otherwise
Running time?
Each level costs cn
log n levels
T (n) aT (n / b) f (n)
Substitution method
Guess the form of the solution
Then prove it’s correct by induction
T ( n ) T ( n / 2) d
Guesses?
Substitution Method
Guess the form of the solution
Then prove it’s correct by induction
T (n) T (n / 2) d
Halves the input then constant amount of work
Similar to binary search:
Guess: O(log2 n)
Proof?
T (n) T (n / 2) d O(log 2 n) ?
Ideas?
Proof?
Proof by induction!
Assume it’s true for smaller T(k),
i.e. k < n
prove that it’s then true for
current T(n)
T (n) T (n / 2) d
Assume T(k) = O(log2 k) for all k < n
Show that T(n) = O(log2 n)
T (n) T (n / 2) d
c log 2 (n / 2) d from our inductive hypothesis
£c log 2 n - c log 2 2 + d
£ c log 2 n - c + d residual
c log 2 n
if c ≥ d
Base case?
For an inductive proof we need to show two things:
Assuming it’s true for k < n show it’s true for n
Show that it holds for some base case
if n is small
T ( n)
T (n / 2) d otherwise
T (n) T (n 1) n
Guess the solution?
At each iteration, does a linear amount of work
(i.e. iterate over the data) and reduces the size by
one at each step
O(n2)
c(n 2n 1) n
2
cn 2cn c n
2
residual
cn 2
if 2cn c n 0
2cn c n
c(2n 1) n
n
c
2n 1
1
which holds for any c
c ≥1 for n ≥1 2 1/ n
T (n) 2T (n / 2) n
Guess the solution?
Recurses into 2 sub-problems that are half the
size and performs some operation on all the
elements
O(n log n)
2cn 2 / 4 n
1 / 2cn 2 n
cn 2 (1 / 2cn 2 n) residual
cn 2
if
(1 / 2cn n) 0
2
overkill?
1 / 2cn 2 n 0
cn 2
T (n) 2T (n / 2) n
What if we guess wrong, e.g. O(n)?
T (n) 2T (n / 2) n
2cn / 2 n
cn n
cn
factor of n so we can
just roll it in?
T (n) 2T (n / 2) n
What if we guess wrong, e.g. O(n)?
T (n) 2T (n / 2) n
2cn / 2 log( n / 2) n
cn(log 2 n log 2 2) n
cn log 2 n cn n residual
cn log 2 n
if cn ≥ n, c > 1
Changing variables
T (n) 2T ( n ) log n
Guesses?
We can do a variable change: let m = log2 n
(or n = 2m)
T (2 m ) 2T (2 m / 2 ) m
Now, let S(m)=T(2m
S ( m ) 2 S ( m / 2) m
Changing variables
S(m) =2S(m / 2) + m
T (n) 3T (n / 4) n 2 cost
cn2 cn2
n
2
n
2
n
2 cn2(3/16)
c c c
4 4 4
n
2
n
2
n
2 cn2(3/16)
c c c
4 4 4
2 2 2 2 2 2
2
n n n
2 2
n n n n n n
c c c c c c c c c cn2(3/16)2
16 16 16 16 16 16 16 16 16
d
3 2
What is the cost at each level? cn
16
What is the depth of the tree?
At each level, the size of the data is divided by 4
n
d
1
4
n
log d 0
4
log n log 4 d 0
d log 4 log n
d log 4 n
T (n) 3T (n / 4) n 2
cn2
2 2 2
n n n
c c c
4 4 4
2 2 2 2 2 2
2
n n n
2 2
n n n n n n
c c c c c c c c c
16 16 16 16 16 16 16 16 16
T (1)
How many leaves are there?
How many leaves?
How many leaves are there in a complete
ternary(Three) tree of depth d?
3 3
d log4 n
Total cost
2 d 1
3 2 3 2 3
T (n) cn 2 cn cn ... cn 2 (3log4 n )
16 16 16
log4 n 1 i
3
cn 2 (3log4 n )
i 0 16
i
3
cn 2 (3log4 n ) 1
k 0 x k
i 0 16
1 x
1
cn 2 (3log4 n )
1 (3 / 16) let x = 3/16
16 2
cn (3log4 n )
13 ?
16 2
Total cost T (n) cn (3log4 n )
13
log4 3lo g4 n
log4 n
3 4
4log4 n log4 3
log4 n lo g4 3
4
n log4 3
16 2
T (n) cn ( n log4 3 )
13
T ( n) O ( n 2 )
Verify solution using substitution
T (n) 3T (n / 4) n 2
T(n/4) ≤ c(n/4)
T (n) 3T (n / 4) n 2
T (n) 3T (n / 4) n 2
3c(n / 4) 2 n 2
cn 2 3 / 16 n 2
cn 2
if
16
c
13
Master Method
Provides solutions to the recurrences of the form:
T (n) aT (n / b) f (n)
a = 16 n logb a n log4 16
b = 4
f(n) = n n2
2
is n O( n ) ?
is n (n 2 ) ? Case 1: Θ(n2)
is n (n 2 ) ?
T (n) T (n / 2) 2 n
a = 1 n logb a n log2 1
b = 2
f(n) = 2n n0
0 Case 3?
is 2 O(n ) ?
n
is 2 n / 2 c 2 n for c 1?
is 2 n (n 0 ) ?
is 2 n (n 0 ) ?
T ( n ) T ( n / 2) 2 n
is 2 n / 2 c 2 n for c 1?
Let c = 1/2
2 n / 2 (1 / 2)2 n
2 n / 2 2 12 n T(n) = Θ(2n)
2 n / 2 2 n 1
T (n) 2T (n / 2) n
if f (n) O(n logb a ) for 0, then T (n) (n logb a )
if f (n) (n logb a ), then T (n) (n logb a log n)
if f (n) (n logb a ) for 0 and af (n / b) cf (n) for c 1
then T (n) ( f (n))
a = 2 n logb a n log2 2
b = 2
f(n) = n n1
1
is n O(n ) ?
is n (n1 ) ? Case 2: Θ(n log n)
is n (n1 ) ?
T (n) 16T (n / 4) n!
if f (n) O(n logb a ) for 0, then T (n) (n logb a )
if f (n) (n logb a ), then T (n) (n logb a log n)
if f (n) (n logb a ) for 0 and af (n / b) cf (n) for c 1
then T (n) ( f (n))
a = 16 n logb a n log4 16
b = 4
f(n) = n! n2
2 Case 3
is n! O(n ) ?
is 16(n/ 4 )! cn! for c 1?
is n! ( n 2 ) ?
is n! (n 2 ) ?
T (n) 16T (n / 4) n!
if f (n) O(n logb a ) for 0, then T (n) (n logb a )
if f (n) (n logb a ), then T (n) (n logb a log n)
if f (n) (n logb a ) for 0 and af (n / b) cf (n) for c 1
then T (n) ( f (n))
Let c = 1/2
cn! 1 / 2n! T(n) = Θ(n!)
(n / 2)!
therefore,
a = 2 n logb a n log2 2
b = 2 log2 21 / 2
f(n) = logn n
n
1 / 2
is log n O(n )?
is log n (n1/ 2 ) ? Case 1: Θ( n )
is log n (n1/ 2 ) ?
T (n) 4T (n / 2) n
if f (n) O(n logb a ) for 0, then T (n) (n logb a )
if f (n) (n logb a ), then T (n) (n logb a log n)
if f (n) (n logb a ) for 0 and af (n / b) cf (n) for c 1
then T (n) ( f (n))
a = 4 n logb a n log2 4
b = 2
f(n) = n n2
2
is n O( n ) ?
is n (n 2 ) ? Case 1: Θ(n2)
is n (n 2 ) ?
Recurrences
T (n) 2T (n / 3) d T (n) 7T (n / 7) n
if f (n) O(n logb a ) for 0, then T (n) (n logb a )
if f (n) (n logb a ), then T (n) (n logb a log n)
if f ( n) (n logb a ) for 0 and af (n / b) cf (n) for c 1
then T (n) ( f (n))