Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Copyright M.R.K. Krishna Rao 2003 Ch 4. Counting Techniques Counting techniques are important in programming design. EG: How large an array or hash table.

Similar presentations


Presentation on theme: "1 Copyright M.R.K. Krishna Rao 2003 Ch 4. Counting Techniques Counting techniques are important in programming design. EG: How large an array or hash table."— Presentation transcript:

1 1 Copyright M.R.K. Krishna Rao 2003 Ch 4. Counting Techniques Counting techniques are important in programming design. EG: How large an array or hash table or heap is needed? EG: What is the average case complexity of quick-sort? Answers depend on being able to count. Counting is useful in computing the probability of discrete events. EG: What is the probability of winning a lottery?

2 2 Copyright M.R.K. Krishna Rao 2003 Counting Basics Set Cardinalities Interested in answering questions such as: How many bit strings of length n are there? How many bit strings of length n are there? How many ways are there to buy 13 different bagels from a shop that sells 17 types? How many ways are there to buy 13 different bagels from a shop that sells 17 types? How many bit strings of length 11 contain a streak of one type of bit of exact length 7? How many ways can a marriage service match 13 men to 17 women? COMMON THEME: convert to set cardinality problems so each question above is a about counting the number of elements in some set: Q: What is the corresponding set in each case?

3 3 Copyright M.R.K. Krishna Rao 2003 Counting Basics Set Cardinalities A: The set to measure the cardinality of is… How many bit strings of length n are there? How many bit strings of length n are there? –{bit strings of length n} How many ways are there to buy 13 different bagels from a shop that sells 17 types? How many ways are there to buy 13 different bagels from a shop that sells 17 types? –{S  {1, 2, … 17} | |S | = 13 } (…arguable) How many bit strings of length 11 contain a streak of one type of bit of exact length 7? –{length 11 bit strings with 0-streak of length 7}  {length 11 bit strings with 1-streak of length 7} How many ways to match 13 M to 17 W ? –{ f :{1,2,…,13}  {1,2,…,17} | f is 1-to-1}

4 4 Copyright M.R.K. Krishna Rao 2003 Basic Counting Principles The sum rule: If a task can be done in n 1 ways and a second task in n 2 ways, and if these two tasks cannot be done at the same time, then there are n 1 + n 2 ways to do EITHER task. Example: The department will award a free computer to either a ICS student or a ICS professor. How many different choices are there, if there are 530 students and 45 professors? There are 530 + 45 = 575 choices.

5 5 Copyright M.R.K. Krishna Rao 2003 Basic Counting Principles Generalized sum rule: If we have tasks T 1, T 2, …, T m that can be done in n 1, n 2, …, n m ways, respectively, and no two of these tasks can be done at the same time, then there are n 1 + n 2 + … + n m ways to do ONE of these tasks. If we have tasks T 1, T 2, …, T m that can be done in n 1, n 2, …, n m ways, respectively, and no two of these tasks can be done at the same time, then there are n 1 + n 2 + … + n m ways to do ONE of these tasks.Example: A student can choose a computer project from one of the three lists. The three lists contain 23, 15, and 19 possible projects, respectively. How many possible projects are there to choose from? There are 23 + 15 + 19 = 57 projects.

6 6 Copyright M.R.K. Krishna Rao 2003 Basic Counting Principles The product rule: Suppose that a procedure can be broken down into two successive tasks. If there are n 1 ways to do the first task and n 2 ways to do the second task after the first task has been done, then there are n 1 n 2 ways to do the procedure (BOTH the tasks). Example: There are 32 computers in a lab. Each computer has 24 ports. How many different ports to a computer in the lab are there? Solution: There are 32 possibilities to pick the computer and 24 ports to choose on that computer. So there are 32  24 = 768 different ports. Exercise: A student brings 5 shirts and 4 pairs of pants to campus. How many outfits can he wear?

7 7 Copyright M.R.K. Krishna Rao 2003 Basic Counting Principles Generalized product rule: If we have a procedure consisting of sequential tasks T 1, T 2, …, T m that can be done in n 1, n 2, …, n m ways, respectively, then there are n 1  n 2  …  n m ways to carry out the procedure. Example: How many different license plates are there if each plate consists of 3 English letters and 3 digits? Solution: There are 26 possibilities to pick each letter and 10 possibilities to pick each digit. So there are 26  26  26.10.10.10 = 17,576,000 different license plates. Exercise: A Chinese restaurant serves 3 soups, 5 meat entrees and 6 vegetables. A regular priced meal consists of one soup, one meat, and one vegetable. How many different regular meals could one order?

8 8 Copyright M.R.K. Krishna Rao 2003 Basic Counting Principles The sum and product rules can also be phrased in terms of set theory. Sum rule: Let A 1, A 2, …, A m be disjoint sets. Then the number of ways to choose an element from anyone of these sets is |A 1  A 2  …  A m | = |A 1 | + |A 2 | + … + |A m |. Product rule: Let A 1, A 2, …, A m be finite sets. Then the number of ways to choose one element from each set in the order A 1, A 2, …, A m is |A 1  A 2  …  A m | = |A 1 |  |A 2 |  …  |A m |.

9 9 Copyright M.R.K. Krishna Rao 2003 Cardinality of Power Set Q: How many bit strings of length n are there? A: 2 n THM: |P ({1,2,3,…,n})| = 2 n Proof. The set of bit strings of length n is in 1-to-1 correspondence with the P({1,2,3,…,n}) since subsets are represented by length n bit strings.

10 10 Copyright M.R.K. Krishna Rao 2003 Sum and Product Rules together In general, The Sum Rule and The Product Rule should be used in combination. Example: ? Example: In how many ways you can travel from A to C? Solution: 2 possibilities: (a) Go thru B or (b) Not go thru B A C B R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R7R7 Use product rule to compute # of ways to do (a). 3.2=6 There are 2 ways to do (b) –- There are 2 ways to do (b) –- R6 and R7. Use sum rule to compute # of ways to do (a) or (b). 6+2=8

11 11 Copyright M.R.K. Krishna Rao 2003 Sum and Product Rules together Another example: the number of length 11 bit strings with a streak of length exactly 7. Q: Which of the following should be counted: 100110010100110111101011100000000111000000010101111111010

12 12 Copyright M.R.K. Krishna Rao 2003 Sum and Product Rules together Another example: the number of length 11 bit strings with a streak of length exactly 7. Q: Which of the following should be counted: 10011001010No!, longest streak has length 2. 0110111101011 No! Too long. 10000000011 No! Streak too long. 10000000101Yes! 01111111010Yes!

13 13 Copyright M.R.K. Krishna Rao 2003 Sum Rule We are trying to compute the cardinality of: {length 11 bit strings with 0-streak of length 7}  {length 11 bit strings with 1-streak of length 7} Call the first set A and the second set B. Q: Are A and B disjoint?

14 14 Copyright M.R.K. Krishna Rao 2003 Sum Rule A: Yes. If had both a 0-streak and a 1-streak of length 7 each, string would have length at least 14! When counting the cardinality of a disjoint union we use: SUM RULE: If A and B are disjoint, then |A  B| = |A|+|B| By symmetry, in our case A and B have the same cardinality. Therefore the answer would be 2|A|.

15 15 Copyright M.R.K. Krishna Rao 2003 Sum Rule Break up A = {length 11 bit strings with 0-streak of length exactly 7} into more cases and use sum rule: A 1 = {00000001***} (* is either 0 or 1) A 2 = {100000001**} A 3 = {*100000001*} A 4 = {**100000001} A 5 = {***10000000}. Apply sum rule: |A| = |A 1 | +|A 2 | +|A 3 | +|A 4 | +|A 5 |

16 16 Copyright M.R.K. Krishna Rao 2003 Sum and Product Rules together So let’s count each set. A 1 = {00000001***}. There are 3 *’s, each with 2 choices, so product rule gives |A 1 | = 2 3 = 8 A 2 = {100000001**}. There are 2 *’s. Therefore, |A 2 | = 2 2 = 4 A 3 = {*100000001*}, A 4 = {**100000001} Similarly: |A 2 | = |A 3 | = |A 4 | = 4 A 5 = {***10000000}. |A 1 | = |A 5 | = 8 |A| = |A 1 | +|A 2 | +|A 3 | +|A 4 | +|A 5 | = 8+4+4+4+8 = 28. Therefore answer is 56.

17 17 Copyright M.R.K. Krishna Rao 2003 Sum and Product Rules together Another example: Each user on a computer system has a password, which is six to eight characters long, where each character is an uppercase letter or a digit. Each password must contain at least one digit. How many possible passwords are there? Solution: 678 Solution: Let P be the total number of possible passwords, and let P 6, P 7, and P 8 denote the number of possible passwords of length 6; 7, and 8, respectively. 678By the sum rule, P = P 6 + P 7 + P 8. By the product rule, – 6 –the number of strings of six characters is 36 6, and – 6 –the number of strings with no digits is 26 6. – 6 6 6 –Hence, P 6 = 36 6 – 26 6. 7 7 7 8 8 8Similarly, P 7 = 36 7 – 26 7, and P 8 = 36 8 – 26 8. 678Consequently, P = P 6 + P 7 + P 8 = 2,684,483,063,360

18 18 Copyright M.R.K. Krishna Rao 2003 Counting Functions How many ways to match 13M to 17W ? { f :{1,2,…,13}  P {1,2,…,17} | f is 1-to-1} Use product rule thoughtfully. 17 possible output values for f (1) 16 values remain for f (2) …………………………… 17- i + 1 values remain for f (i ) …………………………… 17-13+1=5 values remain for f (13) ANS: 17·16·15 ·14 ·… ·7·6·5 = 17! / 4! Q: In general how many 1-to-1 functions from size k to size n set?

19 19 Copyright M.R.K. Krishna Rao 2003 Counting Functions A: The number of 1-to-1 functions from a size k set to a size n set is n ! / (n - k) ! as long as k is no larger than n. If k > n there are no 1-to-1 functions. Q: How about general functions from size k sets to size n sets?

20 20 Copyright M.R.K. Krishna Rao 2003 Counting Functions A: The number of functions from a size k set to a size n set is n k

21 21 Copyright M.R.K. Krishna Rao 2003 Inclusion-Exclusion The principle of Inclusion-Exclusion generalized the sum rule the the case of non-empty intersection: INCLUSION-EXCLUSION: If A and B are sets, then |A  B | = |A|+|B |- |A  B | This says that when counting all the elements in A or B, if we just add the the sets, we have double-counted the intersection, and must therefore subtract it out.

22 22 Copyright M.R.K. Krishna Rao 2003 Inclusion-Exclusion Visualize. This diagram gives proof Inclusion- Exclusion principle: A-A  B U ABAB B-A  B

23 23 Copyright M.R.K. Krishna Rao 2003 Inclusion-Exclusion Example: 250 students take Java programming, 180 students take discrete math and 60 students take both. How many students are taking either Java programming or discrete math? Solution: A 1 students taking Java programming and A 2 students taking discrete math. A 1  A 2 students taking either Java programming or discrete math. Solution: Let A 1 be a set of students taking Java programming and A 2 be a set of students taking discrete math. A 1  A 2 is the set of students taking either Java programming or discrete math. |A 1  A 2 | = |A 1 | + |A 2 | - |A 1  A 2 | = 250 + 180 - 60 = 370. Therefore, 370 students take either Java programming or discrete math.

24 24 Copyright M.R.K. Krishna Rao 2003 Inclusion-Exclusion How many bit strings of length 8 either start with a 1 or end with 00? Task 1: Construct a string of length 8 that starts with a 1. A 1 Task 1: Construct a string of length 8 that starts with a 1. Let A 1 be a set of these bit strings. There is one way to pick the first bit (1), two ways to pick the second bit (0 or 1), two ways to pick the third bit (0 or 1),... two ways to pick the eighth bit (0 or 1). Product rule: Task 1 can be done in 1  2 7 = 128 ways. |A 1 | = 2 7 = 128

25 25 Copyright M.R.K. Krishna Rao 2003 Inclusion-Exclusion Task 2: Construct a string of length 8 that ends with 00. A 2 Task 2: Construct a string of length 8 that ends with 00. Let A 2 be a set of these bit strings. There are two ways to pick the first bit (0 or 1), two ways to pick the second bit (0 or 1),... two ways to pick the sixth bit (0 or 1), one way to pick the seventh bit (0), and one way to pick the eighth bit (0). Product rule: Task 2 can be done in 2 6 = 64 ways. |A 2 | = 2 6 = 64.

26 26 Copyright M.R.K. Krishna Rao 2003 Inclusion-Exclusion Since there are 128 ways to do Task 1 and 64 ways to do Task 2, does this mean that there are 192 bit strings either starting with 1 or ending with 00 ? No, because here Task 1 and Task 2 can be done at the same time. When we carry out Task 1 and create strings starting with 1, some of these strings end with 00. Therefore, we sometimes do Tasks 1 and 2 at the same time, so the sum rule does not apply but the principle of inclusion-exclusion can be applied.

27 27 Copyright M.R.K. Krishna Rao 2003 Inclusion-Exclusion Principle of inclusion-exclusion: |A 1  A 2 | = |A 1 | + |A 2 | - |A 1  A 2 | |A 1  A 2 | = |A 1 | + |A 2 | - |A 1  A 2 | Compute |A 1  A 2 |, the number of strings start with 1 and end with 00? There is one way to pick the first bit (1), two ways for the second, …, sixth bit (0 or 1), one way for the seventh, eighth bit (0). Product rule: |A 1  A 2 | = 2 5 = 32. Therefore, |A 1  A 2 | = 128 + 64 – 32 = 160.

28 28 Copyright M.R.K. Krishna Rao 2003 Tree Diagrams How many bit strings of length four do not have two consecutive 1s? Task 1Task 2Task 3Task 4 (1 st bit)(2 nd bit) (3 rd bit)(4 th bit) 0 000 1 1 0 1 0 0 1 1 0 0 0 1 1 0 There are 8 strings.

29 29 Copyright M.R.K. Krishna Rao 2003 Blackboard Example for Section 4.1 In a version of the computer language BASIC, the name of a variable is a string of one or two alphanumeric characters, where uppercase and lowercase are not distinguished. (An alphanumeric character is either on of the 26 English letters of on of the 10 digits.) Moreover, a variable name must begin with a letter and must be different from the five strings of two characters that are reserved for programming use. How many different variable names are there in this version of BASIC?

30 30 Copyright M.R.K. Krishna Rao 2003 Blackboard Exercises for Section 4.1 Problem 4.1.19 (a,b,c,f). How many positive integers with exactly three decimal digits are… Problem 4.1.19 (a,b,c,f). How many positive integers with exactly three decimal digits are… –…divisible by 7? –…odd? –…have the same 3 digits? –…are not divisible by 3 or 4? Problem 4.1.49. How many ways can the letters a,b,c,d be arranged such that a is not immediately followed by b? Problem 4.1.49. How many ways can the letters a,b,c,d be arranged such that a is not immediately followed by b?

31 31 Copyright M.R.K. Krishna Rao 2003 4.2 The Pigeonhole Principle The pigeonhole principle is the (rather obvious) statement: If there are n+1 pigeons, which must fit into n pigeonholes then some pigeonhole contains 2 or more pigeons. Less obvious: Why this would ever be useful… Principle often applied in surprising ways.

32 32 Copyright M.R.K. Krishna Rao 2003 Pigeonhole Principle EG: Given 12 or more numbers between 0 and 1 inclusive, there are 2 (or more) numbers x,y who are strictly within 0.1 of each other. Proof. Any pigeonhole principle application involves discovering who are the pigeons, and who are the pigeonholes. In our case: Pigeons: The 12 numbers Pigeonholes: The sets [0,0.1), [0.1,0.2), [0.2,0.3), …,[0.9,1), {1} There are 11 pigeonholes so some x and y fall in one of these sets, and have difference < 0.1

33 33 Copyright M.R.K. Krishna Rao 2003 Pigeonhole Principle Harder Example: In a party of 2 or more people, there are 2 people with the same number of friends in the party. (Assuming you can’t be your own friend and that friendship is mutual.) Proof. Pigeons –the n people (with n > 1). Pigeonholes –the possible number of friends. I.e. the set {0,1,2,3,…n-1}

34 34 Copyright M.R.K. Krishna Rao 2003 Pigeonhole Principle The proof proceeds with 2 cases. There is a pigeonhole which isn’t hit. In that case, left with n -1 remaining pigeonholes, but n pigeons, so done. Every pigeonhole hit. In particular, the friendship numbers n-1 as well as 0 were hit. Thus someone is friends with everyone while someone else is friends with no-one. This is a contradiction so this case cannot happen!  

35 35 Copyright M.R.K. Krishna Rao 2003 Generalized Pigeonhole Principle If N objects are placed into k boxes, there is at least one box containing  N/k  objects. Specialize to N = n+1 and k = n, gives at least  (n+1)/n  = 2 objects in one box, which is the regular pigeonhole principle. Q: Suppose that Tokyo has more than 7,000,000 inhabitants and that the human head contains at most 500,000 hairs. Find a guaranteed minimum number of people in Tokyo that all have the same number of hairs on their heads.

36 36 Copyright M.R.K. Krishna Rao 2003 Generalized Pigeonhole Principle A:  7,000,000 / 500,001  = 14 Example: If there are 11 players in a soccer team that wins 12-0, there must be at least one player in the team who scored at least twice. Example: If you have 6 classes from Saturday to Wednesday, there must be at least one day on which you have at least two classes. Example: In a 36-student class, at least 8 students will get the same letter grade (A, B, C, D, or F).

37 37 Copyright M.R.K. Krishna Rao 2003 Generalized Pigeonhole Principle Example: Assume you have a drawer containing a random distribution of a dozen brown socks and a dozen black socks. It is dark, so how many socks do you have to pick to be sure that among them there is a matching pair? There are two types of socks, so if you pick at least 3 socks, there must be either at least two brown socks or at least two black socks. Generalized pigeonhole principle:  3/2  = 2.

38 38 Copyright M.R.K. Krishna Rao 2003 Generalized Pigeonhole Principle Example: Example: During a period of 75 hours a wrestler plays at least 1 mach an hour, but no more than 125 total matches. Show that there must be a period of some number of consecutive hours during which the wrestler must play exactly 24 matches. Proof: j 1275 j Proof: Let a j be the number of matches played on or before the jth hour. Then, a 1, a 2, …, a 75 is an increasing sequence of integers, with 1 ≤ a j ≤125. 1275 j Moreover, a 1 +24, a 2 +24,…, a 75 +24 is also an increasing sequence of integers, with 25 ≤ a j +24 ≤149.

39 39 Copyright M.R.K. Krishna Rao 2003 Generalized Pigeonhole Principle Proof (cont.): 12 751275 Proof (cont.): The 150 positive integers a 1, a 2, …, a 75, a 1 +24, a 2 +24,…, a 75 +24 are all less than or equal to 149. Hence, by the pigeonhole principle at least two of these integers are equal. 1275 1275 ij Since the integers a 1, a 2, …, a 75 are all distinct and the integers a 1 +24, a 2 +24,…, a 75 +24 are all distinct, there must be indices i and j with a i = a j +24. This means that exactly 24 matches were played from hour j+1 to hour i. Q.E.D.

40 40 Copyright M.R.K. Krishna Rao 2003 Blackboard Exercises for Section 4.2 Problem 4.2.9: Problem 4.2.9: How many students, each of whom comes from one of 50 states, should be enrolled in a university to ensure that there are at least 100 coming from the same state? Problem 4.2.27: There are 38 different time periods during which classes at a university can be scheduled. If there are 677 different classes, how many different rooms will be needed? Show that if S  {1,2,3,…,50} & |S |> 9 then there are at least 2 different 5 element subsets in S all having the same sum. Read and understand Examples 7 and 8 in sec. 4.2.

41 41 Copyright M.R.K. Krishna Rao 2003 4.3 Permutations and Combinations How many ways are there to pick a set of 3 people from a group of 6? There are 6 choices for the first person, 5 for the second one, and 4 for the third one, so there are 6  5  4 = 120 ways to do this. This is not the correct result! For example, picking person C, then person A, and then person E leads to the same group as first picking E, then C, and then A. However, these cases are counted separately in the above equation.

42 42 Copyright M.R.K. Krishna Rao 2003 Permutations and Combinations So how can we compute how many different subsets of people can be picked (that is, we want to disregard the order of picking) ? To find out about this, we need to look at permutations and combinations. A permutation of a set of distinct objects is an ordered arrangement of these objects. An ordered arrangement of r elements of a set is called an r-permutation.

43 43 Copyright M.R.K. Krishna Rao 2003 Permutations and Combinations Example: Let S = {1, 2, 3}. The arrangement 3, 1, 2 is a permutation of S. The arrangement 3, 2 is a 2-permutation of S. The number of r-permutations of a set with n distinct elements is denoted by P(n, r). We can calculate P(n, r) with the product rule: P(n, r) = n  (n – 1)  (n – 2)  …  (n – r + 1). (n choices for the first element, (n – 1) for the second one, (n – 2) for the third one…)

44 44 Copyright M.R.K. Krishna Rao 2003 Permutations and Combinations Example: 8 runners compete for 3 medals (Gold, S, B). How many ways can the race finish? P(8, 3) = 8  7  6 = 336 = (8  7  6  5  4  3  2  1)/(5  4  3  2  1) = (8  7  6  5  4  3  2  1)/(5  4  3  2  1) General formula: P(n, r) = n!/(n – r)! Knowing this, we can return to our initial question: How many ways are there to pick a set of 3 people from a group of 6 (disregarding the order of picking)?

45 45 Copyright M.R.K. Krishna Rao 2003 Permutations and Combinations An r-combination of elements of a set is an unordered selection of r elements from the set. Thus, an r-combination is simply a subset of the set with r elements. Example: Let S = {1, 2, 3, 4}. Then {1, 3, 4} is a 3-combination from S. The number of r-combinations of a set with n distinct elements is denoted by C(n, r). Example: C(4, 2) = 6, since, for example, the 2- combinations of a set {1, 2, 3, 4} are {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}.

46 46 Copyright M.R.K. Krishna Rao 2003 Permutations and Combinations How can we calculate C(n, r)? Consider that we can obtain the r-permutation of a set in the following way: First, we form all the r-combinations of the set (there are C(n, r) such r-combinations). Then, we generate all possible orderings in each of these r-combinations (there are P(r, r) such orderings in each case). Therefore, we have: P(n, r) = C(n, r)  P(r, r)

47 47 Copyright M.R.K. Krishna Rao 2003 Permutations and Combinations C(n, r) = P(n, r)/P(r, r) = n!/(n – r)!/(r!/(r – r)!) = n!/(n – r)!/(r!/(r – r)!) = n!/(r!(n – r)!) = n!/(r!(n – r)!) Now we can answer our initial question: How many ways are there to pick a set of 3 people from a group of 6 (disregarding the order of picking)? C(6, 3) = 6!/(3!  3!) = 720/(6  6) = 720/36 = 20 There are 20 different ways, that is, 20 different groups to be picked.

48 48 Copyright M.R.K. Krishna Rao 2003 Permutations and Combinations Corollary: Let n and r be nonnegative integers with r  n. Then C(n, r) = C(n, n – r). Note that “picking a group of r people from a group of n people” is the same as “splitting a group of n people into a group of r people and another group of (n – r) people”. Please also look at proof on page 252.

49 49 Copyright M.R.K. Krishna Rao 2003 Permutations and Combinations Example: How many programming teams (4 members) can be selected from 10 CS students? C(10, 4) = 10!/(4!  6!) = (10  9  8  7) / (4  3  2)= 210 Example: In how many ways can we select 4 players from 10 to play the first 4 matches, 1 st, 2 nd … of the day? P(10, 4) = 10!/6! = 10  9  8  7= 5040 Example: Example: A soccer club has 8 seniors and 7 juniors. For today’s match, the coach wants to have 6 seniors and 5 seniors on the grass. How many ways can he choose?  C(7, 5) = 8!/(6!  2!)  7!/(5!  2!) = 28  21 = 588 C(8, 6)  C(7, 5) = 8!/(6!  2!)  7!/(5!  2!) = 28  21 = 588

50 50 Permutations and Combinations Several ways of interpreting question “ How many ways are there to buy 13 different bagels out of 17 types? ” 1 st Interpretation: order matters 2 nd Interpretations: order doesn ’ t matter so only care about set of bagels that have at the end, not about the purchasing instructions. ……… ……… same purchase

51 51 Permutations and Combinations Usually 2 nd interpretation is taken, but in other situations, 1 st interpretation makes more sense. EG: Suppose the bagels were destined to the 13 alphabetized children in a kindergarten. ……… ……… Al Bina Charmaine Darlene

52 52 First Interpretation: Number of r-permutations Solution under 1 st interpretation: ……… ……… Al Bina Charmaine Darlene 17 · 16 · 15 · 14 · … · 6 · 5 = P (17,13) = 17!/4! = Number of 1-to-1 Functions (13b + 17g)

53 53 Second Interpretation: Number of r-combinations In the second interpretation of bagel counting problem, order doesn ’ t matter. Answer will be C (17,13) -- rather than P (17,13). = 17!/(13!. 4!)

54 54 String Counting Example EG: How many length 38 ASCII strings are there s.t. –All characters are distinct –The characters ‘ a ’ ‘ b ’ and ‘ c ’ appear in order –(and assume that |ASCII| = 128)

55 55 String Counting Example Method for computing answer: Decide where to put (a,b,c). There are 38 spots, out of which 3 must be chosen together, therefore C (38,3) choices. After 1 has been chosen, 35 spots remain. So there are 125=128-3 choices for the first, 124 for the second, etc. Therefore, P(125,35) choices. Product rule applies to (1) and (2) to obtain the final answer: C (38,3) · P(125,35)

56 56 Copyright M.R.K. Krishna Rao 2003 Combinations We saw the following: This symmetry is intuitively plausible. For example, let us consider a set containing six elements (n = 6). Picking two elements and leaving four is essentially the same as picking four elements and leaving two. In either case, our number of choices is the number of possibilities to divide the set into one set containing two elements and another set containing four elements.

57 57 Copyright M.R.K. Krishna Rao 2003 Combinations Pascal’s Identity: Let n and k be positive integers with n  k. Then C(n + 1, k) = C(n, k – 1) + C(n, k). How can this be explained? What is it good for?

58 58 Copyright M.R.K. Krishna Rao 2003 Combinations Imagine a set S containing n elements and a set T containing (n + 1) elements, namely all elements in S plus a new element a. Calculating C(n + 1, k) is equivalent to answering the question: How many subsets of T containing k items are there? Case I: The subset contains (k – 1) elements of S plus the element a: C(n, k – 1) choices. Case II: The subset contains k elements of S and does not contain a: C(n, k) choices. Sum Rule: C(n + 1, k) = C(n, k – 1) + C(n, k).

59 59 Copyright M.R.K. Krishna Rao 2003 Pascal’s Triangle In Pascal’s triangle, each number is the sum of the numbers to its upper left and upper right: 1 11 121 1331 14641 ………………

60 60 Copyright M.R.K. Krishna Rao 2003 Pascal’s Triangle Since we have C(n + 1, k) = C(n, k – 1) + C(n, k) and C(0, 0) = 1, we can use Pascal’s triangle to simplify the computation of C(n, k): C(0, 0) = 1 C(1, 0) = 1 C(1, 1) = 1 C(2, 0) = 1 C(2, 1) = 2 C(2, 2) = 1 C(3, 0) = 1 C(3, 1) = 3 C(3, 2) = 3 C(3, 3) = 1 C(4, 0) = 1 C(4, 1) = 4 C(4, 2) = 6 C(4, 3) = 4 C(4, 4) = 1 k n

61 61 Copyright M.R.K. Krishna Rao 2003 4.4 Binomial Coefficients Expressions of the form C(n, k) are also called binomial coefficients. How come? A binomial expression is the sum of two terms, such as (a + b). Now consider (a + b) 2 = (a + b)(a + b). When expanding such expressions, we have to form all possible products of a term in the first factor and a term in the second factor: (a + b) 2 = a·a + a·b + b·a + b·b Then we can sum identical terms: (a + b) 2 = a 2 + 2ab + b 2

62 62 Copyright M.R.K. Krishna Rao 2003 Binomial Coefficients For (a + b) 3 = (a + b)(a + b)(a + b) we have (a + b) 3 = aaa + aab + aba + abb + baa + bab + bba + bbb (a + b) 3 = a 3 + 3a 2 b + 3ab 2 + b 3 There is only one term a 3, because there is only one possibility to form it: Choose a from all three factors: C(3, 3) = 1. There is three times the term a 2 b, because there are three possibilities to choose a from two out of the three factors: C(3, 2) = 3. Similarly, there is three times the term ab 2 (C(3, 1) = 3) and once the term b 3 (C(3, 0) = 1).

63 63 Copyright M.R.K. Krishna Rao 2003 Binomial Coefficients This leads us to the following formula: With the help of Pascal’s triangle, this formula can considerably simplify the process of expanding powers of binomial expressions. For example, the fifth row of Pascal’s triangle (1 – 4 – 6 – 4 – 1) helps us to compute (a + b) 4 : (a + b) 4 = a 4 + 4a 3 b + 6a 2 b 2 + 4ab 3 + b 4 (Binomial Theorem)

64 64 Copyright M.R.K. Krishna Rao 2003 Permutations and Combinations What is the coefficient of x 12 y 13 in the expansion of (x+y) 25 ? Example: What is the coefficient of x 12 y 13 in the expansion of (x+y) 25 ? C(25,13) = 25! / (13! 12!) = 5,200,300 What is the coefficient of x 12 y 13 in the expansion of (2x-3y) 25 ? Example: What is the coefficient of x 12 y 13 in the expansion of (2x-3y) 25 ? C(25,13).2 12.(-3) 13 = - (25! 2 12 3 13 ) / (13! 12!) Theorems 4, 5 and 7:… Blackboard Exercises: 14,16,18,20,24,32,48

65 65 Copyright M.R.K. Krishna Rao 2003 4.5 Generalized Permutations and Combinations It turns out the the following algorithmic problem is very important to computer science. In fact, almost every algorithmic problem can be converted to this problem as it is “NP- complete”. Integer Linear Programming: Given integer variable inequalities with integer coefficients, find a solution to all variables simultaneously which maximizes some function. EG: Find integers x,y,z satisfying: x  0, y  0, z  0, x+y+z  136, x+y+z  136 and maximizing f (x) = 36x - 14y + 17z

66 66 Copyright M.R.K. Krishna Rao 2003 Integer Linear Programming Unfortunately, there is no known fast algorithm for solving this problem. In general, forced to try every possibility and keep track of (x,y,z) with current best f (x,y,z). Would like to get an idea at least, of how many non- negative integer solutions there are to x+y+z = 136 before commencing search for best (x,y,z) so have idea of how long solution will take to find.

67 67 Copyright M.R.K. Krishna Rao 2003 Permutations with Repetitions How many strings of length n can be formed using the 26 lower case letters of the alphabet? 26 n Every letter can be used repeatedly, so it is like a n digit number to the base 26 This is an application of the product rule. r The number of r-permutations of a set of n objects with repetition allowed is n r.

68 68 Copyright M.R.K. Krishna Rao 2003 Permutations with Repetitions An anagram of a string is a rearrangement of the letters in the string. EG, ignoring white-space and capitals an anagram on “William Gates” is: “Will I tame gas” -also allow random anagrams such as “liiwlegamast” Q: How many different anagrams are there on the string “williamgates”?

69 69 Copyright M.R.K. Krishna Rao 2003 Permutations with Repetitions A: Alphabetize “ williamgates ” to get: “ aaegiillmstw ” (length = 12) Choose 2 spaces to put the a ’ s: C (12,2) Choose 1 space of remaining for e: C (10,1) Choose 1 of remaining 9 for g: C (9,1) Choose 2 of remaining 8 for i: C (8,2) Choose 2 of remaining 6 for l: C (6,2) Choose 1 of remaining 4 for m: C (4,1) Choose 1 of remaining 3 for s: C (3,1) Choose 1 of remaining 2 for t: C (2,1) Only one choice remains for w ( C(1,1) = 1 )

70 70 Copyright M.R.K. Krishna Rao 2003 Permutations with Repetitions Use product rule obtaining answer: C (12,2)C (10,1)C (9,1)C (8,2)C (6,2)C (4,1)C (3,1) C (2,1) = 66·10·9·28·15·4·3·2 = 59,875,200 In general, if there are n letters and the repetition numbers are a 1, a 2, a 3, …., a k then the number of anagrams is given by n ! / (a 1 ! a 2 ! a 3 ! … a k !) the number of anagrams of “William Gates” is 12! / (2! · 2! · 2!) = 479,001,600 / 8 = 59,875,200

71 71 Copyright M.R.K. Krishna Rao 2003 Permutations with Repetitions Theorem: The number of ways to distribute n distinguishable objects into k distinguishable boxes so that a i objects are placed into box i is n ! / (a 1 ! a 2 ! a 3 ! … a k !). Example: In how many ways can we distribute 40 different books into 4 numbered boxes, with 10 books each? Example: In 40!/(10!. 10!. 10!. 10!) ways.

72 72 Copyright M.R.K. Krishna Rao 2003 Select 4 pieces of fruit from a bowl that contains 5 apples, 5 pears and 5 oranges. 4a, 3a + 1p, 3a + 1o, 2a + 2p, 2a + 1p + 1o 4p, 3p + 1a, 3p + 1o, 2p + 2o, 2p + 1a + 1o 4o, 3o + 1a, 3o + 1p, 2o + 2a, 2o + 1a + 1p 15 ways altogether Combinations with Repetitions

73 73 Copyright M.R.K. Krishna Rao 2003 Reconsider our problem of selecting 4 pieces of fruit where the four pieces are made up of apples, pears and oranges We can think of this as having 3 containers one for apples one for pears one for oranges The sum of the contents has to be 4 We can draw this as “stars and bars” we have 4 stars one for each piece of fruit two bars to give us 3 regions

74 74 Copyright M.R.K. Krishna Rao 2003 apples|pears|oranges   ****| | 4 apples   **|**| 2 apples, 2 pears   **| |** 2 apples, 2 oranges   **|*|* 2 apples, 1 pear, 1 orange   *|**|* 1 apple, 2 pears, 1 orange   *|*| ** 1 apple, 1 pear, 2 oranges and so on Therefore we consider all arrangements of 4 stars and 2 bars (6 objects) Pick 2 positions (out of 6) for 2 bars C(6,2) = 15

75 75 Copyright M.R.K. Krishna Rao 2003 Counting with Repetitions There are C(n+r-1,r) r-combinations from a set of n elements when repetition of elements is allowed. Example: Example: Revisiting the fruit question, we want the number of 4-combinations (4 pieces of fruit, r = 4) from a set with three elements (apples, pears, oranges, n = 3) allowing repetitions. C(n+r-1,r) = C(6,4) = 15. Example: Find the number of non-negative integers solutions to x+y+z = 136. Solution: Solution: C(n+r-1,r) = C(138,136) = 9453. Q: How many ways are there to buy 13 bagels from 17 types with repetitions?

76 76 Copyright M.R.K. Krishna Rao 2003 Counting with Repetitions A: How many ways are there to buy 13 bagels from 17 types? Let x i = no. of bagels bought of type i. Interested in counting the number of solutions to x 1 +x 2 +…+x 17 = 13. Therefore, answer is C (16+13,13) = C (29,13) = 67,863,915. Q: How many solutions in N are there to x 1 +x 2 +x 3 +x 4 +x 5 = 21 if x 1 ≥ 1 ?

77 77 Copyright M.R.K. Krishna Rao 2003 Counting with Repetitions A: x 1 +x 2 +x 3 +x 4 +x 5 = 21 & x 1 ≥ 1 : | {x 1 +x 2 +x 3 +x 4 +x 5 = 21 | x 1 ≥ 1 } | = | {x 1 +x 2 +x 3 +x 4 +x 5 = 20} | This is because one  is forced to be on pile 1, so are asking how many ways are there to distribute remaining 20  ’s. Answer = C (24,4) = 10,626 Q: How many solutions in N are there to x 1 +x 2 +x 3 +x 4 +x 5 = 21 x 1 +x 2 +x 3 +x 4 +x 5 = 21 if x 1 ≥ 2, x 2 ≥ 2, x 3 ≥ 2, x 4 ≥ 2 and x 5 ≥ 2 ?

78 78 Copyright M.R.K. Krishna Rao 2003 Counting with Repetitions A: x 1 +x 2 +x 3 +x 4 +x 5 = 21, x 1 ≥2, x 2 ≥2, x 3 ≥2, x 4 ≥ 2 and x 5 ≥ 2 : Same idea. 2  ’s are forced to remain on each of 5 piles. So this is the same as counting solutions of x 1 +x 2 +x 3 +x 4 +x 5 = 11. So answer is C (15,4) = 1365 Q: How many solutions in N are there to x 1 +x 2 +x 3 +x 4 +x 5 = 21 if x 1 < 11 ? x 1 +x 2 +x 3 +x 4 +x 5 = 21 if x 1 < 11 ?

79 79 Copyright M.R.K. Krishna Rao 2003 Counting with Repetitions A: x 1 +x 2 +x 3 +x 4 +x 5 = 21 & x 1 < 11: | {x 1 +x 2 +x 3 +x 4 +x 5 = 21 | x 1 <11} | = | {all solutions} - {solutions with x 1 ≥ 11} | = C (25,4) - C (14,4) = 11,649 Q: How many solutions in N are there to x 1 +x 2 +x 3 +x 4 +x 5 = 21, x 1 <4, 1≤x 2 <4, x 3 ≥ 15 ? x 1 +x 2 +x 3 +x 4 +x 5 = 21, x 1 <4, 1≤x 2 <4, x 3 ≥ 15 ?

80 80 Copyright M.R.K. Krishna Rao 2003 Counting with Repetitions A: x 1 +x 2 +x 3 +x 4 +x 5 = 21, x 1 <4, 1≤x 2 <4, x 3 ≥ 15 : | {x 1 +x 2 +x 3 +x 4 +x 5 = 21| x 1 <4, 1≤x 2 <4, x 3 ≥ 15 } | = | {x 1 +x 2 +x 3 +x 4 +x 5 = 5| x 1 <4, x 2 <3} | (1  stuck on pile #2 and 15  ’s stuck on #3) So: | {all solutions} | - | {solutions with x 1 ≥ 4 OR x 2 ≥ 3} | Inclusion-Exclusion principle implies: | {x 1 ≥ 4 OR x 2 ≥ 3} |=| {x 1 ≥ 4} |+| {x 2 ≥ 3} |-| {x 1 ≥ 4 AND x 2 ≥ 3} | =C (5,1) + C (6,2) - “C (2,4)” = 5 +15 -0= 20 =C (5,1) + C (6,2) - “C (2,4)” = 5 +15 -0= 20 Number of solutions = C (9,4) - 20 = 126-20 = 106

81 81 Copyright M.R.K. Krishna Rao 2003 Blackboard Exercises for 4.6 How many solutions in N are there to How many solutions in N are there to x 1 +x 2 +x 3 +x 4 +x 5 ≤ 21 ? x 1 +x 2 +x 3 +x 4 +x 5 ≤ 21 ? How many solutions in N are there to x 1 +x 2 +x 3 +x 4 +x 5 > 21 ? x 1 +x 2 +x 3 +x 4 +x 5 > 21 ?


Download ppt "1 Copyright M.R.K. Krishna Rao 2003 Ch 4. Counting Techniques Counting techniques are important in programming design. EG: How large an array or hash table."

Similar presentations


Ads by Google