410 likes | 528 Views
Chapter 5. Counting. 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
E N D
Chapter 5 Counting
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
5.1 The basics of counting • Problem: [# allowable passwds] • Properties on a passwd: 1. 6,7 or 8 chars in length 2. alphabet: S ={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 = |Sk \ Sk w/o digit | • = |Sk| - |Sk w/o digit| =|S|k - (|S|-10)k • Hence #passwd = (368 - 268) + (367-267)+(366-266).
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.
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 | = Si=1,k |Ai|
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 = Pi=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 )
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 = Ps=1,..m ns = # of 0 printed out.
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) | xiAi } |
More complicated counting problems • require both rules • Ex13: 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.
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 = ?
0 0 1 1 0 0 0 1 1 0 0 1 1 Irregular cases: tree diagram. • #bit strings of length 4 w/o two consecutive 1s.
5.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 = Si=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 = ?
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 => S 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.
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 exactly 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]
More elegant application Ex8; a1,..., an+1: n+1 positive integers £ 2n. => $ i,j s.t., ai | aj. sol: Write each ak as 2 pkx 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.
more applcations Def: a1,..,an: a sequence of numbers. aj1,aj2,...,ajm is a subsequence iff 1£ j1<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 => $ 1£s < t£ n2+1 s.t. (Ds,Is) = (Dt, It) => contradiction!.
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.
5.3 permutations and combinations • [Permutation] S={x1,...,xn } : a set with n elements. 1. Any ordered listing (arrangement) of all elements of S: ie. a1,...,an s.t. {a1,..,an} = S is called a permutation of S. 2. Any ordered listings of r distinct elements of S: a1,...,ar 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.
# 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: a1,..ar: any r-perm. ==> n ways to choose a1; n-1 ways to choose a2, ... n-r+1 ways to choose ar ==> 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.
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)! Ex12: #ways to choose 5 among 10 persons to attend a game. Sol: C(10,5) Corollary 2: C(n,r) = C(n,n-r). -- a subset with r elements present = a subset with n-r elements not present. Ex14: #bit strings of length n containing exactly r 1's = C(n, r). = #bit stings of length n containing (n-r) 0's = C(n, n-r)
4.5 Binominal Coefficients • C(n,r) is called a binominal coefficient since it is the coefficient in the expansion of binominal expression (x+y)n. • Theorem 1: (x+y)n = Sk=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). Ex4: What is the coeff of x12y13 in (2x-3y)25 ?
Corollaries of the binominal theorem Corollary 1: Sk=0,n C(n,k) = 2 n pf: 2 n = (1+1)n = Sk=0,nC(n,k)1k1n-k =Sk=0,nC(n,k). Corollary 2: Sk=0,n (-1)k C(n,k)= 0 pf: 0 = (-1 +1)n = Sk=0,n C(n,k)(-1)k 1 n-k = 0 Corollary 2' : (n,0) + (n,2) + … (n, 2n/2 ) = (n,1) + (n,3) + …(n, 2 (n-1)/2 +1). Corollary 3: Sk=0,n C(n,k)2k= 3n. Pf: Show it yourself.
Pascal's identity Theorem 2: [Pascal's identity]: C(n+1,k) = C(n,k) + C(n, k-1). Def1: A combinatorial proof of an identity s = t is a proof that uses counting arguments to show that both sides of the identity correspond to different ways of counting the same set of objects. Pf: 1. by def. (omitted) 2. Combinatorial proof: Let S: any set of n+1 elements; a: any element of S. Then C(n+1, k) = # k-comb of S = #k-comb of S containing a + #k-comb 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 ---- ------------------------ -- ----- -- --- -- - --
Binomial coefficients(cont'd) Corollary 1: Sk=0,n C(n,k) = 2n. Pf: combinatorial proof: S: any set with n elements. => #subsets of S = 2n = Sk=0,n (#subsets of S with k elements) = Sk=0,n C(n,k) Theorem 3: Vandermonde's identity: [generalization of Thm 2] m,n ³ 0 ; r £ m + n. ==> C(m+n, r) = St=0,r C(m,t) C(n,r-t) Pf: Let |S| = m, |T| = n and S and T are disjoint. ==> C(m+n,r) = #r-comb. of SUT. = St=0,r (#t-combinations of S x #(r-t)-comb of T) = St=0,r C(m,t) C(n,r-t) Note: for Theorem 2: C(1+n,k) = C(1,0)C(n,k) +C(1,1)C(n,k-1) +… = C(n,k)+C(n,k-1) where (m,n,r) = (1,n, k).
Corollaies of Theorem 3: Corollary 4: C(2n,n) = Sk=0,n C(n,k)2. Pf: C(2n,n) = C(n+n, n) = Sk=0,n C(n, k) C(n,n-k) = Sk=0,n C(n,k) C(n,k) Corollary 5: C(n+1,r+1) = Sk=r,n C(k,r). pf: By combinatorial argument. C(n+1, r+1) = #of bit strings of length (n+1) containing r+1 1's. = Sk'=r+1,n+1 #bit strings of length (n+1) with the last 1 appearing at position k'. = Sk'=r+1,n+1 C(k'-1, r) = C(r,r) + C(r+1,r) + … + C(n,r) = Sk=r,n C(k, r)
5.5 generalized permutations and combinations • Permutations with repetition: Def: S = {x1,..,Xn}: a set. An r-permutation of S with repetition is any seq a1,..,ar s.t. {a1,..,ar} Í S. Theorem 1: #r-perms in a set with n elements = nr. Ex1: #strings of length n in the English alphabet = 26n.
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.
Properties of multisets A3: Cardinality: • |S| = # of occurrences of elements of S • = Sx U #(S, x). • For simplicity, a finite multiset S can be represented as {X1a1, X2a2,...,Xnan}, where X1,..,Xn are all objects of U s.t. #(S,Xj) =aj > 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|.
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) - #(AÇB,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 elements 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.
Combinations with repetition Def: [r-combination with repetition] • An r-combination with repetition 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 repetition from a set with n objects = #ways to choose r objects from n kinds of objects H(n,r) = C(n+r-1,r) --- = C(n+r-1, n-1) 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 H(3,4) = C(6,4) 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)
More precise proof of Theorem 2 (skipped): 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 T 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 - xk-1 -1 (where x0 = 0 and x n = n+r.) 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 (xj, yj) be the first of all pairs at which X and Y disagree => #(f(X), j) =xj - x j-1 -1¹ yj - y j-1 -1 = #f(f(Y), j) ==> f(X) ¹ f(Y)
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 x2a2 x3a3 } with a1 + a2 + a3 = 11 ). ==> #solu = H(3,11) = 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) = H(3,5) = C(5+3-1, 2) = 21.
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 } = H(n,m) = C(n+m-1, m). [ = #m-comb with rep. from {1,..,n} ] Ex7’ : What is the # of increasing (and strictly increasing) integer functions f: {1,…,m} {1,..,n} ? Sol: H(n,m) = C(m+n-1, m) and C(n,m), resprctively. Ex: if m = 4 and select {2, 32, 5} (I4,I3,I2,I1) = (2, 3,3 5)
Permutations of sets with indistinguished objects. • Permutations of multisets: • S = {x1a1,...,xnan } with S 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 of type 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(m,a1)xC(m-a1,a2 )x …xC(an+an-1, an-1)xC(an,an) = m! / Pai!
Examples Theorem 4: #ways to distribute n distinguished objects into k boxes so that box i contains ni objects = n! /n1!...nk!. • Same as #way to permute S={Box1n1,Box2n2,…,Boxknk} in n positions pf: C(n,n1) ways to put n1 objects into box 1 C(n-n1,n2) ways to put n2 objects into box 2,... C(nk, nk) ways to put nk objects into box k ==> totally n! /n1! ...nk! ways Ex9: 52 cards to 4 players, each with 5 cards. • Same as #ways to permute {E5,S5,W5,N5, Rest32} ==> C(52,5) x C(52-5,5) xC(52-10,5) x C(52-15,5). c.f: #ways to distribute n indistinguishable objects into k distinguished boxes = H(k, n).
5.6 Generating permutations & combinations • Sometimes permutations and combinations need to be generated, instead of just being counted! • Ex1: [salesman problems] • visiting 6 cities without 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. Syk = 100. • sol: find each subset (combination) T of S and check their sum.
Lexicographical ordering of permutations • S={1,2,...,n} • a = a1,a2,..,as ; • b = b1,..,bt : two permutations of S [, possibly with repetitions ]. • Define a < b iff either • 1. s < t and a is a prefix of b (i.e. b = a,b(s+1) ,.. bt) or • 2. $ 1 £ k £ min(s,t) s.t. ak < bk and aj = bj 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.
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.
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 { 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)
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 }.
r-combinations of a set • S={1,...,n}; T1, T2: two r-combinations of S ( r n) • Define T1 < T2 iff increasing sorting listing of T1 < that of T2 in lexicographical order. Eg: T1={1,2,6}, T2={3, 5, 2} be two 3-combinations => T1 < T2 since 1 2 6 < 2 3 5 • Problem: 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}