Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Changing variable
Recurrences
MERGE-SORT
Contains details:
T(n) = (1) if n=1 T( n/2 )+ T( n/2 )+ (n) if n>1 Ignore details, T(n) = 2T(n/2)+ (n).
T(n) = (1)
if n=1
By recursion tree.
1.
Use mathematical induction to find the constants and show that the solution works.
3
Suppose this bound holds for n/2 , i.e., T(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 (as long as c 1) T( n/2 ) c n/2 lg ( n/2 ).
Subtleties
Guess is correct, but induction proof not work. Problem is that inductive assumption not strong enough. Solution: revise the guess by subtracting a lower-order term. Example: T(n)=T( n/2 )+T( n/2 )+1.
Guess T(n)=O(n), i.e., T(n) cn for some c. However, T(n) c n/2 +c n/2 +1 =cn+1, which does not imply T(n) cn for any c. Attempting T(n)=O(n2) will work, but overkill. New guess T(n) cn b will work as long as b 1. (Prove it in an exact way).
6
Avoiding Pitfall
It is easy to guess T(n)=O(n) (i.e., T(n) cn) for T(n)=2T( n/2 )+n. And wrongly prove:
T(n) 2(c n/2 )+n
cn+n =O(n). wrongly !!!!
Problem is that it does not prove the exact form of T(n) cn.
7
Changing Variables
Suppose T(n)=2T(n)+lg n. Rename m=lg n. so T(2m)=2T(2m/2)+m. Domain transformation:
S(m)=T(2m), so S(m)=2S(m/2)+m. Which is similar to T(n)=2T(n/2)+n. So the solution is S(m)=O(m lg m). Changing back to T(n) from S(m), the solution is T(n) =T(2m)=S(m)=O(m lg m)=O(lg n lg lg n).
9
Particularly suitable for divide-and-conquer recurrence. Best used to generate a good guess, tolerating sloppiness. If trying carefully to draw the recursion-tree and compute cost, then used as direct proof.
10
cn2
c(n/4)2 c(n/4)2
(a)
(b)
c(n/4)
2
cn2
c(n/4)2
(c)
cn2 (3/16)cn2
c(n/4)2
log 4n
(3/16)2cn2
T(1)T(1)T(1)
3log4n= nlog 43
T(1)T(1)T(1)
(nlog 43)
(d)
Total O(n2)
11
Solution to T(n)=3T(
n/4 )+(n2)
The height is log 4n, #leaf nodes = 3log 4n= nlog 43. Leaf node cost: T(1). Total cost T(n)=cn2+(3/16) cn2+(3/16)2 cn2+ +(3/16)log 4n-1 cn2+ (nlog 43) =(1+3/16+(3/16)2+ +(3/16)log 4n-1) cn2 + (nlog 43) <(1+3/16+(3/16)2+ +(3/16)m+ ) cn2 + (nlog 43) =(1/(1-3/16)) cn2 + (nlog 43) =16/13cn2 + (nlog 43) =O(n2).
12
Show T(n) dn2 for some d. T(n) 3(d ( n/4 )2) +cn2 3(d (n/4)2) +cn2 =3/16(dn2) +cn2 dn2, as long as d (16/13)c.
13
14
15
Master Method/Theorem
Theorem 4.1 (page 73)
for T(n) = aT(n/b)+f(n), n/b may be n/b or n/b . where a 1, b>1 are positive integers, f(n) be a nonnegative function. 1. If f(n)=O(nlogba-) for some >0, then T(n)= (nlogba). 2. If f(n)= (nlogba), then T(n)= (nlogba lg n). 3. If f(n)=(nlogba+) for some >0, and if af(n/b) cf(n) for some c<1 and all sufficiently large n, then T(n)= (f(n)).
16
T(n) = T(2n/3)+1
logba
Note: 1. for case 3, the regularity also must hold. 2. if f(n) is lg n smaller, then fall in gap in 1 and 2 3. if f(n) is lg n larger, then fall in gap in 3 and 2 4. if f(n)=(nlogbalgkn), then T(n)=(nlogbalgk+1n). (as exercise)
21
Proof:
By iterating the recurrence By recursion tree (See figure 4.3)
22
23
Lemma 4.3:
Let a 1, b>1, f(n) be a nonnegative function defined on exact power of b, then g(n)=
logbn-1 j=0
1. 2. 3.
If f(n)=O(nlogba-) for some >0, then g(n)= O(nlogba). If f(n)= (nlogba), then g(n)= (nlogba lg n). If f(n)= (nlogba+) for some >0 and if af(n/b) cf(n) for some c<1 and all sufficiently large n b, then g(n)= (f(n)).
24
ajf(n/bj) =O(
logbn-1
logbn-1 j=0
aj(n /bj)logba- )
j a j(b-)j)) /(a
= O(nlogba- = O(nlogba-
a j/(blogba-)j ) = O(nlogba-
logbn-1 j=0
ajf(n/bj) = (
logbn-1 j=0
logbn-1 j=0
aj(n /bj)logba)
logbn-1
= (nlogba
aj/(blogba)j ) = (nlogba
1)
j=0
26
a f(n/b )
j j
logbn-1 j=0
Lemma 4.4:
for T(n) = (1) if n=1 aT(n/b)+f(n) if n=bk for k 1 where a 1, b>1, f(n) be a nonnegative function, 1. If f(n)=O(nlogba-) for some >0, then T(n)= (nlogba). 2. If f(n)= (nlogba), then T(n)= (nlogba lg n). 3. If f(n)=(nlogba+) for some >0, and if af(n/b) cf(n) for some c<1 and all sufficiently large n, then T(n)= (f(n)).
28
29
Since n/b n/b, and n/b n/b, upper bound for floors and lower bound for ceiling is held. So prove upper bound for ceilings (similar for lower bound for floors).
30
Upper bound of proof for T(n)=aT( n/b ) +f(n) consider sequence n, n/b , n/b /b ,
n/b /b /b ,
= nj-1/b
31
n/b )+f(n)
32
Thus similar to Lemma 4.3 and 4.4, the upper bound is proven.
33
34
Summary
Recurrences and their bounds
Substitution Recursion tree Master theorem. Proof of subtleties Recurrences that Master theorem does not apply to.
35