40
Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

  • View
    232

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics

Transparency No. 4-1

Chapter 4

Counting

Page 2: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-2

Contents

Combinatorics (組合學 ) The study of arrangement of objects

Enumeration: The counting of objects with certain properties i.e., Find the size of the set {x | x satisfies property P} A branch of combinatorics

Permutation: Ordered arrangements of objects

Combination: Unordered arrangement of objects

Problems that can be solved by counting techniques: Allowable passwords on a computer system Gambling games Probability problems

Page 3: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-3

4.1 The basics of counting

Problem: [# allowable passwds] Properties on a passwd:

1. 6,7 or 8 chars in length

2. alphabet: ={0,..,9} U {A,B,..,Z}

3. contains at least one digit.

==> How many passwds are there ?

Sol: #passwd = #passwd of length 6 + ..7 +.. 8 #passwd of length k = |k \ k w/o digit | = |k| - |k w/o digit| =||k - (||-10)k

Hence #passwd = (368 - 268) + (367-267)+(366-266).

Page 4: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-4

Basic counting principles:

The sum rule: Task t1 can be done in n1 ways task t2 can be done in n2 ways and t1 and t2 cannot be done at the same time ==>There are n1+n2 ways to do either task.

Ex1: Box1 : n1 balls ; Box 2: n2 balls, ==> there are n1 + n2 possible outcomes to take a ball

from either box1 or box 2.

Ex 2: 37 teachers; 83 students to be chosen as a

representative ==> there are 37+83 possible choices.

Page 5: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-5

Generalized sum rule

Task t1 can be done in n1 ways

...

Task tk can be done in nk ways

==> There are (n1+...+nk) ways to do a task among t1,..,tn.

Ex2: List A: 23 projects

B: 15 projects

C: 19 projects

==> There are 23 + 19 + 15=57 projects that can be chosen

Formalization: [in terms of set notation] A1,..,Ak : k disjoint finite sets

|A1 U A2 ...U Ak | = i=1,k |Ai|

Page 6: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-6

The product rule

T = (T1,..,Tk)

T1 can be done in n1 ways,

...

Tk can be done in nk ways

==> there are n1 x n2 x...x nk = i=1,k ni ways to do the task.

Ex 4: Labeling a chair with a upper-case letter and a number in [1,100].

==> there are at most 26 x 100 chairs with different labels.

Ex 5: 32 computers, each containing 24 ports

==> there are 32 x 24 ways to choose a port. (why production rule ?)

Task = (choose a computer, chose a port )

Page 7: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-7

Example product rule

Ex 6. # of bit strings of length 7 = ?

Ex 8: # functions from {1...,m} to {1,..,n} =?

Ex 8': #of partial functions from {1,..m} to {1,...,n} = ?

Ex 9: # of 1-1 functions from {1,..,m} to {1,...,n} (m n) = ?

Ex11: Value of k = ? after execution of the code.

k := 0;

for i1 = 1 to n1

for i2 = 1 to n2

...

for im = 1 to nm

{k = k+1 ; print 0};

print k // ==> k = s=1,..m ns = # of 0 printed out.

Page 8: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-8

Ex 12. use product rule to prove |P(S)| = 2|S|

sol: S = {e1,e2,...,en}, at the ith step choose or not choose ei ==> 2x2...x2 = 2n possible choices, each determining a unique subset.

Formalization:

- |A1xA2x...xAn| = |A1|x...x|An|.

- ie. = |{(x1,...,xn) | xi Ai } |

Page 9: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-9

More complicated counting problems

require both rulesEx13: identifier(name of variable) requirement:

a string of one or two alphanumeric chars lower and upper cases treated as identical beginning with a letter different from 5 reserved words: {if, Do, for ,..} ==> # different identifiers available = ?

sol: #id = #id of size 1 + #id of size 2 - 5 --- sum rule #id1 = 26 #id2 = 26 x (10+26) --- product rule #id = 26 + 26x36 - 5 = 957.

Page 10: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-10

The inclusion-Exclusin principle:

Ex15:#bit strings of length 8 beginning with 1 or ending with 0

= ?

sol: #bitString-b1 = 27; #bitString-e0 = 27; #bitString-b1&-e0 = 26

=> #bitString-b1|e0 = 27 + 27 - 26.

Set formalization: |A U B | = |A| + |B| - |A B|

Analogy: Box 1: n balls of diff colors

Box 2: m balls of diff colors

k: #colors appearing in both Box 1 and Box 2.

==> #possible colors for a ball chosen = ?

Page 11: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-11

Irregular cases: tree diagram.

#bit strings of length 4 w/o two consecutive 1s.

0

0

0

0 1

1

1

1

1

1 0

0 0

Page 12: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-12

4.2 The pigeonhole principle

Theorem: If k+1 objects are put into k boxes, then there is at least one box containing more than one objects.

pf: Let xi be #objects box i contains.

If xi 1 for all i => k+1 = i=1..k xi k, a contradiction!.

Ex1: At least two persons have the same birthday in 367 people.

Analogy: people =>object, date => box.

Ex2: 27 words => two beginning with the same letter.

Ex3: exam. scale: 0~100 => min # of students to guarantee occurrence of receiving same scores on students = ?

Page 13: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-13

Generalized pigeonhole principle

Theorem 2: N objects placed on k boxes => some box contains at least [N/k] (ceiling) objects.

Pf: let Xi = #objects box i contains

if xi < [N/k] for all i => xi k([N/k]-1) < K (N/k +1 -1) = N, impossible!.

Ex4: 100 people => there are at least [100/12] = 9 born in the same month.

Ex5: min # students in a course to guarantee at least 6 receive the same grade, if there are 5 possible grades.

sol: find min N s.t. [N/5] = 6.

Page 14: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-14

Elegant applications

Ex7: Baseball team:1. play 1 game at least a day.

2. play no more than 45 games a month (30 days) => an interval [n, n+m] during which the team play e

xactly 14 games.

sol: Let ak = # games played in [1,k]

=> 1=a1 < a2< ... < a30 45

and 15=a1+14 < a2+14 < ... <a30+14 59.

All 60 terms: a1,..,a30,a1+14,..,a30+14 are among [1, 59]

=> term aj and ai+14 s.t., aj = ai + 14.This means 14 games were played in [i+1,j]

Page 15: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-15

More elegant application

Ex8; a1,..., an+1: n+1 positive integers 2n.

=> i,j s.t., ai | aj.

sol:

Write each ak as 2 pk

x qk (Pk 0; qk: odd)

ak odd => ak = 20 x ak

ak even = 1101000 => pk = 3; qk = 13.

=> there are n+1 items: q1,..,qk+1, each odd and 2n.

=> i < j s.t. qi = qj = q.

=> ai = 2pi q; aj = 2pj q => aj = ai 2 pj-pi

=> ai | aj.

Page 16: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-16

more applcations

Def: a1,..,an: a sequence of numbers.

aj1,aj2,...,ajm is a subsequence iff 1j1<j2<..< jm n.

Theorem 3: Every seq of n2 + 1distinct numbers contains a subseq of n+1 numbers that is either strictly increasing or decreasing.

Example: 8,11,9,1,4,6,12,10,5,7 : 32+ 1 numbers.

=> incr. or decr. subseq of length 4: 1 4 6 12, and more...

Pf: a1,a2,... an2+1 :any seq of distinct n2+ 1 numbers.

Ik: the length of the longest incr. subseq beginning from ak.

Dk: the length of the longest decr. subseq beginning from ak.

Note: for 1 i,j n2 + 1, we have i=j iff (Ii,Di) = (Ij,Dj).

Now suppose no incr. nor decr. subseq of length n+1.

=> each 1 Di,Ii n for i = 1,…, n2+1. => only n2 distinct (D,I)’s

=> 1s < t n2+1 s.t. (Ds,Is) = (Dt, It) => contradiction!.

Page 17: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-17

Ramsey theory

Ex 10: A group of 6 people. Each pair are either friends or enemies. => three mutual friends or mutual enemies.

Pf: Let A : anyone of the group.

=> among other 5:

at least 3 are all A's friend --- By PigeonHole Prinp.--- (1)

(say,B,C,D) or A's enemies --- (2)

case 1: if a friend pair (say, BC) => A,B,C are mutual friends.

o/w: B,C,D are mutual enemies.

case 2: Similar to case 1.

Page 18: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-18

4.3 permutations and combinations

[Permutation] S={x1,...,xn } : a set with n elements.

1. Any ordered listing (arrangement) of all elements of S:

ie. 1,...,n s.t. {1,..,n} = S

is called a permutation of S.

2. Any ordered listings of r distinct elements of S:

1,...,r

is called an r-permutation of S.

Ex: S= {1,2,3}

==> 3,1,2 is a perm of S;

1,3 is a 2-perm of S.

Page 19: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-19

# of r-permutations

Theorem 1: #r-perm in a set with n elements (r n ) is

P(n,r) = n (n-1)...(n-r+1) = n!/(n-r)!

pf: 1,..r: any r-perm.

==> n ways to choose 1; n-1 ways to choose 2,

... n-r+1 ways to choose r ==> P(n,r) possible choices.

Ex3: 8 runners compete for 3 medals.

#ways to award medals = ?

Sol: gold medal --- 8 ways

Silver ---> 7 ways; bronze --> 6 ways ==> P(8,3) = 8x7x6.

Ex4: Visiting 8 cities beginning from a specific city.

P(7,7) = 7 ! = 5040 ways.

Page 20: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-20

Combinations

S = {X1,...,Xn }: a set . An r-combination of S is an unordered selection of r elements from S. (i.e., any subset of S of length r)Ex: S= {1,2,3,4} ==> {1,3,4} = {4,3,1}, {1,2,3} are 3-comb. of S.

Theorem 2: |S|= n, r n => # r-combinations of S = C(n,r) = ? Sol: Since P(n,r) =C(n,r) (r!), ----- to find a perm, first choose r elements from S and then permute them. C(n,r) = n! / r!(n-r)!Corollary: C(n,r) = C(n,n-r).Ex7: #ways to choose 5 among 10 persons to attend a game. Sol: C(10,5)

Page 21: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-21

Binomial coefficients

Theorem 3: [Pascal's identity]: C(n+1,k) = C(n,k) + C(n, k-1).

Pf: 1. by def. (omitted)

2. S: any set of n+1 elements;

a: any element of S.

C(n+1, k) = # k-comb of S =

#k-comb of S containing a + #k-perm of S w/o a

= C(n, k-1) + C(n, k).

Pascal's Triangle: (0,0) 1

(1,0) (1,1) 1 1

(2,0) (2,1) (2,2) 1 2 1

(3,0)(3,1)(3,2)(3,3) 1 3 3 1

---- ------------------------ -- ----- -- --- -- - --

Page 22: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-22

Binomial coefficients(cont'd)

Theorem 4: k=0,n C(n,k) = 2n.

Pf: S: any set with n elements.

=> #subsets of S = 2n

= k=0,n (#subsets of S with k elements)

= k=0,n C(n,k)

Theorem 5: Vandermonde's identity: [generalization of Thm 3]

m,n 0 ; r m or r n.

==> C(m+n, r) = k=0,r C(m,r-k) C(n,k)

Pf: Let |S| = m, |T| = n and S and T are disjoint.

==> C(m+n,r) = #r-comb. of SUT.

= k=0,r (#k-combinations of S x #(r-k)-comb of T)

= k=0,r C(m,r-k) C(n,k)

note: for Theorem 3, n =1 and r = k.

Page 23: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-23

The binomial theorem

(x+y)n = k=0,n C(n,k)xkyn-k.Pf: (x+y)n = (x1+y1)(x2+y2)...(xn+yn).

Each xkyn-k corresponds to one k-combination in {x1,..,xn}.

There are C(n,k) k-comb, so the coeff of xkyn-k is C(n,k).

Ex11: What is the coeff of x12y13 in (2x-3y)25 ?

Ex: 2 n = (1+1)n = k=0,n C(n,k)1k1n-k = k=0,n (n,k)

Page 24: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-24

4.6 generalized permutations and combinations

4.4 discrete probability --- skipped

4.5 probability theory --- skipped

Permutations with repetitions:

Def: S = {x1,..,Xn}: a set.

An r-permutation of S with repetition is any seq

1,..,r s.t. {1,..,r} S.

Theorem 1: #r-perms in a set with n elements = nr.

Ex1: #strings of length n in the English alphabet = 26n.

Page 25: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-25

Multisets

A multiset (bag) is a set allowing multiple occurrences of its elements.

Ex: S={1,1,2,3,4,4,4,5} is a multiset in which 1 occurs twice,4 occurs 3 times. Note: S {1,2,3,4}.

Properties of multisets:A1. [Membership:] For each multiset and each object x:

#(S,x) =def number of occurrences of x in S. ==> 1. #(S,x) is called the multiplicity of x in S. 2. x is a memebr of S iff #(S,x) > 0. 3. Every multiset is uniquely identified by its membership function.A2. Empty multiset {}: for all object x, #({},x) = 0.

Page 26: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-26

Properties of multisets

A3: Cardinality: |S| = # of occurrences of elements of S = x in U #(S, x).

For simplicity, a finite multiset S can be represented as

{X11, X2

,...,Xnn}, where X1,..,Xn are all objects of U s.t.

#(S,Xj) =j > 0.

A4: [inclusion and equality] A B iff for all x, #(A,x) #(B,x). A = B iff A B and B A iff #(A,x) = #(B,x) for all x. Ex: {1,2, 3} {1,1, 2,3 }.

Facts: A = B => |A| = |B|; A B => |A| |B|.

Page 27: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-27

Multiset operations

A4: operations: A U B = S with #(S,x) = max(#(A,x), #(B,x)) A B = S with #(S,x) = min(#(A,x), #(B,x)) A \ B = S with #(S,x) = #(A,x) - #(AB,x) Note: complement is not defined in multisets.

Ex: A = {1,2,2,2,3,5,5}; B = {3,4,4,1,1} ==> A U B = ? A B = ? A \ B = ?

A5: Bag space: D: an (ordinary) set domain Dn =def the set of all multisets whose elements are in D s.t. no ele

ments occur more than n times. (i.e., S in Dn iff #(S,x) n for all objects x.) Note: If bag space is given => #(~S, x) = n - #(S,x). If n = 1 => all multisets become ordinary sets.

Page 28: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-28

Combinations with repetitions

Def: [r-combination with repetitions] An r-combination with repetitions of a set S is a multiset of

size r with elements got from S. i.e., any multiset of size r from the bag space Sr.

Theorem 2: #r-combinations with repetitions from a set with n objects = C(n+r-1,r).

Pf: 1. ex: #ways to choose 4 elements from {1,2,3}.

==> 1 2 2 3 <==> 0 | 0 0 | 0

000|0| <==> 111 2

there are C(6,2) ways to put two bars on 6 possible positions

Hence the solu is C(6,2) = 15.

In general the problem is equ. to #ways to put n-1 bars on r+n-1 positions = C(n+r-1, n-1) = C(n+r-1, r)

Page 29: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-29

More precise proof of thm 2:

Pf: Let S = {1,2,..., n}, T = {1,2,..n, .., n+r-1} TT : the set of all (n-1)-combinations of T (i.e., subsets of with n-1 elements ). SS: the set of all multi subsets of Sr with r elements. note: |SS| = #r-combinations with repetitions from S.

Define f: TT -> SS s.t. if ordered listing of X = x1,..,xn-1, then #(f(X), k) = xk - x

k-1. (where x0 = 1 and x n = n+r-1.)

Fact: f is 1-1 and onto. Hence |SS| = |TT| = C(n+r-1, r). 1. onto: for any element Y = {1a1,2a2,...,nan} of SS

let x1 = 1+a1, x2 = x1 +1+ a2,.., xn-1 = xn-2 + 1+ an-1

==> X = {x1,..,xn-1} is a (n-1)-comb and f(X) = Y.

2. 1-1: If X = {x1,..,xn-1} Y = {y1,..,yn-1} ==>let X any Y disagree first at the j-th element

=> #(f(X), j) =xj - x j-1 yj - y j-1 = #f(f(Y), j) ==> f(X) f(Y)

Page 30: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-30

Applications

Ex6: #solutions in x1 + x2 + x3 = 11, where

x1,x2,x3 are non-negative integer.

Sol: Each solu. corresponds to a way of selecting 11 items

from {x1,x2,x3}. (i.e., {x1a1 x2

a2 x3a3 } with a1 + a2 + a3 = 11 ).

==> #solu = C(11+3-1, 2) =13 12 /2 = 78.

Ex6': The same as Ex6 but with constraints: x1 1, x2 2, x3 3.

solu: Let y1 = x1 -1, y2 = x2 -2, y3 = x3 -3.

y1 + y2 + y3 = x1 + x2 + x3 - 6 = 11 -6 = 5 --- (1)

with constraints: y1,y2,y3 0 ---(2)

#solu of (x1,x2,x3) = #solu(y1,y2,y3) in (1,2)

= C(5+2, 2) = 21.

Page 31: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-31

Examples

Ex7: What is the value of k after execution ? k := 0;

for i1 := 1 to n

for i2 := 1 to i1

....

for im := 1 to im-1

k := k+1.

sol: k is increased by 1 for each iteration (i1,i2,..,im) with

1 im im-1 ...i2 i1 n .

k = the size of the set {(i1,..,im) | 1 im im-1 ...i2 i1 n } = C(n+m-1, m). [ = #m-comb with rep. from {1,..,n} ]Ex7’ :

What is the # of increasing integer functions f: {1,…,m} {1,..,n} ? Sol: C(m+n-1, m).

Page 32: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-32

Permutations of sets with indistinguished objects.

Permutations of multisets: S = {x1

a1,...,xnan } with ai = m.

==> #permutations of S = m!/(a1! a2!... an!) ==> #r-permutations of S = ? (0 < r < m+1)

Ex 8: #ways to reorder the letters of the word: "success"

sol: S = {s3,c2,e,u} => #perm = 7! / 3!2!

pf: a1 objects of type x1 can be placed among the m positions in c(m,a1) ways.

a2 objects o ftype x2 ... m-a1 pos .. in c(m-a1,a2) ways,

...

an objects of type xn can be placed in C(an,an) ways.

==> #perm = C(an,an) C(an+an-1, an-1)...C(m,a1)

= m! / ai!

Page 33: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-33

examples

Theorem 4: #ways to distribute n distinguished objects into k boxes so that box i contains ni objects = n! /n1!...nk!.

pf: C(n,n1) ways to box 1

C(n-n1,n2) ways to Box 2,...

C(nk, nk) ways to box k

==> totally n! /n1! ...nk! ways

Ex9: 52 cards to 4 players, each with 5 cards.

==> C(52,5) x C(52-5,5) xC(52-10,5) x C(52-15,5).

Page 34: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-34

4.7 Generating permutations & combinations

Sometimes permutations and combinations need to be generated, instead of just being counted!

Ex1: [salesman problems] visiting 6 cities w/t duplication with minimal instances. ==> try all possible permutations of the cities and find one

with minimal instances.

Ex2: S={X1,...,x6}, find a subset {y1,..,yj} of S s.t. yk = 100.

sol: find each subset (combination) T of S and check their sum.

Page 35: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-35

Lexicographical ordering of permutations

S={1,2,...,n} = 1,2,..,s ;

= 1,..,t : two permutations [ with repetitions ] of S.

Define < iff either 1. s < t and is a prefix of (i.e. = s+1) ,.. t) or

2. 1 k min(s,t) s.t. k < k and j = j for all j < k.

Ex: a = 1 3 5 7 2 2 b = 1 3 5 2 4 6 7 8 c = 1 3 5 ==> what are their lexico. order ?

show that the lexico order < is a total order. 1. a < b and b < c => a < c (transitive) 2. a ~ a < a (irreflexive) 3. ab,either a < b or b < a or a = b. (comparable)

(T, <): a total order ; a, b : two elements of T [a,b] = {x in T | a x b}; successor(a) = b iff a < b and for all x > a, b x.

Page 36: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-36

Structure of the lexicographic order [skipped]

Problem: S = {1,..,n} : a set. a = a1,...,am : a permutation with repetition of S. The next permutation following a (i.e., successor of a) = ?

Ex: S = [1,2,3] ==> 1, 11, 111,... 112, 1121..., 12,.. 21, 211, 2111, ...,

==> 1. "a 1" is the successor of a. (or say a is predecessor of a1) 2. there are possibly infinite number of permutations between two permutations (eg. [1,2]) 3. Some permutations may have no predecessor (e.g. 2)Exercises: 1. x has a predecessor iff it ends with 1. 2. show that [x, y] is infinite iff m < n and n is not ending with 1. 3. Does there exist an infinite seq x1 > x2 > x3 > ... ?

Page 37: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-37

Generating permutation

Problem: S = {1,..,n} : a set. a = a1,...,an : a permutation w/o repetition of S. S(a) = the next permutation following a = ?

Ex: S = [1,6] ==> 2 3 4 1 5 6 --- not max in {5,6} --> 2341 65 2 3 4 1 6 5 --- 65 max in {6,5}; 165 not max in {1,5,6} --> 2 3 4 516 2 3 4 6 5 1 ---> 2 3 5 1 4 6 Ex2: s( 3 6 2 5 4 1) = 3 6 4 1 2 5.Rules: a = a1 a2 ... an be a permutation of S= {1,..,n}.

k: the largest index s.t. ak < ak+1>,...> an is in decreasing order. If k = 0 ==> a is the max permutation. o/w. Let b = min{x | x > ak /\ x in {ak+1,..,an}} and bk+1,.., bn be the least perm. of {ak,..an} \{b} ==> s(a) = a1 ... a k-1 b, bk+1 ... bn.

Page 38: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-38

Finding next permutation

procedure s(a) // find next perm following a=a1,..,an.

// 1. find largest j s.t. aj < a j+1

j := n-1; a0 := 0;

while aj > aj+1 do j := j - 1;

if j = 0 return(no successor);

// 2. Find smallest ak > aj among aj < a j+1 > a j+2> .. > an

k := n;

while aj > ak do k := k-1;

//3. exchange aj, ak

aj <-> ak;

//4. sorting aj+1 ...an

sorting(aj+1,...,an);

return(a1,..,an)

Page 39: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-39

generating combinations

S={x1,..,xn}

Each combination(i.e., subset) T of S can be represented by a bit strings t=b1 b2... bn with the correspondence that

xi T iff bi = 1.

next combination of T = combination with string coding next to that of T.

Ex: 10100100111 ---> 10100101000 (i.e. add one).

Procedure next(t) // t = b1b2,,,bn

{ i := n; while bi =1 do

{bi := 0; i := i-1}

bi := 1 }.

Page 40: Discrete Mathematics Transparency No. 4-1 Chapter 4 Counting

Discrete Mathematics Ch 4 Counting

Transparency No. 4-40

r-combinations of a setS={1,...,n}; T1, T2: two r-combinations of S ( r n)Define T1 < T2 iff increasing sorting listing of T1 < that of T2 in lexi

cographical order.Eg: T1={1,2,6}, T2={3, 5, 2} be two 3-combinations => T1 < T2 since 1 2 6 < 2 3 5Problem: how to find the next largest r-combination of any given

r-combination?ex: S = {1,...,6}, T = {1,2,5,6} => next-4-com(T) = ? 56 : the largest 2-com, 256: not the largest 3-com ==> replace 256 by 345, which is the next 3-comb > 256. ==> next(T) = 1345

rule: T= {a1,...,ar} {n-r+1,...,n}, which is the largest r-combination

=> let j be the largest index s.t. aj ≠ n-r +j.

=> next(T)={a1,..,aj-1, aj+1, aj+2,...aj+(r-j+1)}.Ex: S={1,...,9}; T={1 3 5 8 9} => next-5-comb(T) = {1,3,6,7,8}