Inhomogeneous Recurrences
a0tn + a1tn-1 + a2tn-2 + aktn-k = bn p(n)
where b is a constant and p(n) is a polynomial in n of degree d.
General solution is obtained by solving the following equation:
(a0xk + a1xk-1 + a2xk-2 + … + ak) (x-b)d+1 = 0
Example:
Function Hanoi(m,i,j)
// move m(smallest) rings from rod i to rod j by way of rod 6-i-j.
if m>0
{ Hanoi(m-1,i,6-i-j);
write “move ring “, m, “ from rod “, i, “ to rod “, j;
Hanoi(m-1,6-i-j,j);
}
// end of function Hanoi
tm = a if m=0
= 2tm-1+b otherwise.
So, (x-2)= b =1m * b .
So b=1 and p(m)= b which is of degree 0 (d=0).
(x-2)(x-1)1 = 0.
x=2 and x=1
General solution: tm = c1*2m + c2*1m = c1*2m + c2
We find that
1. c1+c2 =a (which is t0)
2. 2*c1+c2=2a+b (which is t1)
3. c1= a+b
4. c2= -b
So, tm = (a+b)*2m -b and O(tm) = 2m
Using the ordinary generating function.
t0 = a
tm = 2tm-1 + b , for m>=1
inf. inf. inf.
S tm xm = 2 S tm-1 xm + b S xm
m=1 m=1 m=1
G(x) - t0 = G(x) - a = 2xG(x) + bx/(1-x)
G(x) = a/(1-2x) + bx/((1-x)(1-2x))
G(x) = a/(1-2x) + b(1/(1-2x)-1/(1-x))
= (a+b)/(1-2x) - b/(1-x)
So, tm = (a+b)*2m – b*1m, the same result.
In general, if we have recurrences of the form:
a0tn + a1tn-1 + a2tn-2 + aktn-k = b1n p1(n) + b2n p2(n) + … + bmn pm(n)
where polynomials, p(n), on RHS are of degree d1, d2, and dm,
respectively,
we can solve this by solving the following characteristic polynomial:
(a0 xk + a1 xk-1 + … + ak) (x-b1) d1+1 (x-b2) d2+1 … (x-bm) dm+1
Example:
Tn = 0 if n=0
2 Tn-1 + n + 2n if n>=1
So, T0 = 0, T1 = 3, T2 = 12 and T3 = 35, which we may need later.
And, then the characteristic polynomial turns out to be:
(x-2) (x-1)2 (x-2) = (x-2)2 (x-1)2 = 0
General solution is: Tm = c12m + c2 m2m + c31m + c4 m1m
Now, to get the particular solution, we need to find particular values of c’s that
Satisfy the first four values of T’s, T0 , T1 , T2 , and T3.
T0 = 0 = c1 + c3
and c1 = -c3. (a)
T1 = 3 = 2c1 + 2c2 + c3 + c4 = -2c3 + 2c2 + c3 + c4,
c3 = 2c2 + c4 - 3 (b)
T2 = 12 = 4c1 + 8c2 + c3 + 2c4
T3 = 35 = 8c1 + 24c2 + c3 + 3c4
From (a), (b) and T2, we have 2c2 - c4 = 3.
From (a), (b) and T3, we have 10c2 - 4c4 = 14.
From the last two equations, we get c4 = -1, c2 = 1, c3 = -2 and c1 =2.
So, the particular solution is:
Tm = 2*2m + m2m - 2*1m - m1m
= 2m+1 + m2m - m - 2
and O(Tm) = m2m.
Change of Variables: Useful especially when the parameter changes
by a geometric progression like halving
Example1:
T(n) = 4T(n/2) + n2 where n>=2 is a power of
2.
Let n = 2i and ti = T(n) = T(2i) and T(n/2) = T(2i-1) =
ti-1
So, the recurrence becomes:
ti = 4ti-1 + (2i)2 = 4ti-1 + 4i
From above, we get tm = a*4m + b*m4m
The two above arbitrary constants a and b can be solved
from two initial conditions t0 and t1.
And, from n = 2i and ti = T(n) = T(2i), we get
T(n) = a n2+ bn2 log n
Example2: (Simpler one by Iteration)
T(n) = T(n/2) + n
where n>=2 is a power of 2 and T(1)=c.
Just like Example1 above, we can use “Change of variables” method.
But, in this case, we can just use an iteration method due to the
simplicity of recurrence relation.
Since n is a power of 2, n=2k for some integer k.
T(n) = T(2k) = 2k + T(2k-1) = 2k + 2k-1 + T(2k-2) = 2k + 2k-1 + 2k-2 + T(2k-3)
= 2k + 2k-1 + 2k-2 + 2k-3 + T(2k-4) = 2k + 2k-1 + 2k-2 + 2k-3 + …+ 21 + T(20)
= 2k + 2k-1 + 2k-2 + 2k-3 + …+ 21 + T(1)
= 2k+1 - 2 + c = 2n - 2 + c
So, T(n) = O(n) and also Ω(n).
It is noteworthy that due to the fact that T(n) is monotonically non-decreasing
the above result, T(n) = O(n) and also Ω(n), holds even when n is not
a power of 2.
How would you show that T(n) is monotonically non-decreasing, that is,
T(n) >= T(n-1) for every n>=2???
Ω Notation: (Lower Bounds)
Definition:
f(n) is in Ω(g(n)) if and only if f(n) >= c1 (g(n)) for all n>= c2.
This is exactly analogous to the definition of O(n) as
f(n) is in O(g(n)) if and only if f(n) <= c1 (g(n)) for all n>= c2.
Main Recurrence Theorem.
Let a, b, and k be integers such that a>=1, b>=2 and k>=0.
In the following, n/b denotes either the floor function or the ceiling function.
In the case of the floor function, the initial condition T(0)=u is given.
And, in the case of the ceiling function, the initial condition T(1)=u is given.
If T(n) <= aT(n/b) + f(n) and f(n)=O(nk ) ---Rec.1.1
then T(n) = O(nk ) if a< bk ,
= O(nk log n) if a= bk ,
= O(n log b a ) if a >bk
Note that if Rec.1.1 above is an equality (instead of an inquality) then the Ө-notation will
replace the O-notation.
Example-3:
T(n) <= T(floor(n,2)) + T(ceiling(n,2)) + n2 .
Assume that T(n) is nondecreasing.
Since T(n) is nondecreasing, T(floor(n,2)) <= T(ceiling(n,2)).
So, T(n) <= 2T(ceiling(n,2)) + n2.
Applying the above Theorem with a = b = k = 2, we have T(n) = O(n2 ) as a < bk .