T(n)=4T(n/2)+cn
=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)
steps the T(n/2k)
converges to T(1)
=4n2T(1/2)+(2n−1)cn
=n2.c′+n(2n−1)c
= O(n2)
4.3 The substitution method for solving
recurrences
4.3-1
Show that the solution of T(n)=T(n−1)+n is O(n2).
We guess (n)≤cn , 2
2,T(n)≤c(n−1) +n=cn2−2cn+c+n=cn +n(1−2c)+c≤cn ,
2 2 2
where the last step holds for c>1/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).
First, we guess �(�)≤��lg�T(n)≤cnlgn,
�(�)≤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.
Next, we guess �(�)≥�(�+�)lg(�+�)T(n)≥c(n+a)lg(n+a),
�(�)≥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,
where the last step holds for �≥1c≥1.
This time, the boundary condition is
�(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)
To show ΘΘ bound, separately show �O and ΩΩ bounds.
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,
where the last step holds for �>�c>d.
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,
where the last step holds for �>�d>c.
4.3-6
Show that the solution
to �(�)=2�(⌊�/2⌋+17)+�T(n)=2T(⌊n/2⌋+17)+n is �(�lg�)O(nlgn).
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.
We guess �(�)≤��log34T(n)≤cnlog34 first,
�(�)≤4�(�/3)log34+�=��log34+�.T(n)≤4c(n/3)log34+n=cnlog34+n.
We stuck here.
We guess �(�)≤��log34−��T(n)≤cnlog34−dn again,
�(�)≤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,
where the last step holds for �≥3d≥3.
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.
First, let's try the guess �(�)≤��2T(n)≤cn2. Then, we have
�(�)=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.
Alternatively, let us try the guess
�(�)≤��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,
where the last step holds for �≥1/2c≥1/2.
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�(�)+lg� let �=lg��(2�)=3�(2�/2)+��(�)=3�(�/
2)+�.T(n)T(2m)S(m)=3T(n)+lgn=3T(2m/2)+m=3S(m/2)+m. let m=lgn
Now we guess �(�)≤��lg3+��S(m)≤cmlg3+dm,
�(�)≤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)
Then we guess �(�)≥��lg3+��S(m)≥cmlg3+dm,
�(�)≥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)
Thus,
�(�)=Θ(�lg3)�(�)=Θ(lglg3�).S(m)T(n)=Θ(mlg3)=Θ(lglg3n).
Solve the following recurrence relation using recursion tree method-
T(n) = 2T(n/2) + n
The given recurrence relation shows-
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/2 into its 2 sub-problems and
then combining its solution is n/2 and so on.
This is illustrated through following recursion tree where each node represents
the cost of the corresponding sub-problem-
Step-02:
Determine cost of each level-
Cost of level-0 = n
Cost of level-1 = n/2 + n/2 = n
Cost of level-2 = n/4 + n/4 + n/4 + n/4 = n and so on.
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
Continuing in similar manner, we have-
Size of sub-problem at level-i = n/2i
Suppose at level-x (last level), size of sub-problem becomes 1. Then-
n / 2x = 1
2x = n
Taking log on both sides, we get-
xlog2 = logn
x = log2n
∴ Total number of levels in the recursion tree = log2n + 1
Step-04:
Determine number of nodes in the last level-
Level-0 has 20 nodes i.e. 1 node
1
Level-1 has 2 nodes i.e. 2 nodes
2
Level-2 has 2 nodes i.e. 4 nodes
Continuing in similar manner, we have-
Level-log2n has 2log2n nodes i.e. n nodes
Step-05:
Determine cost of last level-
Cost of last level = n x T(1) = θ(n)
Step-06:
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:
Solve the following recurrence relation using recursion tree method-
T(n) = T(n/5) + T(4n/5) + n
Solution-
Step-01:
Draw a recursion tree based on the given recurrence relation.
The given recurrence relation shows-
A problem of size n will get divided into 2 sub-problems- one of size n/5
and another of size 4n/5.
Then, sub-problem of size n/5 will get divided into 2 sub-problems- one
of size n/52 and another of size 4n/52.
On the other side, sub-problem of size 4n/5 will get divided into 2 sub-
problems- one of size 4n/52 and another of size 42n/52 and so on.
At the bottom most layer, the size of sub-problems will reduce to 1.
This is illustrated through following recursion tree-
The given recurrence relation shows-
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:
Determine cost of each level-
Cost of level-0 = n
Cost of level-1 = n/5 + 4n/5 = n
2 2 2 2 2
Cost of level-2 = n/5 + 4n/5 + 4n/5 + 4 n/5 = n
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
Continuing in similar manner, we have-
Size of sub-problem at level-i = (4/5)in
Suppose at level-x (last level), size of sub-problem becomes 1. Then-
(4/5)xn = 1
(4/5)x = 1/n
Taking log on both sides, we get-
xlog(4/5) = log(1/n)
x = log5/4n
∴ Total number of levels in the recursion tree = log5/4n + 1
Step-04:
Determine number of nodes in the last level-
Level-0 has 20 nodes i.e. 1 node
1
Level-1 has 2 nodes i.e. 2 nodes
2
Level-2 has 2 nodes i.e. 4 nodes
Continuing in similar manner, we have-
Level-log5/4n has 2log5/4n nodes
Step-05:
Determine cost of last level-
Cost of last level = 2log5/4n x T(1) = θ(2log5/4n) = θ(nlog5/42)
Step-06:
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
You are looking at this recurrence relation: lg lg n
Suppose T is a solution. Then we have T(1)=2T(1)+1=2+1, so =3. That is,
this recurrence relation has no solution.
x x
To solve 2x √ n≈1, note that this is equivalent to exp((logn)/2 )≈1, thus log n ≈ 2 ≈
2
and x≈log log n
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:
Solve the following recurrence relation using recursion tree method-
T(n) = 3T(n/4) + cn2
Solution-
Step-01:
Draw a recursion tree based on the given recurrence relation-
(Here, we have directly drawn a recursion tree representing the cost of sub
problems)
Step-02:
Determine cost of each level-
Cost of level-0 = cn2
2 2 2 2
Cost of level-1 = c(n/4) + c(n/4) + c(n/4) = (3/16)cn
2 2 2
Cost of level-2 = c(n/16) x 9 = (9/16 )cn
Step-03:
Determine total number of levels in the recursion tree-
Size of sub-problem at level-0 = n/40
1
Size of sub-problem at level-1 = n/4
2
Size of sub-problem at level-2 = n/4
Continuing in similar manner, we have-
Size of sub-problem at level-i = n/4i
Suppose at level-x (last level), size of sub-problem becomes 1. Then-
n/4x = 1
4x = n
Taking log on both sides, we get-
xlog4 = logn
x = log4n
∴ Total number of levels in the recursion tree = log4n + 1
Step-04:
Determine number of nodes in the last level-
Level-0 has 30 nodes i.e. 1 node
1
Level-1 has 3 nodes i.e. 3 nodes
2
Level-2 has 3 nodes i.e. 9 nodes
Continuing in similar manner, we have-
Level-log4n has 3log4n nodes i.e. nlog43 nodes
Step-05:
Determine cost of last level-
Cost of last level = nlog43 x T(1) = θ(nlog43)
Step-06:
Add costs of all the levels of the recursion tree and simplify the expression so
obtained in terms of asymptotic notation-
= cn2 { 1 + (3/16) + (3/16)2 + ……… } + θ(nlog43)
Now, { 1 + (3/16) + (3/16)2 + ……… } forms an infinite Geometric progression.
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