36
Discrete Mathematics Chapter 6 Advanced Counting Techniques

Discrete Mathematics Chapter 6 Advanced Counting Techniques

Embed Size (px)

Citation preview

Page 1: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Discrete Mathematics

Chapter 6 Advanced Counting Techniques

Page 2: Discrete Mathematics Chapter 6 Advanced Counting Techniques

7.1 Recurrence Relations( 遞迴關係 )

Example 1. Let {an} be a sequence that satisfies the recurrence relation an=an1an2 for n=2,3,…, and suppose that a0=3,and a1=5.

Here a0=3 and a1=5 are the initial conditions.By the recurrence relation,

a2 = a1a0 = 2

a3 = a2a1 = 3 a4 = a3a2 = 5 :Q1: Applications ?Q2: Are there better ways for computing the terms of

{an}?

Page 3: Discrete Mathematics Chapter 6 Advanced Counting Techniques

※Modeling with Recurrence RelationsWe can use recurrence relations to model (describe) a wide variety of problems.

Example 3. Compound Interest ( 複利 )Suppose that a person deposits( 存款 ) $10000 in

a saving account at a bank yielding 11% per year with interest compounded annually. How much will be in the account after 30 years ?

Sol : Let Pn denote the amount in the account after n years. Pn=Pn1 + 0.11Pn1=1.11 Pn1,

∴ P30=1.11 P29=(1.11)2 P28=…=(1.11)30 P0

=228922.97

P0=10000

Page 4: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Example 5. (The Tower of Hanoi)

The rules of the puzzle allow disks to be moved one at a time from one peg to another as long as a disk is never placed on top of a smaller disk. Let Hn denote the number of moves needed to solve the Tower of Hanoi problem with n disks. Set up a recurrence relation for the sequence {Hn}.

Sol : Hn=2Hn-1+1, ( n1 個 disk 先從 peg 1→peg 3, 第 n 個 disk 從 peg 1→peg 2, n1 個 disk 再從 peg 3→peg 2)

peg 1 peg 2 peg 3

H4 moves

目標 : n 個 disk 都從 peg 1 移到 peg 2

H1=1

Page 5: Discrete Mathematics Chapter 6 Advanced Counting Techniques

上例中 Hn=2Hn1+1, H1=1

∴Hn=2Hn1+1

=2(2Hn2+1)+1

=22Hn2+2+1

=22(2Hn3+1)+2+1

=23Hn3+(22+2+1)

:

=2n1H1+(2n2+2n3+…+1)

=2n1+2n2+…+1

= =2n112

12

n

Page 6: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Example 6. Find a recurrence relation and give initial conditions for the number of bit strings of length n that do not have two consecutive 0s. How many such bit strings are there of length 5 ?

Sol :

∴ an = an1+an2, n 3

a1=2 (string : 0,1) a2=3 (string : 01,10,11)

∴ a3=a2+a1=5, a4=8, a5=13

1an-1 種

an-2 種 1 0

n-2 n-1 n1 2 n-3…

Page 7: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Example 7. (Codeword enumeration)

A computer system considers a string of decimal digits a valid codeword if it contains an even number of 0 digits. Let an be the number of valid n-digit codewords. Find a recurrence relation for an.

Sol :

1~9an1 種

10n1 an1 種 0

∴ an = 9an1 + (10n1an1) = 8an1 + 10n1 , n2 a1 = 9

n-1 n1 2 3 …

Page 8: Discrete Mathematics Chapter 6 Advanced Counting Techniques

求 an 通解 :

)10108108(8 12323

3 nnn

na

Exercise : 3,23,25,27,29,41 (41 推廣成n)

122

11 10)108(8108

nnn

nnn aaa

)10108(8 122

2 nn

na

)10108108108(8 122321

1 nnnnn ...a

)1081081081088(8 1022321 nnnnnn ...

184102

1 nn

項共有 ,8

10公比為 ,8首項 1 nn-

11

1

84)8

10(848

)18

10(

)1)8

10((8

8

nnnn

nn

n

Page 9: Discrete Mathematics Chapter 6 Advanced Counting Techniques

7.2 Solving Recurrence RelationsDef 1. A linear homogeneous recurrence relation of

degree k (i.e., k terms) with constant coefficients

is a recurrence relation of the form

where ciR and ck≠0

Example 1 and 2. fn = fn1 + fn2 an = an5

an = an1 + an22

an = nan1

Hn = 2Hn1 + 1

an = c1an1+c2an2+…+ckank

(True, deg=2)(True, deg=5)

(False, 不是 linear)(False , 不是 linear, not constant coeff. )

(False, 不是 homogeneous)

Page 10: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Theorem 1.

Let an = c1an1+ c2an2 be a recurrence relation

with c1,c2R.

If r2 c1r c2= 0 ( 稱為 characteristic equation) has two distinct roots r1 and r2.

Then the solution of an is an = 1r1n + 2r2

n ,

for n=0,1,2,…, where 1 , 2 are constants.

(1 , 2 可利用 a0, a1 算出 )

Page 11: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Example 3.What’s the solution of the recurrence relation

an = an1 + 2an2

with a0=2 and a1=7 ?Sol :

The characteristic equation is r2 – r 2=0.

Its two roots are r1= 2 and r2 = 1.

Hence an=12n +2 (1)n .

∵a0 = 12 = 2, a1=212=7

∴1 = 3, 2 = 1

an = 32n (1)n.

驗算: a2 = a1 + 2a0 =11 a2= 322 1 =11

Page 12: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Example 4. Find an explicit formula for the Fibonacci numbers.

Sol :

fn = fn1 + fn2 , n 2, f0=0 , f1=1.

The characteristic equation is r2 r 1=0.

Its two roots are , .

So we have

2

511

r

2

512

r

nnnf )

2

51()

2

51( 21

,0210 f∵ 1)2

51()

2

51( 211

f

5

1 ,

5

121

nnnf )

2

51(

5

1)

2

51(

5

1

Page 13: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Thm 2.

Let an = c1an1+c2an2 be a recurrence relation

with c1,c2R.

If r2 c1r c2= 0 has only one root r0 .

Then the solution of an is

an = 1 r0n + 2 n r0

n

for n=0,1,2,…, where 1 and 2 are constants.

Page 14: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Example 5.

What’s the solution of an= 6an1 9an2 with a0=1 and a1=6 ?

Sol :

The root of r2 6r + 9 = 0 is r0 = 3.

Hence an = 1 . 3n +2 . n . 3n .

∵a0 = 1 = 1

a1 = 31 + 32 = 6

∴ 1 = 1 and 2 = 1

an = 3n + n . 3n驗算: a2 = 6a1 9a0 =27

a2= 32 +2 32 =27

Page 15: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Thm 3.

Let an = c1an1 + c2an2 + … + ckank be a recurrence relation with c1, c2, …, ck R.

If rk c1rk-1 c2rk-2 … ck = 0 has k distinct roots r1, r2,…, rk.

Then the solution of an is

an = 1r1n +2r2

n + …+krkn, for n = 0, 1, 2, …

where 1, 2,…k are constants.

Page 16: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Example 6 (k = 3)

Find the solution of an = 6an1 11an2 + 6an3

with initial conditions a0=2, a1=5 and a2=15 .Sol :

The roots of r3 6r2 + 11r – 6 = 0 are

r1 = 1, r2 = 2, and r3 = 3

∴an = 1 1n + 2 2n + 3 3n

∵a0 = 1 + 2 + 3 = 2

a1 = 1 + 22 + 33 = 5

a2 = 1 + 42 + 93 = 15

∴an = 1 2n + 2 3n

1 = 1, 2 = 1, 3 = 2

驗算: a3 = 6a2 11a1+ 6a0 =47 a3= 1 23 + 2 33 =47

Page 17: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Thm 4.

Let an = c1an1 + c2an2 + … + ckank be a recurrence relation with c1, c2, …, ck R.

If rk c1rk1 c2rk2 … ck = 0 has t distinct roots r1, r2, …, rt with multiplicities m1, m2, …, mt respectively, where mi 1,i, and m1+ m2 +…+ mt = k,

then ( 接下一頁 )

Page 18: Discrete Mathematics Chapter 6 Advanced Counting Techniques

nt

mt,mt,t,

ni

mi,mi,i

nm,m,,n

r)nα...nα(α

...

r)nα...nα(α

...

)rnα...nα(αa

t

t

i

i

1110

1110,

11

1111011

1

where i,j are constants.

(1 i t , 0 j mi1)

Page 19: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Example 8. Find the solution to the recurrence relation an = 3an1 3an2 an3 with initial conditionsa0 = 1, a1 = 2 and a2 = 1.

Sol :

r3 + 3r2 + 3r + 1 = 0 has a single root r0 = 1 of multiplicity three.

∴ an = (1+2n+3n2) r0n = (1+2n+3n2)(1)n

∵ a0 = 1 = 1

a1 = (1+2+3) (1) = 2

a2 = (1+2+3) = 1

∴1 = 1, 2 = 3, 3 = 2

an = (1+3n2n2) (1)nExercise : 3,13,15,19

驗算: a3 = 3a2 3a1 a0 =8 a3= (1+33232)(1)3 =8

Page 20: Discrete Mathematics Chapter 6 Advanced Counting Techniques

7.4 Generating Functions.

Def 1. The generating function for the sequence {an} is the infinite power series.

G(x) = a0 + a1x +… + anxn +…

=

( 若 {an} 是 finite ,可視為是 infinite ,但後面的 term都等於 0)

0k

kk xa

Page 21: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Example 2. What is the generating function for the sequence 1,1,1,1,1,1 ?

Sol :

1

1

...1)(6

52

x

x

xxxxG (expansion ,展開式 )

(closed form)

Page 22: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Example 3.

Let mZ+ and ,for k = 0, 1, …, m.

What is the generating function for the sequence a

0, a1,…, am ?

Sol :

G(x) = a0 + a1x + a2x2 + … + amxm

= (1+x)m (by 二項式定理 )

k

mak

mxm

mx

mx

mm

2

210

Page 23: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Example 5. The function f (x) = is the generating

function of the sequence 1, a, a2, …,

since = 1 + ax + a2x2 + …=

when |ax| < 1 for a≠0

ax1

1

ax1

1

0

)(k

kax

Page 24: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Def 2.

Let uR and kZ+∪{0}. Then the extended

binomial coefficient is defined by

k

u

0 if 1

0 if ),1)...(2)(1(!

1

k

kkuuuuk

k

u

Page 25: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Example 7.

Find and

Sol :

321

3

2

4)4)(3)(2(!3

1

3

2

16

1

2

3

2

1

2

1

!3

1

321

Page 26: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Thm 2. (The Extended Binomial Theorem)

Let xR with |x|<1 and let uR, then

0

1k

kuk

u xx

Page 27: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Example 9. Find the generating functions for (1+x)n and (1x)n where nZ+

Sol : By the Extended Binomial Theorem,

0

0

0 0

1 )1(

)1( )1( )( !

)1(

)1( )1)(( !

1)1(

k

kk

k

k

k

k

k k

kn

xk

kn

xkn...nnk

xkn...nnk

xk

nx

By replacing x by –x we have

k

k

n xk

knx

1)1(

0

k

kn

k

n k 1)1(:Note

Def 2.

Page 28: Discrete Mathematics Chapter 6 Advanced Counting Techniques

※Using Generating Functions to solve Recurrence Relations.

Example 16.

Solving the recurrence relation ak = 3ak1 for k=1,2,3,… and initial condition a0 = 2.

Sol :

另法: (by 7.2 公式 )

r – 3 = 0 r = 3 an = 3n

∵ a0 = 2 = ∴ an = 2 3n

Page 29: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Let be the

generating function for {ak}.

First note that ak xk = 3ak1 xk

G(x) a0 = 3x G(x)

∵a0 = 2 G(x) 3x G(x) = G(x)(13x) = 2

0

2210 ...)(

k

kk xaxaxaaxG

1 1 1 0

111 333

k k k k

kk

kk

kk

kk xaxxaxxaxa

0 0

32)3(231

2)(

k k

kkk xxx

xG

∴ ak = 2 3kExercise : 5,7,11,33

Page 30: Discrete Mathematics Chapter 6 Advanced Counting Techniques

7.5 Inclusion-Exclusion 排容原理A,B,C,D : sets

CBACBCABACBACBA.

BABABA.

)2(

)1(

1

1 12

2 23

A

B C

|A|+|B|+|C| 時各部分被計算的次數

?)3( DCBA.

11

1

1

20

+|ABC| 後

-|AB|-|AC|-|BC| 後

Page 31: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Theorem 1.

A1, A2, …, An : sets

nn

nkjikji

njiji

n

iin

AAAAAA

AAAAAA

...)1(...

...

21,,1

1121

Exercise : 17

Page 32: Discrete Mathematics Chapter 6 Advanced Counting Techniques

7.6 Applications of Inclusion and Exclusion

Example 2. How many onto functions are there form set A={1, 2, 3, 4, 5, 6} to set B={a, b, c} ?

Sol : f : A → Bf (1)= {a, b, c}f (2)= ︰ ︰f (6)=

不同的填法造出不同的函數如何使 a,b,c 都出現 ?

# of onto functions = ( 所有函數個數 ) (a,b,c 中有一個沒被對應 ) + (a,b,c 中二個沒被對應 ) (a,b,c 都沒被對應 )= 63

363

263

16 0123

Page 33: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Thm 1. |A| = m , |B| = n

There are

onto functions f : A → B.

mn

nn

mnmnmnm nnnn

1)1(

...)3()2(1

11

321

pf : A = {a1, a2, …, am}. B = {b1, b2, …, bn}

f (a1)= f (a2)= ︰ ︰

f (am)=

b1, b2, …, bn

Page 34: Discrete Mathematics Chapter 6 Advanced Counting Techniques

※Derangements 亂序Def.

A derangement is a permutation of

objects that leaves no object in its

original position.

Page 35: Discrete Mathematics Chapter 6 Advanced Counting Techniques

D4 = ( 所有 4 個元素的 permutation 數 ) (4 個元素有一個在原位置的 permutation 數 ) + (4 元素中有二個在原位置的個數 ) (4 個元素中有三個在原位置的個數 ) + (4 元素都在原位置的個數 )

=

Example 5. derangements of 12345 :

Let Dn denote the number of derangements of n objects.

!04

4!1

3

4!2

2

4!3

1

4!4

21453, 23451, 34512, …

Def.

Page 36: Discrete Mathematics Chapter 6 Advanced Counting Techniques

Theorem 2. ( 亂序公式 )

Exercise : 8

) !

1 )1( ...

!2

1

!1

1 1 ( !

)!

!0

!0!

! )1(

... !

)!2(

)!2(!2

!

!

)!1(

)!1(!1

! 1( !

!0 )1( ... )!2( )!1( ! 21

nn

nn

n

n

n

n

n

n

n

n

nn

nnnD

n

n

nn

nnnn

參考: 12,13