Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
DD2458, Problem Solving and Programming Under Pressure Lecture 10: Combinatorics Date: 2008-11-17 Scribe(s): John Eriksson, Tony Karlsson Lecturer: Douglas Wikström This lecture will go through different areas in combinatorics such as binomial coefficients, multinomial coefficients, permutations, Stirling numbers, Bell numbers and Catalan numbers. 1 n k Binomial coefficient and Pascals triangle - is called the binomial coefficient or sometimes the binomial numbers and is defined as the number of ways you can choose a subset of k elements from a set of n elements. The binomial coefficient nk can be computed as: n! n = k k!(n − k)! (1) An alternative way to compute the binomial coefficient is the following: ( 1 n = n−1 k k−1 + n−1 k if n = k or k = 0 otherwise (2) Since using the first way to calculate a binomial coefficient would produce very large numbers it is better to simply use the second way to recursively calculate the binomial coefficient the same way it’s done in Pascals triangle. We can see that the construction of Pascals triangle is related to the binomial numbers by the recursive definition above. The definition is exactly how Pascals triangle is built from top to bottom, where an element is the sum of the two closest elements above it. 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 2 4 0 3 0 0 4 1 0 3 1 0 0 2 1 4 2 1 1 1 3 2 2 2 4 3 3 3 4 4 2 1.1 DD2458 – Popup HT 2008 Binomial coefficient algorithm The following algorithm will calculate the binomial coefficient with the help of dynamic programming and memoization. Elements are saved in a matrix T [i][j] where they are initially set to ⊥ . The table is then reused between calls the where n−1 and results of previous calls to calculate nk , and its recursive calls n−1 k , k−1 have been saved in the matrix T [i][j]. The technique of reusing results calculated by previous calls is called memoization. Input: Integers n and k such that 0 ≤ k ≤ n n Output: k Bin(n,k) (1) if T [n, k] = ⊥ (2) if n = k ∨ k = 0 (3) T [n, k] = 1 (4) else (5) T [n, k] ← Bin(n − 1, k − 1) + Bin(n − 1, k) (6) return T [n, k] 1.2 Binomial coefficient algorithm The following algorithm will use only dynamic programming to calculate the binomial coefficient using recursion. Input: Integers n and k such that 0 ≤ k ≤ n Output: nk Bin(n, k) (1) for i = 0 to n (2) T [i, 0] ← T [i, i] ← 1 (3) for i = 2 to n (4) for j = 1 to min(i − 1, k) (5) T [i, j] ← T [i − 1, j − 1] + T [i − 1, j] (6) return T [n, k] 2 Multinomial coefficient Suppose we have t boxes of sizes k1 , . . . , kt . Then the multinomial coefficient n k1 ,...,kt can be interpreted as the number of ways to place the elements 1, . . . , n Pt in these boxes where n = i=1 ki . This can be computed as: n k1 , . . . , kt = n! k1 ! · · · kt ! (3) The multinomial coefficient gets its name from the following equality, where it defines a coefficient. Combinatorics 3 X t n xi X = Pt i=1 i=1 ki =n Y t n xki k1 , . . . , kt i=1 i (4) Since it is too expensive to use equation 3 to calculate the multinomial coefficient due to the large numbers that will be computed by the factorial expressions the following expression can be used, where we just have to use one of the recursive algorithms described in the previous section to calculate the binomial coefficients and multiply them. n k1 , . . . , kt Pt = i=1 k1 ki Pt i=2 k2 ki kt ... kt (5) Equation 5 can be derived from the following argument. Fill the first box by choosing k1 elements from the set of n elements, this can be done in kn1 ways. Next fill the second box by choosing k2 elements from the remaining set of n − k1 1 elements, this can be done in n−k ways and so on until all boxes have been filled. k2 The total number of ways to do this can be seen as the product of the individual binomial expressions. 3 Permutations A permutation of a string x = x1 x2 . . . xn is a rearrangement of that string into a new string. To find all the permutations of the string x one have to find all the possible rearrangements such that no two rearrangements are equal. If x = x1 x2 . . . xn is a string of length n then we can calculate the number of permutations of the string x in the following way. Let fa = number of times the letter a is present in the string x. n (6) fa , fb , . . . An example is the string x = cbaac , then we get fa = 2, fb =51,fc = 2. Which will give us the following number of permutations fa ,fnb ,fc = 2,1,2 = 30. In the special case where all the letters, xi of string x are distinct we get the number of permutations as n!. 3.1 Lexicographical order generation A string x1 x2 . . . xk is given, then the following algorithm will generate all permutations of the string x1 x2 . . . xk in lexicographical order. If the string is sorted then the complete set of permutations in lexicographical order will be generated, otherwise the permutation following the one in lexicographical order of the string will be generated. The algorithm Next first try to find a suffix in reverse lexicographical order then swapping the element xi just before the suffix with the rightmost element larger than xi and finally reversing the suffix. If we for example run the algorithm Next on the string bcfecb we get i = 2 and j = 4. Running Swap we get the string befccb and then running reverse will get the next permutation as bebccf. 4 DD2458 – Popup HT 2008 If we in another example run the algorithm Next on the string dcba we cannot find any such i since this is the last permutation in lexicographical order. Next(x1 x2 . . . xn ) (1) Find i such that xi < xi+1 ≥ . . . ≥ xn (2) Find maximum j such that xj > xi (3) Swap(xj , xj ) (4) Reverse(xi+1 . . . xn ) (5) return x1 x2 . . . xn 3.2 Generating k-subsets If we have a set {1, . . . n} and we want to generate all possible subsets of size k from that set then we can represent the k-subset S as binary string x1 x2 . . . xn where xi = 1 if i ∈ S and xi = 0 if i ∈ S. For example if we have the set {1, 2, 3} the 2-subsets {1, 2}, {1, 3}, {2, 3} can be coded as the binary strings 110, 101, 011. There exists a couple of ways to generate these k-subsets: 1. Generate all subsets, that is all elements of the power set and filter out the ones that are not of size k. 2. Use the binary representation in a string and generate with Next. 3. Generate with a binary version of Next called BinaryNext which uses binary operations on the bits. BinaryNext(x1 x2 . . . xn ) (1) t ← x ∧ (−x) (2) r ←x+t (3) return ((r ⊕ x)/4t) ∨ r 4 Partitions Definition 4.1 A set B = { B1 , B2 , . . . , Bk } is a partition of the set [n] = {1, 2, . . . ,n} if and only if: 1. ∪ki=1 Bi = [n] 2. Bi ∩ Bj = ∅ 3. Bi 6= ∅ That is, we divide the set into one or more blocks where each block contains at least one element and each element belongs to exactly one block. An example partition of [10]: {{1, 5, 8}, {2, 3, 10}, {4}, {6, 7, 9}}. Combinatorics 4.1 5 Stirling numbers How many partitions of [n] in k blocks exists? These numbers are known as Stirling numbers of the second kind. Definition 4.2 The Stirling numbers of the second kind, S(n, k), describe the number of possible ways to partition a set of n elements into k blocks. S(n, 0) = 0 if n > 0 S(n, 1) = 1 S(n, n) = 1 S(n, k) = S(n − 1, k − 1) + kS(n − 1, k) The base cases are natural: You cannot put n elements into 0 blocks. You can put n elements in a single block in exactly one way, and you can put n elements in n blocks in exactly one way (one element in each block). The last line can be explained as follows. When putting n elements in k blocks, we can either put n − 1 of the first elements in k − 1 blocks, letting the last element form its own block. Alternatively, we can put the n − 1 elements in k blocks and put the last element in one of the first blocks. 4.2 Bell numbers What is the total number of ways to partition a set? These numbers are known as Bell numbers. The Bell numbers can be described using the Stirling numbers as follows: B(n) = n X S(n, i) (7) i=0 Alternatively, they can be specified using this recursion formula: B(n + 1) = n X n B(i) n−i i=0 B(0) = 1 (8) (9) The formula can be explained as follows. Consider the block containing the number n + 1. Suppose that block contains i elements other than n + 1. Then we n can choose the elements in the other blocks in n−i ways. We can also partition the rest of the elements in that block in B(i) ways. We can do this for every choice of i from 0 to n. 5 Catalan numbers The Catalan numbers can be used for different purposes. Among them is describing the number of balanced parenthesis expressions for a given number of parenthesis pairs. We will concentrate on the definition where they are explained as follows. 6 DD2458 – Popup HT 2008 Definition 5.1 The Catalan numbers describes the number of ordered binary trees with n nodes. A binary tree is a tree where each node has at most two children. The Catalan numbers can be computed recursively as: ( 1 if n = 0 C(n) = Pn−1 C(k)C(n − 1 − k) otherwise k=0 (10) The formula can be explained as follows. Let Ck (n) denote the number of ordered binary trees that has a total of n nodes, with k nodes in its left subtree. If the left subtree has k nodes, then the right subtree will have n − k − 1 nodes (the total number of nodes minus the ones in the left subtree and the root node). The subtrees can be chosen independently, so we will have Ck (n) = C(k)C(n − k − 1) Pn−1 possible trees. We also know that C(n) = k=0 Ck (n), because the left subtree can be chosen to have 0 to n − 1 nodes. However, it is usually easier to use the fact that the Catalan numbers can be described as: C(n) = 2n n (11) n+1 The ten first (C(0) − C(9)) Catalan numbers are: 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862. 6 Inclusion-Exclusion In order to compute the cardinality of a union A1 ∪ A2 , we can first add the sizes of the two sets and then subtract the number of elements that are contained in both sets: |A1 | + |A2 | − |A1 ∩ A2 |. When computing the cardinality of a union A1 ∪ A2 ∪ A3 , we add the sizes of the individual sets, subtract their pairwise intersection sizes and finally add their total intersection size: |A1 |+|A2 |+|A3 |−|A1 ∩A2 |−|A1 ∩A3 |−|A2 ∩A3 |+|A1 ∩A2 ∩A3 |. The general formula for counting the union cardinality of n sets A1 ∪ A2 ... ∪ An is: X \ (−1)|I|+1 | Ai | (12) |A1 ∪ A2 ... ∪ An | = I⊆[n] i∈I This formula is only T useful in programming contexts for small values of n (up to n ' 20), or when | i∈I Ai | can be computed efficiently.