Recur tree
Recur tree
=4[4T(n/4)+cn/2]+cn
=16T(n/4)+3cn
=16[4T(n/8)+cn/4]+3cn
=64T(n/8)+7cn
=64[4T(n/16)+cn/8]+7cn
=256T(n/16)+15cn
......................
......................
After k substitutions:
=4k+1T(n/2k+1)+(2k+1−1)cn
Putting k=log(n)
as after log(n)
converges to T(1)
=4n2T(1/2)+(2n−1)cn
=n2.c′+n(2n−1)c
= O(n2)
We guess (n)≤cn , 2
4.3-2
Show that the solution of T(n)=T(⌈n/2⌉)+1 O(lgn).
We guess �(�)≤�lg(�−�)T(n)≤clg(n−a),
�(�)≤�lg(⌈�/2⌉−�)+1≤�lg((�+1)/2−�)+1=�lg((�+1−2�)/
2)+1=�lg(�+1−2�)−�lg2+1(�≥1)≤�lg(�+1−2�)
(�≥1)≤�lg(�−�),T(n)≤clg(⌈n/2⌉−a)+1≤clg((n+1)/2−a)
+1=clg((n+1−2a)/2)+1=clg(n+1−2a)−clg2+1≤clg(n+1−2a)≤clg(n−a),
(c≥1)(a≥1)
4.3-3
We saw that the solution of �(�)=2�(⌊�/2⌋)+�T(n)=2T(⌊n/2⌋)
+n is �(�lg�)O(nlgn). Show that the solution of this recurrence is
also Ω(�lg�)Ω(nlgn). Conclude that the solution is Θ(�lg�)Θ(nlgn).
�(�)≤2�⌊�/2⌋lg⌊�/2⌋+�≤��lg(�/
2)+�=��lg�−��lg2+�=��lg�+(1−�)�≤��lg�,T(n)
≤2c⌊n/2⌋lg⌊n/2⌋+n≤cnlg(n/
2)+n=cnlgn−cnlg2+n=cnlgn+(1−c)n≤cnlgn,
where the last step holds for �≥1c≥1.
�(�)≥2�(⌊�/2⌋+�)(lg(⌊�/2⌋+�)+�≥2�((�−1)/2+�)(lg((�−1)/
2+�))+�=2��−1+2�2lg�−1+2�2+�=�(�−1+2�)lg(�−1+2�)
−�(�−1+2�)lg2+�=�(�−1+2�)lg(�−1+2�)
+(1−�)�−(2�−1)�(0≤�<1,�≥(2�−1)�1−�)≥�(�−1+2�)lg(�−1
+2�)(�≥1)≥�(�+�)lg(�+�),T(n)≥2c(⌊n/2⌋+a)(lg(⌊n/2⌋+a)
+n≥2c((n−1)/2+a)(lg((n−1)/2+a))+n=2c2n−1+2alg2n−1+2a
+n=c(n−1+2a)lg(n−1+2a)−c(n−1+2a)lg2+n=c(n−1+2a)lg(n−1+2a)
+(1−c)n−(2a−1)c≥c(n−1+2a)lg(n−1+2a)≥c(n+a)lg(n+a),
(0≤c<1,n≥1−c(2a−1)c)(a≥1)
4.3-4
Show that by making a different inductive hyptohesis, we can overcome the difficulty with
the boundary condition �(1)=1T(1)=1 for recurrence (4.19)(4.19) without adjusting the
boundary conditions for the inductive proof.
We guess �(�)≤�lg�+�T(n)≤nlgn+n,
�(�)≤2(�⌊�/2⌋lg⌊�/2⌋+⌊�/2⌋)+�≤2�(�/2)lg(�/2)+2(�/
2)+�=��lg(�/
2)+2�=��lg�−��lg2+2�=��lg�+(2−�)�≤��lg�+�,T(n)
≤2(c⌊n/2⌋lg⌊n/2⌋+⌊n/2⌋)+n≤2c(n/2)lg(n/2)+2(n/2)+n=cnlg(n/
2)+2n=cnlgn−cnlg2+2n=cnlgn+(2−c)n≤cnlgn+n,
�(1)=1≤��lg�+�=0+1=1.T(1)=1≤cnlgn+n=0+1=1.
4.3-5
Show that Θ(�lg�)Θ(nlgn) is the solution to the "exact" recurrence (4.3)(4.3) for
merge sort.
The recurrence is
�(�)=�(⌈�/2⌉)+�(⌊�/2⌋)+Θ(�)(4.3)T(n)=T(⌈n/2⌉)+T(⌊n/2⌋)+Θ(n)
(4.3)
For �(�lg�)O(nlgn), we
guess �(�)≤�(�−2)lg(�−2)−2�T(n)≤c(n−2)lg(n−2)−2c,
2⌉−2)lg(⌈�/2⌉−2)+�(⌊�/2⌋−2)lg(⌊�/2⌋−2)+��≤�(�/2+1−2)lg(�/
�(�)≤�(⌈�/
2+1−2)−2�+�(�/2−2)lg(�/2−2)−2�+��≤�(�/2−1)lg(�/
2−1)+�(�/2−1)lg(�/
2−1)+��=��−22lg�−22+��−22lg�−22−4�+��=�(�−2)lg�−
22−4�+��=�(�−2)lg(�−2)−�(�−2)−4�+��=�(�−2)lg(�−2)+
(�−�)�+2�−4�≤�(�−2)lg(�−2)−2�,T(n)
≤c(⌈n/2⌉−2)lg(⌈n/2⌉−2)+c(⌊n/2⌋−2)lg(⌊n/2⌋−2)+dn≤c(n/2+1−2)lg(n/
2+1−2)−2c+c(n/2−2)lg(n/2−2)−2c+dn≤c(n/2−1)lg(n/2−1)+c(n/
2−1)lg(n/2−1)+dn=c2n−2lg2n−2+c2n−2lg2n−2−4c+dn=c(n−2)lg2n−2
−4c+dn=c(n−2)lg(n−2)−c(n−2)−4c+dn=c(n−2)lg(n−2)+
(d−c)n+2c−4c≤c(n−2)lg(n−2)−2c,
For Ω(�lg�)Ω(nlgn), we
guess �(�)≥�(�+2)lg(�+2)+2�T(n)≥c(n+2)lg(n+2)+2c,
2⌉+2)lg(⌈�/2⌉+2)+�(⌊�/2⌋+2)lg(⌊�/2⌋+2)+��≥�(�/2+2)lg(�/
�(�)≥�(⌈�/
2+2)+2�+�(�/2−1+2)lg(�/2−1+2)+2�+��≥�(�/2+1)lg(�/
2+1)+�(�/2+1)lg(�/
2+1)+4�+��≥��+22lg�+22+��+22lg�+22+4�+��=�(�+2)l
g�+22+4�+��=�(�+2)lg(�+2)−�(�+2)+4�+��=�(�+2)lg(
�+2)+(�−�)�−2�+4�≥�(�+2)lg(�+2)+2�,T(n)
≥c(⌈n/2⌉+2)lg(⌈n/2⌉+2)+c(⌊n/2⌋+2)lg(⌊n/2⌋+2)+dn≥c(n/2+2)lg(n/
2+2)+2c+c(n/2−1+2)lg(n/2−1+2)+2c+dn≥c(n/2+1)lg(n/2+1)+c(n/
2+1)lg(n/2+1)+4c+dn≥c2n+2lg2n+2+c2n+2lg2n+2
+4c+dn=c(n+2)lg2n+2
+4c+dn=c(n+2)lg(n+2)−c(n+2)+4c+dn=c(n+2)lg(n+2)+
(d−c)n−2c+4c≥c(n+2)lg(n+2)+2c,
We guess �(�)≤�(�−�)lg(�−�)T(n)≤c(n−a)lg(n−a),
�(�)≤2�(⌊�/2⌋+17−�)lg(⌊�/2⌋+17−�)+�≤2�(�/2+17−�)lg(�/
2+17−�)
+�=�(�+34−2�)lg�+34−2�2+�=�(�+34−2�)lg(�+34−2�)
−�(�+34−2�)+�(�>1,�>�0=�(�))≤�(�+34−2�)lg(�+34−2�)
(�≥34)≤�(�−�)lg(�−�).T(n)≤2c(⌊n/2⌋+17−a)lg(⌊n/2⌋+17−a)
+n≤2c(n/2+17−a)lg(n/2+17−a)+n=c(n+34−2a)lg2n+34−2a
+n=c(n+34−2a)lg(n+34−2a)−c(n+34−2a)
+n≤c(n+34−2a)lg(n+34−2a)≤c(n−a)lg(n−a).(c>1,n>n0=f(a))(a≥34)
4.3-7
Using the master method in Section 4.5, you can show that the solution to the
recurrence �(�)=4�(�/3)+�T(n)=4T(n/3)+n is �(�)=Θ(�log34)T(n)=
Θ(nlog34). Show that a substitution proof with the
assumption �(�)≤��log34T(n)≤cnlog34 fails. Then show how to subtract off a
lower-order term to make the substitution proof work.
�(�)≤4�(�/3)log34+�=��log34+�.T(n)≤4c(n/3)log34+n=cnlog34+n.
We stuck here.
�(�)≤4(�(�/3)log34−��/3)+�=4(��log34/4−��/
3)+�=��log34−43��+�≤��log34−��,T(n)≤4(c(n/3)log34−dn/
3)+n=4(cnlog34/4−dn/3)+n=cnlog34−34dn+n≤cnlog34−dn,
4.3-8
Using the master method in Section 4.5, you can show that the solution to the
recurrence �(�)=4�(�/2)+�T(n)=4T(n/2)+n is �(�)=Θ(�2)T(n)=Θ(n2
). Show that a substitution proof with the assumption �(�)≤��2T(n)≤cn2 fails.
Then show how to subtract off a lower-order term to make the substitution proof work.
�(�)=4�(�/2)+�≤4�(�/2)2+�=��2+�.T(n)=4T(n/2)+n≤4c(n/
2)2+n=cn2+n.
We can't proceed any further from the inequality above to
conclude �(�)≤��2T(n)≤cn2.
�(�)≤��2−��,T(n)≤cn2−cn,
which subtracts off a lower-order term. Now we have
�(�)=4�(�/2)+�=4(�(�/2)2−�(�/2))+�=4�(�/2)2−4�(�/
2)+�=��2+(1−2�)�≤��2,T(n)=4T(n/2)+n=4(c(n/2)2−c(n/2))
+n=4c(n/2)2−4c(n/2)+n=cn2+(1−2c)n≤cn2,
4.3-9
Solve the recurrence �(�)=3�(�)+log�T(n)=3T(n)+logn by making a change
of variables. Your solution should be asymptotically tight. Do not worry about whether
values are integral.
First,
�(�)≤3(�(�/2)lg3+�(�/2))
+�≤��lg3+(32�+1)�(�≤−2)≤��lg3+��.S(m)≤3(c(m/
2)lg3+d(m/2))+m≤cmlg3+(23d+1)m≤cmlg3+dm.(d≤−2)
�(�)=Θ(�lg3)�(�)=Θ(lglg3�).S(m)T(n)=Θ(mlg3)=Θ(lglg3n).
This is illustrated through following recursion tree where each node represents
the cost of the corresponding sub-problem-
Step-02:
Step-03:
Determine total number of levels in the recursion tree-
Size of sub-problem at level-0 = n/20
1
Size of sub-problem at level-1 = n/2
2
Size of sub-problem at level-2 = n/2
Step-04:
Step-05:
Add costs of all the levels of the recursion tree and simplify the expression so
obtained in terms of asymptotic notation-
= n x log2n + θ (n)
= nlog2n + θ (n)
= θ (nlog2n)
Problem-02:
Solution-
Step-01:
The cost of dividing a problem of size n into its 2 sub-problems and then
combining its solution is n.
The cost of dividing a problem of size n/5 into its 2 sub-problems and
then combining its solution is n/5.
The cost of dividing a problem of size 4n/5 into its 2 sub-problems and
then combining its solution is 4n/5 and so on.
This is illustrated through following recursion tree where each node represents
the cost of the corresponding sub-problem-
Step-02:
Step-03:
Determine total number of levels in the recursion tree. We will consider the
rightmost sub tree as it goes down to the deepest level-
Size of sub-problem at level-0 = (4/5)0n
1
Size of sub-problem at level-1 =(4/5) n
2
Size of sub-problem at level-2 =(4/5) n
Step-04:
Step-05:
Add costs of all the levels of the recursion tree and simplify the expression so
obtained in terms of asymptotic notation-
= nlog5/4n + θ(nlog5/42)
= θ(nlog5/4n)
Another example
T(n)=2T(sqrt(n))+1
T(1)=1
exp((logn)/2x)=1, which has no finite solution x if n>1. Hence the idea of the method
is to estimate the number x of steps needed to make 2x√ n smaller than any finite fixed
level >1, say, the smallest integer x such that n2x√ n <2. Rigorously speaking, this is
the smallest x such that 2x>log2n, thus x is indeed of order loglogn
x
Problem-03:
Solution-
Step-01:
Step-02:
Step-03:
Step-04:
Step-05:
Step-06:
Add costs of all the levels of the recursion tree and simplify the expression so
obtained in terms of asymptotic notation-
On solving, we get-
= (16/13)cn2 { 1 – (3/16)log4n } + θ(nlog43)
= (16/13)cn2 – (16/13)cn2 (3/16)log4n + θ(nlog43)
= O(n2)
Iteration method
Merge sort
Quick sort
Binary search