Example Recurrence Problem And Solution
Solve the following recurrence exactly for n a power of 2.
T(1)=1; T(2)=2; T(4)=3
T(n) = 5T(n/2) - 7T(n/4) + 3T(n/8) + nlog(n), for n>4
SOLUTION
Step One - Change Variable
(Note: All logarithms in this discussion are base-2)
n=2k; k=log(n); (n/2)=2k-1; (n/4)=2k-2; (n/8)=2k-3;
nlog(n) = 2k*k = k*2k;
T(2s) = ts
tk = 5tk-1 - 7tk-2 + 3tk-3 + k*2k
tk - 5tk-1 + 7tk-2 - 3tk-3 = k*2k
Step Two - Find Characteristic Polynomial
On the right hand side above we have k*2k which is of the form p(k)*bk where b=2 and p(k)=k is a polynomial of degree d=1.
Accordingly the characteristic polynomial of the recurrence is
(x3 - 5x2 + 7x - 3)(x-2)d+1
= (x3 - 5x2 + 7x - 3)(x-2)2
= (x-1)2(x-3)(x-2)2
Step Three - Apply Formula And Write Solution
tk = c0*1k + c1*k*1k + c2*3k + c3*2k + c4*k*2k
= c0 + c1*k + c2*3k + c3*2k + c4*k*2k
Step Four - Use Intial Values To Get Equations In The Unknown Constants
1 = t0 = c0 + c2 + c3
2 = t1 = c0 + c1 + c2*3 + c3*2 + c4*2
3 = t2 = c0 + c1*2 + c2*9 + c3*4 + c4*8
Step Five - If There Are Not Yet As Many Equations As Unknown Constants, Make More Equations Using the Recurrence Relation
The recurrence is: tk = 5tk-1 - 7tk-2 + 3tk-3 + k*2k.
We get
t3 = 5(t2)-7(t1)+3(t0)+24 = 15-14+3+24=28, and
t3 = c0 + c1*3 + c2*27 + c3*8 + c4*24
and also
t4 = 5(t3)-7(t2)+3(t1)+64
= 5(28)-7(3)+3(2)+64 = 140-21+6+64 = 189, and
t4 = c0 + c1*4 + c2*81 + c3*16 + c4*64
which leads to:
28 = c0 + c1*3 + c2*27 + c3*8 + c4*24, and
189 = c0 + c1*4 + c2*81 + c3*16 + c4*64
The complete set of 5 equations in 5 unknowns is:
1 = c0 + c2 + c3
2 = c0 + c1 + c2*3 + c3*2 + c4*2
3 = c0 + c1*2 + c2*9 + c3*4 + c4*8
28 = c0 + c1*3 + c2*27 + c3*8 + c4*24
189 = c0 + c1*4 + c2*81 + c3*16 + c4*64
Step Six - Solve The Set Of Equations For The Unknown Constants and Check The Values
By Substituting Back Into The Equations
One method of solution is to start this way:
subtract the appropriate multiple of the first equation from the others in order to
eliminate c0 from them:
1 = c0 + c2 + c3
1 = + c1 + c2*2 + c3 + c4*2
2 = + c1*2 + c2*8 + c3*3 + c4*8
27 = + c1*3 + c2*26 + c3*7 + c4*24
188 = + c1*4 + c2*80 + c3*15 + c4*64
Next subtract an appropriate multiple of the second equation from all the equations below it to
eliminate c1 from all those equations:
1 = c0 + c2 + c3
1 = + c1 + c2*2 + c3 + c4*2
0 = + + c2*4 + c3 + c4*4
24 = + + c2*20 + c3*4 + c4*18
184 = + + c2*72 + c3*11 + c4*56
Now continue the pattern - you get:
1 = c0 + c2 + c3
1 = + c1 + c2*2 + c3 + c4*2
0 = + + c2*4 + c3 + c4*4
24 = + + + c3*(-1) + c4*(-2)
184 = + + + c3*(-7) + c4*(-16)
and then:
1 = c0 + c2 + c3
1 = + c1 + c2*2 + c3 + c4*2
0 = + + c2*4 + c3 + c4*4
24 = + + + c3*(-1) + c4*(-2)
16 = + + + + c4*(-2)
Now the bottom equation says: c4 = -8. If you then
plug in the value c4 = -8 into the fourth equation, it
is easy to get c3 = -8. Then plugging the values of
c3 and c4 into the third equation we easily
find the value of c2. Similarly we find the values of
c1 and c0. We get:
c0 = -1; c1 = 5; c2 = 10; c3 = c4 = -8
We check by verifying that
1 = -1 + 10 + (-8)
2 = -1 + 5 + 10*3 + (-8)*2 + (-8)*2
3 = -1 + 5*2 + 10*9 + (-8)*4 + (-8)*8
28 = -1 + 5*3 + 10*27 + (-8)*8 + (-8)*24
189 = -1 + 5*4 + 10*81 + (-8)*16 + (-8)*64
Step Seven - Plug The Values Of The Constants Into The Formal
Expression For tk
We get
tk = c0 + c1*k + c2*3k + c3*2k + c4*k*2k
= -1 + 5k + 10*3k - 8*2k - 8*k*2k
Step Eight - Rewrite The Solution By Doing
The Reverse Of The Change Of Variable
T(n) = tk; n=2k; k=log(n)
tk = -1 + 5k + 10*3k -8*2k -8*2k*k
T(n) = -1 + 5log(n) + 10*3log(n) -8n - 8nlog(n)
Also, since
log(3log(n)) = log(n)log(3) = log(nlog(3)),
we see that
3log(n) = nlog(3)
so we can write the solution as:
T(n) = -1 + 5log(n) + 10*nlog(3) -8n - 8nlog(n)
(Since log(3) ~ 1.585, we see that T(n) is Q(nlog(3)), i.e. about Q(n1.585).)
Step Nine - Final Check: Verify That The Original Relation And
Initial Values Are Consistent With The Answer
T(1) = -1 + 5log(1) + 10*3log(1) - 8*1 - 8*1*log(1)
= -1 + 0 + 10 - 8 - 0
= 1 (checks)
T(2) = -1 + 5log(2) + 10*3log(2) - 8*2 - 8*2*log(2)
= -1 + 5 + 30 - 16 - 16
= 2 (checks)
T(4) = -1 + 5log(4) + 10*3log(4) - 8*4 - 8*4*log(4)
= -1 + 10 + 90 - 32 - 64
= 3 (checks)
T(n) = -1 + 5log(n) + 10*3log(n) -8n - 8nlog(n)
T(n/2) = -1 + 5log(n/2) + 10*3log(n/2) -8n/2 - 8n/2log(n/2)
= -1 + 5log(n) - 5 + 10*3log(n)-1 -4n - 4nlog(n) +4n
= -6 + 5log(n) + (10/3)*3log(n) - 4nlog(n)
T(n/4) = -1 + 5log(n/4) + 10*3log(n/4) -8n/4 - 8n/4log(n/4)
= -1 + 5log(n) -10 + 10*3log(n)-2 -2n - 2nlog(n) +4n
= -11 + 5log(n) + (10/9)*3log(n) +2n - 2nlog(n)
T(n/8) = -1 + 5log(n/8) + 10*3log(n/8) -8n/8 - 8n/8log(n/8)
= -1 + 5log(n) - 15 + 10*3log(n)-3 -n - nlog(n) + 3n
= -16 + 5log(n) + (10/27)*3log(n) +2n - nlog(n)
5T(n/2) = -30 + 25log(n) + (50/3)*3log(n) - 20nlog(n)
-7T(n/4) = 77 - 35log(n) - (70/9)*3log(n) -14n + 14nlog(n)
3T(n/8) = -48 + 15log(n) + (10/9)*3log(n) + 6n - 3nlog(n)
5T(n/2) - 7T(n/4) + 3T(n/8) + nlog(n)
= -1 + 5log(n) + 10*3log(n) - 8n - 8nlog(n)
= T(n) (checks)