Chap 6
Chap 6
Note: Hn = 2Hn−1 + 1
Why is Hn = 2Hn-1 + 1
Only move 1 disk at a time.
Never set a larger disk on a smaller one.
Solving Tower of Hanoi RR
Hn = 2 Hn−1 + 1
= 2 (2 Hn−2 + 1) + 1 = 22 Hn−2 + 2 + 1
= 22(2 Hn−3 + 1) + 2 + 1 = 23 Hn−3 + 22 + 2
+1
…
= 2n−1 H1 + 2n−2 + … + 2 + 1
= 2n−1 + 2n−2 + … + 2 + 1 (since H1=1)
= 2n − 1
§6.2: Solving Recurrences
Definition: A linear homogeneous
recurrence relation of degree k with
constant coefficient is a recurrence
relation of the form
an = c1an−1 + … + ckan−k,
where the ci are all real, and ck ≠ 0.
The solution is uniquely determined if
k initial conditions a0…ak−1 are
provided
§6.2: Solving Recurrences..
Linear?: Right hand side is sum of
multiples of previous terms.
Homogenous?: No terms that are NOT
multiples of the ajs.
Degree?: k-degree since previous k
terms are used.
Solving with const. Coefficients
Basic idea: Look for solutions of the form
an = rn, where r is a constant.
This requires the characteristic
equation:
rn = c1rn−1 + … + ckrn−k, i.e.,
rk − c1rk−1 − … − ck = 0 (Dividing both sides
by rn-k and subtracting right hand side from left).
The solutions (characteristic roots) can
yield an explicit formula for the sequence.
Solving ……
Theorem1: Let c1 and c2 be real
numbers.
Suppose that r2 − c1r − c2 = 0 has
two distinct roots r1and r2. Then the
sequence {an} is a solution of the
recurrence relation an = c1an−1 +
c2an−2 if and only if an = α1r1n + α2r2n
for n≥0, where α1, α2 are constants.
Theorem 1: Proof
2 things to prove
Case 1: Roots are r1 & r2, i.e., {an = α1r1n +
α2r2n} an is a solution.
r1 & r2 are roots of r2 − c1r − c2 = 0 r12 =
c1r1 + c2; r22 = c1r2 + c2.
c1an−1 + c2an−2 = c1(α1r1n-1 + α2r2n-1) +
c2(α1r1n-2 + α2r2n-2)
α1r1n-2 (c1r1 + c2) + α2r2n-2 (c1r2 + c2)
α1r1n-2 r12 + α2r2n-2 r22
an
Theorem 1: Proof …
2 things to prove
Case 2: an is a solution an = α1r1n + α2r2n for some α1
& α2.
an is a solution a0 = C0 = α1 + α2.
C1 = α1r1 + α2r2
α1 = (C1 – C0r2)/(r1- r2)
α2 = C0 – α1 = (C0r1 - C1)/(r1- r2)
Works only if r1≠ r2
an = α1r1n + α2r2n works for 2 initial conditions
Since the initial conditions uniquely
determine the sequence, an = α1r1n + α2r2n
Example
Solve the recurrence an = an−1 + 2an−2 given the
initial conditions a0 = 2, a1 = 7.
An = rn rn = rn-1 + 2rn-2 r2 = r +2
Solution: Use theorem 1
c = 1, c = 2
1 2
Characteristic equation:
r2 − r − 2 = 0
Solutions: r = [−(−1)±((−1)2 −
4·1·(−2))1/2] / 2·1
= (1±91/2)/2 = (1±3)/2, so r = 2 or r
= −1.
So a = α 2n + α (−1)n.
n 1 2
Example Continued…
To find α1 and α2, solve the equations for the
initial conditions a0 and a1:
a0 = 2 = α120 + α2 (−1)0
a1 = 7 = α121 + α2 (−1)1
Simplifying, we have the pair of equations:
2 = α1 + α2
7 = 2α1 − α2
which we can solve easily by substitution:
α2 = 2−α1; 7 = 2α1 − (2−α1) = 3α1 − 2;
9 = 3α1; α1 = 3; α2 = -1.
Final answer: an = 3·2n − (−1)n
The Case of Degenerate Roots
Theorem2: Let c1 and c2 be real
numbers with c2 ≠ 0. Suppose that r2
− c1r − c2 = 0 has only one root r0.
A sequence {an} is a solution of the
recurrence relation an=c1an-1 + c2an-2 if
and only if an = α1r0n + α2nr0n, for all
n≥0, for some constants α1, α2.
k-Linear Homogeneous Recurrence
Relations with Constant Coefficients
Theorem3: Let c1,c2,….ck be real numbers.
k
Suppose the C.E. r k
c r k i 0
i 1
i
i 1 j 0
Simplifying, we get
(−2c+2)n + (3c−2d) = 0 (collect terms)
So c = −1 and d = −3/2.
So a(p)n = −n − 3/2 is a solution.
Finding a Desired Solution
From Theorem 5, we know that all general
solutions to our example are of the form:
an = −n − 3/2 + α3n.
Solve this for α for the given case, a1 = 3:
3 = −1 − 3/2 + α31
α = 11/6
The answer is an = −n − 3/2 + (11/6)3n
Theorem 6
k
Suppose {an} satisfies LiNoRR: an ci an i F (n)
i 1
t t i n
And F(n)= bt i n s ; b t and s are real nos.
i 0
When s is not a root of C.E. of the associated
LiHoRR, there is a particular solution of the
form t
t i n
p n s
i 0
t i
Theorem 6 continue…..
When s is a root of this C.E. and its
multiplicity is m, there is a particular
solution of the form
t t i n
n pt i n s
m
i 0
This factor nm ensures that the proposed
particular solution will not already be a
solution of the associated LiHoRR.
Theorem 6: Example
a = 6a
n n-1
– 9a
n-2
+ F(n); F(n) =
3n
C.E.: r2 – 6r + 9 => (r-3)2 = 0; r = 3.
Solution: By Theorem 6, s = 3,
multiplicity m = 2. Solution is of the form
p0n23n, where p0 is polynomial constant.
§6.3: Divide & Conquer R.R.s
Many types of problems are solvable by
reducing a problem of size n into some
number a of independent subproblems, each
of size n/b, where a1 and b>1.
The time complexity to solve such problems is
given by a recurrence relation:
f(n) = a·f(n/b) + g(n)
2 n ( A1 B0 A0 B1 ( A1 B1 A1 B1 ) ( A0 B0 A0 B0 ))
(2 2 n 2 n ) A1 B1 (2 n 1) A0 B0
2 n ( A1 B0 A1 B1 A0 B0 A0 B1 )
(2 2 n 2 n ) A1 B1 (2 n 1) A0 B0
2 n ( A1 A0 )( B0 B1 ) (Factor last polynomial)
Three multiplications, each with n-digit numbers
Recurrence Rel. for Fast Mult.
Notice that the time complexity f(n) of
the fast multiplication algorithm
obeys the recurrence:
Time to do the needed adds &
f(2n)=3f(n)+C(n)
subtracts of n-digit and 2n-digit
i.e., numbers
f(n)=3f(n/2)+C(n)
So a=3, b=2.
Theorem1
Let f(n) = af(n/b) + c whenever n is divisible
by b, where a1, b is an integer greater than 1,
and c is a positive real number. Then
f (n) is
O ( n logb a )
O (log n )
if a > 1
if a = 1
Furthermore, when n =bk, where k is a positive
integer,
f(n) = nClog
1
ba + C .
2
+ clogbn.
n is a power of b or not: f(n) is O(logn), when a = 1.
a > 1: (from Theorem 1, Section 3.2)
f(n) = akf(1) + c(ak – 1)/(a – 1)
= ak[f(1) + c/(a – 1)] – c/(a – 1)
= C1nlogba + C2, since ak = alogbn = nlogba
f(n) is O(nlogba)
MASTER Theorem
Let f(n) = af(n/b) + cnd whenever n=bk,
where a1, b>1, and c and d are real number,
with c positive and d nonnegative. Then
O ( nd )
O ( n d log n )
f(n) is
if a<bd
if a=bd
o ( n logb a ) if a>bd
Example
We know that number of comparisons
used by the merge sort to sort a list of
n elements is less then M(n), where
M(n)=2M(n/2) + n
From Master Theorem we find that
M(n) is O(nlogn)
§6.4 Generating Functions
Generating functions: Used to represent
sequences efficiently by coding the terms of
a sequence as coefficients of powers of a
variable x in a formal power series
Definition: Let S = {a0, a1, a2, a3, ...} be
an (infinite) sequence of real numbers.
Then the generating function G(x), of S is
the series
G(x)=a0+a1x+…+akxk+…=
k 0
ak xk
Example of GF
Let S = {1, 1, 1, 1, ... }. Then
if S=1,1,1,1,1,1
By Theorem 1 of Section 3.2,
(x6-1)/(x-1) = 1+ x + x2 +...+ x5
G(x) =(x6-1)/(x-1) is the generating function
of sequence 1,1,1,1,1,1
Theorems of GF
Theorem 1:
Let f(x) = k and g(x)=
a x k
k Then,
b x
.
k 0
k
k 0
f(x) + g(x)= k
k
( a bk ) xand
k 0
k k
f(x)g(x)= a j bk j x
k 0 j 0
Example
A expression for the generating function of the
sequence S= {1, 1, 1, 1, 1, 1, ...} is 1/(1- x)
1/(1- x) = 1 + x + x2 + x3 +…
Let f(x) = 1/(1-x) ; f(x) = k 0
2 ka x k
u
k
u ( u 1)....( u k 1) / kif! k>0,
1 if k=0.
e.g:EBC(-2,3) = (-2)(-3)(-4) / 3! = -4.
EBC(0.5,3)=(0.5)(-0.5)(-1.5) / 3! = 1/16
Example
When the top parameter (u) is negative
integer, the EBC can be expressed in terms
of ordinary binomial coefficients. Using
earlier definition:
EBC = (-n)(-n-1)….(-n-r+1)/r!
= (-1)rn(n+1)…(n+r-1)/r!
= (-1)r(n+r-1)!/(r!*(n-1)!). Therefore,
n r n r 1
1 1 C (n r 1, r )
r
n, r Z
r r
The Extended Binomial Theorem
Let x be a real number with |x| < 1
and let u be a real number. Then
(1+x)u = x
k 0
u
k
k
Example
Find GF for (1+x)-n and (1-x)-n
By EB Theorem,
it follows that
(1+x)-n = k x
n k
k 0
We know that r n ( 1) r C ( n r 1, r )
(1+x) =
-n
(
k 0
1) k
C ( n k 1, k ) x k
( x=) x r
G(x) = x (1-x) = x
n. -n n
n
r
r
n C ( n r 1, r ) x
r 0 r 0
G(x)= C (n r 1, r ) x
r 0
n r
Continue…
Substituting
t = n+r, we get
G(x)= C (t 1, t n) x
t
t n
Replacing t by r as the index of summation,
we get
G(x)= C (r 1, r n) x r
r n
1 3x k 0 k 0
Uses identity1/(1-ax) = x
a k k
k 0
Consequently, ak=2 . 3k
§6.5 The principle of Inclusion-Exclusion
|A| |B|
|A C| |B C|
|C|
General Inclusion-Exclusion
Theorem 1: Let A1,…,An be finite sets.
Then | A1 A2 …… An | =
| Ai |
1i n
| Ai Aj |
1i j n
i j k
| A A
1i j k n
A | ... ( 1) n 1
| A1 A2 ... An |
General Theorem: Proof
| A1 A2 …… An |: Show an element in the union is
counted only once in the right hand side.
Let a be a member of exactly r sets (A1, …).
a is counted C(r,1) times by 1st summation, C(r,2) by 2nd
summation of the intersections, and C(r,m) times by mth
summation involving m sets Ai.
a is counted exactly C(r,1) – C(r,2) + C(r,3) - … + (-
1)r+1C(r,r).
By Corollary 2 of Section 4.4, C(r,0) - C(r,1) + C(r,2) -
C(r,3) - … + (-1)rC(r,r) = 0.
C(r,0) = C(r,1) – C(r,2) + C(r,3) - … + (-1)r+1C(r,r).
a is counted only once.
§6.6 Applications of Inclusion-Exclusion
Alternative Form: Let Ai be the subset
containing the elements that have Property Pi.
Writing these quantities in terms of sets, we
have | Ai1 Ai 2 ... Aik | N ( Pi1 Pi 2. ..Pik )
( 1) n N ( P1 P2 ...Pn )
The Number of Onto Functions
Let m and n be positive integers with (m n).
How many ways to assign m different jobs to n
different employees if every employee is
assigned at least one job?
Assume m = 5, n = 4, following Theorem 1
(next slide):
45 – C(4,1).35 + C(4,2).25 – C(4,3)15 = 240
The Number of Onto Functions
Theorem: Let m and n be positive integers with
(m n). Then, there are
1 1 1 1 n 1
Dn n! 1 1
1! 2! 3! 4! n!
Theorem Proof
Property Pi: A permutation that fixes element i.
Number of derangements: number of permutations
having none of the properties Pi for i = 1,2,…,n.
Dn = N(P1’,P2’,…,Pn’) (from inclusion-exclusion)
= N - N ( Pi ) N ( Pi Pj ) - ...
i i j