Counting Techniques rcombinations with repetition allowed Binomial theorem
Counting Techniques: r-combinations with repetition allowed, Binomial theorem 1
Number of iterations of a nested loop (First Situation) Ø Consider the following nested loop: for i: =1 to n for j: =1 to i-1 for k: =1 to j-1 [ Statements] next k next j next i Ø Question: How many times the statements in the innermost loop will be executed? Ø Solution: Each iteration corresponds to a triple of integers (i, j, k) where i > j > k. The set of all this kind of triples corresponds to all 3 -combinations of {1, …, n}. Thus, the total number of iterations is C(n, 3). 2
Number of iterations of a nested loop (Second Situation) Ø Consider the following nested loop: for i: =1 to n for j: =1 to i for k: =1 to j [ Statements] next k next j next i Ø Question: How many times the statements in the innermost loop will be executed? Ø Solution: Each iteration corresponds to a triple of integers (i, j, k) where i ≥ j ≥ k. Examples: (5, 3, 2), (4, 4, 3), (2, 1, 1), (3, 3, 3). How to count the number of this kind of triples? 3
Number of iterations of a nested loop (Second Situation) Each triple corresponds to a string of crosses and vertical bars. • Numbers 1, 2, …, n are considered as categories; • n-1 vertical bars separate the categories; • 3 crosses indicate how many items from each category are chosen. Examples when n=5: Category 5 4 3 2 1 | | | | | | Triple (5, 3, 2) | (4, 4, 3) | | (2, 1, 1)
Number of iterations of a nested loop (Second Situation) • Each triple corresponds to a string of n-1 vertical bars and 3 crosses. The length of any string is (n-1)+3 = n+2. The number of distinct positions for the 3 crosses in a string is C(n+2, 3). Thus, the number of distinct triples is C(n+2, 3). Ø Generalizing, if the number of nested loops is r then the number of iterations is C(n-1+r, r). 5
r-combinations with repetition allowed • Definition: r-combinations with repetition allowed from a set X of n elements is an unordered selection of r elements taken from X with repetition allowed. • The number of r-combinations with repetition allowed from a set of n elements is C(n-1+r, r). 6
Which formula to use? • We discussed four different ways of choosing r elements from n. • The summary of formulas used in the four situations: Order matters Order does not matter Repetition allowed nr C(n-1+r, r) Repetition not allowed P(n, r) C(n, r)
Useful formulas for special cases • C(n, n-r) = C(n, r). Proof: • C(n, n)=1 • C(n, n-1) = C(n, 1) = n • C(n, n-2) = C(n, 2) = n(n-1) / 2 8
Binomial Theorem: For any real numbers a and b and nonnegative integer n, Proof: By distributive law, (a+b)n can be expanded into products of n letters where each letter is a or b :
Binomial Theorem Proof(cont. ): For each k=0, 1, …, n, the product an-kbk occurs as a term in the sum the same number of times as the number of ways to choose k positions for b’s. But this number is C(n, k). Thus, the coefficient of an-kbk is C(n, k). ■ Examples: (a+b)2 = a 2+C(2, 1)∙ab+b 2 = a 2+2∙ab+b 2 (a+b)3 = a 3+C(3, 1)∙a 2 b+C(3, 2)∙ab 2+b 3 = a 3+3∙a 2 b+3∙ab 2+b 3 10
Using the Binomial Theorem • Question: Which number is larger: (1. 2)4, 000 or 800 ? • Solution: By the binomial theorem, (1. 2)4, 000 = (1+. 2)4, 000 = 14000 + C(4000, 1)∙ 13999∙. 2 + other positive terms = 1 + 4000∙ 1∙. 2 + other positive terms = 1 + 800 + other positive terms > 800 11
Using the Binomial Theorem 12
- Slides: 12