Counting (cmsc 603) Scribed by Naomi Avigdor (class notes for 2-26-99) Counting Strategies: Fundamental Principles of Counting: Addition Rule Product Rule Urn Model (4 cases): Take a counting problem and say that it is one of the 4 cases of the Urn model. Inclusion/Exclusion: Deals with counting when you express the number of items as unions of sets. Polya’s Theory of Enumeration: (e.g. Burnside’s Lemma) (group theory) How many different ways are there? Set up and solve a summation recurrence: example: binary trees. If all fails: Estimate (bound from above an below). ----------------------------------------------------------------------------------------------------------- Fundamental Principle of counting Addition Rule |A OR B| with |A AND B| = empty |A OR B| = |A| + |B| Product Rule |A x B| Use when choices are independent. Example: An outfit consists of one shirt and one pair of pants. A man has three dif- ferent pairs of pants and six different shirts. he can have 3x6=18 different outfits. Inclusion/Exclusion: sets are not necessarily disjoints - |A OR B| = |A| + |B| - |A AND B| Urn Model: An urn contains n different balls numbered 1,2,.....,n. How many ways are there to select k balls? There are four cases: (replacement/ordering) case 1 - (y/y): Each choice is independent. after we pick a ball, we throw it back into the urn and it can be selected again. (namely, with replacement). Ordering is important. for example, if n = 20 and k=3 and we picked the 1st ball as #4, 2nd ball #15 and 3rd ball #9 it is not the same as 1st ball as #15, 2nd ball #4 and 3rd ball #9. (4,15,9) is not the same as (15,4,9). There are n ways to pick the 1st ball. We make the 1st selection and throw the selection back into the urn. There are again n choices to pick the 2nd ball. We select the 2nd ball and throw it back into the urn. There are again n choices to pick the 3rd ball. We pick the 3rd ball out of the n balls, etc. The total number of ways to select k balls out of n with replacement and with ordering is - n * n * n * n * ..... * n = n**k case 2 - (n/y) k permutations: Each choice is not independent. after we pick a ball, we do not throw it back into the urn and it cannot be selected again. (namely, with no replacement). There are n ways to pick the 1st ball. We make the 1st selection and keep the ball outside of the urn. There are now n-1 balls left in the urn out of which we choose the 2nd ball. We make the 2nd selection and keep the 2nd ball outside of the urn. There are now only n-2 choices to pick the 3rd ball. We select the 3rd ball and keep it outside the ball. There are now n-3 choices to pick the 4th ball. etc. (i.e. no replacement). For example: we select k=3 items out of n=6. There are 6 selection for the 1st item, 5 for the 2nd and 4 for the 3rd item = 6 * 5 * 4 ways to select the 3 items. Ordering is important. (see explanation for case 1). The total number of ways to select k balls out of n with no replacement and with ordering is - n! / (n - k)! (called n falling k or k permutations) case 3 - (y/n) D’Alember’s method of counting (Biggs, 4.2 p 66): Each choice is independent. after we pick a ball, we do throw it back into the urn and it can be selected again. (see explanation for case 1) Ordering is not important. A selection of (15,4,9) is the same as a selection of (4,15,9). (see explanation for case 4). The total number of ways to select k balls out of n with no replacement and with ordering is - (n + k - 1) choose (n - 1) = (n + k - 1) choose k. case 4 - (n/n) Combinations: Each choice is dependent. after we pick a ball, we do not throw it back into the urn and it cannot be selected again. (see explanation for case 2). Ordering is not important. (see explanation for case 3). Now if we select k=3 items out of n=6 as we did in case 2, there are n!/(n-k)! = 6x5x4 ways to select the 3 items but every group of 3 items will be counted k!=6 times (say we selected items 2,4 and 5, they will be counted as (2,4,5), (2,5,4), (4,2,5), (4,5,2), (5,2,4), (5,4,2)), we have to divide the total ways to select k out of n by k! to compensate for the duplication that is formed by the insignificance of the ordering. The total number of ways to select k balls out of n with no replacement and with no ordering is - (called combinations) n choose k = n! / (k! * (n - k)!) Example 1 for case 3: There is a total of n colored balls in an urn, all are unique. We select a ball and throw it back into the urn (i.e, with replacement). We repeat this process k times. If order is not important, how many different results can we get? (A result is 3 red balls, 5 green balls and one blue ball in any selection order. We only care to count how many of each color). Answer: Imagine a table with n columns representing the different colors. As we select a ball, we put a check in the appropriate column. At the end of the entire selection process, we have k checks, and vertical column lines separating the different colors. There are n-1 such lines. (one between every two colors, none at the front and none at the end. for exam- ple, if we have n=3 possible colors (3 columns), and a total of k=5 selections such that 2 balls selected are of the 1st color and 3 balls selected are of the 3rd color. Our table will look as follows: vv||vvv. If all balls are of the 2nd color, the table will look as follows: |vvvvv|, etc. As we can see, the total number of checks is k and the total number of column separators is n-1. Together, there are n+k-1 positions to fill with k checks (and the rest are column separators) or n-1 column separators and (the rest are checks). The total number of ways to fill the table is to pick the n-1 column separator positions out of the n+k-1 positions (which is the same as picking the k colors out of the n+k-1 positions). so the solution is: (n + k - 1) choose (n - 1) = (n + k - 1) choose k. Example 2 for case 3: Ancient Crypto machine M-209 - This machine has a total of 27 bars. Each bar consists of 2 lugs and 8 positions that the lugs can be placed in. 2 of the positions are nop and are not distinguishable from each other. The other 6 positions are numbered 1 through 6 and they are all distinguishable from each other. Two lugs cannot be positioned on the same num- ber. How many different codes can be generated by this crypto machine? Answer: We first look at one bar only. there are 3 cases to consider: case 1: both lugs are in the nop position. there is only one case like this. case 2: one of the lugs is in a nop position and the other is on a number. Since the nop positions are not distinguishable, we have 6 different cases where one lug is in any of the nop positions and the other is on one of the 6 numbers. total of 6 possibilities. case 3: both lugs are on number positions, so we choose 2 numbers out of the 6. The total of these 3 cases is: 1 + (6 choose 1) + (6 choose 2) = 22 Now we make an analogy with the urn model and we say that we have an urn with n=22 balls, one for each of the possible arrangement the bars can be in, and we make k=27 selections, one for each of the bars. i.e. we have case 3 of the urn model. We choose with replacement (always 22 ways to configure each bar) and ordering is not important. The total number of ways to configure the crypto machine is therefore: (n + k -1) choose (n -1) = (22 + 27 -1) choose (22 -1) = 28 choose 21. Inclusion/Exclusion: Use Inclusion/Exclusion to solve a counting problem when it is convenient to express what we are counting as a union of sets. |A OR B| = |A| + |B| - |A AND B| |A OR B OR C| = |A| + |B| + |C| - |A AND B| - |A AND C| - |B AND C| + |A AND B AND C| The general case is: Example of Inclusion/Exclusion: How many lists of length k are there that use all of the integers 1 through n at least once? Solution: Solve by including all possible lists minus (exclusion) all bad lists. The bad lists are the ones with a missing integers. Count of all possible lists = n**k Count of bad lists without any 1s = (n - 1)**k Count of bad lists without any 2s = (n - 1)**k Count of bad lists without any ns = (n - 1)**k Let Bj be the set of strings of length k that have no integer j = |B(j)| = (n - 1)**k |B(1) AND B(2)| = (n - 2)**k |B(1) AND B(2) AND ..... AND B((j)| = (n - j)**k The total number of bad strings is: |B(j)| = (n - 1)**k The total number of good string is all the strings minus (exclusion of) the bad string:(n - j)**k Setting up Summations and Recurrences: Consider a loop: for i = 1 to n foo end loop Set up a summation to find out how many times foo is executed: T(n) = sigma (1 to n) 1 If the loop is doubly nested, then: T(n) = [(sigma (1 to n))*(sigma (1 to n)) 1 Example: How many ordered binary search trees are there? Solution: There are 3 types of trees: 1. Empty tree 2. Tree with one node only, namely, a root node. 3. tree with more than 1 node. In this case we have a root, a left tree, and a right tree. If the root is node i, then the left tree is made up of nodes 1 through (i-1) for a total of (i-1) nodes, and the right tree is made up of nodes (i+1) through n for a total of (n-i) nodes. Since the two sub-trees are independent of each other, the total number of trees is the total number of left trees times the total number of right trees (product rule) summed over all possible ways to choose the root (which is n ways). Let T(n) be the number of binary search trees on nodes 1,2,....n then T(n) = 1 if n = 1 = sigma(1 to n) [T(i - 1) * T(n - i)] otherwise Estimate: Break the problem down to smaller sets and estimate a smaller problem. Estimate the accuracy of the estimate. (ex. estimate is 20% accurate, etc.)