4.4 The recursion-tree method for solving recurrences

4.4-1

First let's create a recursion tree for the recurrence T(n)=3T(n2)+n and assume that n is an exact power of 2.

Alt text

Each level has three times more nodes than the level above, so the number of nodes at depth i is 3i. And each node at depth i, for i=0,1,2,,lgn1, has a cost of n2i. So the total cost over all nodes at depth i, is 3in2i=(32)in. The bottom level, at depth lgn, has 3lgn=nlg3 nodes, each contributing cost T(1), for a total cost of nlg3T(1), which is Θ(nlg3). So:

T(n)=i=0lgn1(32)in+Θ(nlg3)=n1(32)lgn132+Θ(nlg3)=2n3lgn2lgn2lgn+Θ(nlg3)=2n3lgn2lgnn+Θ(nlg3)=2(3lgn2lgn)+Θ(nlg3)=2(nlg3n)+Θ(nlg3)<2nlg3+Θ(nlg3)=O(nlg3)

Thus, we have derived a guess of T(n)=O(nlg3) for our original recurrence. Now let's use the substitution method to verify that our guess was correct. We want to show that T(n)cnlg3 for some constant c>0. So:

T(n)=3T(n2)+n3cn2lg3+n3c(n2)lg3+n=3c(n2)lg3+n=3cnlg32lg3+n=3cnlg33+n=cnlg3+n

But cnlg3+n>cnlg3, so we need to try another guess. Let's try T(n)cnlg32n3. So:

T(n)=3T(n2)+n3(cn2lg3n3)+n3(c(n2)lg3n3)+n=3c(n2)lg3=cnlg3

4.4-2

First let's create a recursion tree for the recurrence T(n)=T(n2)+n2 and assume that n is an exact power of 2.

Alt text

The number of nodes at depth i is 1. And each node at depth i, for i=0,1,2,,lgn1, has a cost of n24i. So the total cost over all nodes at depth i, is n24i. The bottom level, at depth lgn, has 1 node, which contributing cost T(1), for a total cost of T(1), which is Θ(1). So:

T(n)=i=0lgn1n24i+Θ(1)<i=0n24i+Θ(1)=1114n2+Θ(1)=43n2+Θ(1)=O(n2)

Thus, we have derived a guess of T(n)=O(n2) for our original recurrence. Now let's use the substitution method to verify that our guess was correct. We want to show that T(n)cn2 for some constant c>0. So:

T(n)=T(n2)+n2c(n2)2+n2=(c4+1)n2cn2

where the last step holds as long as c43.

4.4-3

When n is large, the difference between n2+2 and n2 not that large, so it's also a sloppiness that we can tolerate. Then let's create a recursion tree for the recurrence T(n)=4T(n2)+n and assume that n is an exact power of 2.

Alt text

Each level has four times more nodes than the level above, so the number of nodes at depth i is 4i. And each node at depth i, for i=0,1,2,,lgn1, has a cost of n2i. So the total cost over all nodes at depth i, is 4in2i=2in. The bottom level, at depth lgn, has 4lgn=n2 nodes, each contributing cost T(1), for a total cost of n2T(1), which is Θ(n2). So:

T(n)=i=0lgn12in+Θ(n2)=n12lgn12+Θ(n2)=n(n1)+Θ(n2)=n2n+Θ(n2)=O(n2)

Thus, we have derived a guess of T(n)=O(n2) for our original recurrence. Now let's use the substitution method to verify that our guess was correct. We want to show that T(n)cn2 for some constant c>0. So:

T(n)=4T(n2+2)+n4c(n2+2)2+n=4c(n24+2n+4)+n=cn2+8cn+16c+n=cn2+(8c+1)n+16c

But cn2+(8c+1)n+16c>cn2, so we need to try another guess, let's try T(n)cn25n. So:

T(n)=4T(n2+2)+n4c((n2+2)25(n2+2))+n=4c(n24+2n+45n210)+n=4c(n24n26)+n=cn2+(12c)n24c<cn2

where the last step holds as long as c12.

4.4-4

First let's create a recursion tree for the recurrence T(n)=2T(n1)+1.

Alt text

Each level has two times more nodes than the level above, so the number of nodes at depth i is 2i. And each node at depth i, for i=0,1,2,,n11, has a cost of 1. So the total cost over all nodes at depth i, is 2i. The bottom level, at depth n, has 2n1 nodes, each contributing cost T(1), for a total cost of 2n1T(1), which is Θ(2n). So:

T(n)=i=0n112i+Θ(2n)=12n112+Θ(2n)=2n11+Θ(2n)=O(2n)

Thus, we have derived a guess of T(n)=O(2n) for our original recurrence. Now let's use the substitution method to verify that our guess was correct. We want to show that T(n)c2n for some constant c>0. So:

T(n)=2T(n1)+12c2n1+1=c2n+1

But c2n+1>c2n, so we need to try another guess. Lets try T(n)c2n1. So:

T(n)=2T(n1)+12(c2n11)+1=c2n1<c2n

4.4-5

4.4-6

First let's create a recursion tree for the recurrence T(n)=T(n3)+T(2n3) and assume that n is an exact power of 3.

Alt text

Each level has 2 times more nodes than the level above, so the number of nodes at depth i is 2i. But in this question, not all leaves reach at the bottom at the same time. The left most branch reaches the bottom first. So when the left most branch reaches the bottom, we assume other nodes also reach the bottom. Then the depth of recursion tree is log3n.

The total cost over all nodes at depth i, is cn and the cost at bottom is also cn So:

T(n)i=0log3n1cn+cn=cnlog3n+cn=clg3nlgn+cn=Θ(nlgn)

But remember we've cut some branches of the recursion tree, so T(n)=Ω(nlgn).

4.4-7

When n is large, the difference between n2 and n2 not that large, so it's also a sloppiness that we can tolerate. Then let's create a recursion tree for the recurrence T(n)=4T(n2)+cn and assume that n is an exact power of 2.

Alt text

Each level has 4 times more nodes than the level above, so the number of nodes at depth i is 4i. And each node at depth i, for i=0,1,2,,lgn1, has a cost of cn2i. So the total cost over all nodes at depth i, is 4icn2i=c2in. The bottom level, at depth lgn, has 4lgn=n2 nodes, each contributing cost T(1), for a total cost of n2T(1), which is Θ(n2). So:

T(n)=i=0lgn1c2in+Θ(n2)=cn12lgn12+Θ(n2)=Θ(n2)

Thus, we have derived a guess of T(n)=O(n2) for our original recurrence. Now let's use the substitution method to verify that our guess was correct. We want to show that T(n)dn2 for some constant d>0. So:

T(n)=4T(n2)+cn4T(n2)+cn4d(n2)2+cn=dn2+cn

But dn2+cn>dn2, so let's try another guess, let's try T(n)dn2dn.

T(n)=4T(n2)+cn4T(n2)+cn4d((n2)2n2)+cn=dn2+(c2d)ndn2

where the last step holds as long as dc2.

So T(n)=O(n2). Then we want to show that T(n)dn2 for some constant d>0. So:

T(n)=4T(n2)+cn4T(n(21)2)+cn=4T(n12)+cn4d(n12)2+cn=dn2+(c2d)n+d>dn2

where the last step holds as long as d<c2.

So T(n)=Ω(n2), so T(n)=Θ(n2).

4.4-8

First let's create a recursion tree for the recurrence T(n)=T(na)+T(a)+cn and assume that n - 1 is divisible by a.

Alt text

The total cost over all nodes at depth i, for i=0,1,2,,n1a1, is c(n(i1)a), for i1. The bottom level, at depth n1a, the total cost is T(1)+ca, which is Θ(a). So:

T(n)=cn+i=1n1a1c(n(i1)a)+Θ(a)=cn+i=1n1a1cni=1n1a1(i1)ca+Θ(a)=n1acnn22a3na+3a+2a2+12ac=n2+3na3a2a212ac=Θ(n2)

Thus, we have derived a guess of T(n)=Θ(n2) for our original recurrence. Now let's use the substitution method to verify that our guess was correct. We want to show that T(n)dn2 for some constant d>0. So:

T(n)=T(na)+T(a)+cnd(na)2+da2+cn=dn2+(cad)n+(2a2an)ddn2

where the last step holds as long as dca and n2a.

So T(n)=O(n2).

Then we want to show that T(n)dn2 for some constant d>0. So:

T(n)=T(na)+T(a)+cnd(na)2+da2+cn=dn2+(c2ad)n+2a2d>dn2

where the last step holds as long as d<c2a.

So T(n)=Ω(n2). So T(n)=Θ(n2).

4.4-9

First let's create a recursion tree for the recurrence T(n)=T(αn)+T((1α)n)+cn.

Alt text

So we can see not each branch reaches at the bottom at the same time, it might be the left most branch reaches at the bottom first, or the right most branch reaches at the bottom first. It depends on which is smaller, α or 1α. Let α1α, so we get α12. That is, when 0<α12, the left most branch reaches at the bottom first, when 12<α<1, the right most branch reaches at the bottom first.

If 0<α12, when the left most branch reaches at the bottom, let's assume other branches also reach at the bottom, so the total cost over all nodes at depth i, for i=0,1,2,...,logα1n1, is cn. The cost at the bottom is also cn. So:

T(n)=T(αn)+T((1α)n)+cni=0logα1n1cn+cn=cn(logα1n)+cn=cnlgnlg1α+cn>cnlgnlg1α=Ω(nlgn)

Thus, we have derived a guess of T(n)=Ω(nlgn) for our original recurrence. Now let's use the substitution method to verify that our guess was correct. We want to show that T(n)dnlgn for some constant d>0. So:

T(n)=T(αn)+T((1α)n)+cndαnlg(αn)+d(1α)nlg((1α)n)+cn=dαn(lgα+n)+d(1α)n(lg(1α)+lgn)+cn=dnlgn+dαlgαn+d(1α)lg(1α)n+cndnlgn+dαlgαn+dαlgαn+cn(0<α12)=dnlgn+(c+2dαlgα)ndnlgn

where the last step holds as long as dc2αlg1α.

Now let's check the right most branch. When it reaches at the bottom, other branches have already reached at the bottom, if the tree depth is k, let's assume other branches become T(1) at depth k - 1.

Each level has two times more nodes than the level above, so the number of nodes at depth i is 2i. And total cost over all nodes at depth i, for i=0,1,2,...,log1α1n, is cn. The cost at the bottom is T(1). So:

T(n)=T(αn)+T((1α)n)+cni=0log1α1n1cn+Θ(1)=cn(log1α1n)+Θ(1)=cnlgnlg11α+Θ(1)<cnlgnlg11α+nlgn=O(nlgn)

Thus, we have derived a guess of T(n)=O(nlgn) for our original recurrence. Now let's use the substitution method to veify that our guess was correct. We want to show that T(n)dnlgn for some constant d>0. So:

T(n)=T(αn)+T((1α)n)+cndαnlg(αn)+d(1α)nlg((1α)n)+cn=dαn(lgα+n)+d(1α)n(lg(1α)+lgn)+cn=dnlgn+dαlgαn+d(1α)lg(1α)n+cndnlgn+d(1α)lg(1α)n+d(1α)lg(1α)n+cn(0<α12)=dnlgn+(c+2d(1α)lg(1α))ndnlgn

where the last step holds as long as dc2(1α)lg11α.

So we proved T(n)=Ω(nlgn) and T(n)=O(nlgn). So T(n)=Θ(nlgn) when 0<α12.

Then let's check another case, when 12<α<1. In this case, the right most branch reaches at the bottom first. Let's assume other branches also reach at the bottom, so the total cost over all nodes at depth i, for i=0,1,2,...,log1α1n, is cn. The cost at the bottom is cn. So:

T(n)=T(αn)+T((1α)n)+cni=0log1α1n1cn+cn=cn(log1α1n)+cn=cnlgnlg11α+cn>cnlgnlg11α=Ω(nlgn)

Thus, we have derived a guess of T(n)=Ω(nlgn) for our original recurrence. Now let's use the substitution method to verify that our guess was correct. We want to show that T(n)dnlgn for some constant d>0. So:

T(n)=T(αn)+T((1α)n)+cndαnlg(αn)+d(1α)nlg((1α)n)+cn=dαn(lgα+n)+d(1α)n(lg(1α)+lgn)+cn=dnlgn+dαlgαn+d(1α)lg(1α)n+cn>dnlgn+d(1α)lg(1α)n+d(1α)lg(1α)n+cn(12<α<1)=dnlgn+(c+2d(1α)lg(1α))ndnlgn

where the last step holds as long as dc2(1α)lg11α.

Now let's check the left most branch. When it reaches at the bottom, other branches have already reached at the bottom, if the tree depth is k, let's assume other branches become T(1) at depth k - 1.

Each level has two times more nodes than the level above, so the number of nodes at depth i is 2i. And total cost over all nodes at depth i, for i=0,1,2,...,logα1n1, is cn. The cost at the bottom is T(1). So:

T(n)=T(αn)+T((1α)n)+cn<i=0logα1n1cn+Θ(1)=cn(logα1n)+Θ(1)=cnlgnlg1α+Θ(1)<cnlgnlg1α+nlgn=O(nlgn)

Thus, we have derived a guess of T(n)=O(nlgn) for our original recurrence. Now let's use the substitution method to veify that our guess was correct. We want to show that T(n)dnlgn for some constant d>0. So:

T(n)=T(αn)+T((1α)n)+cndαnlg(αn)+d(1α)nlg((1α)n)+cn=dαn(lgα+n)+d(1α)n(lg(1α)+lgn)+cn=dnlgn+dαlgαn+d(1α)lg(1α)n+cn<dnlgn+dαlgαn+dαlgαn+cn(12<α<1)=dnlgn+(c+2dαlgα)ndnlgn

where the last step holds as long as dc2αlg1α.

So we proved T(n)=Ω(nlgn) and T(n)=O(nlgn). So T(n)=Θ(nlgn) when 12<α<1. Thus T(n)=Θ(nlgn) for all 0<α<1.