Topic 0-1 Basic combinatorics For a sample space S with equally probable outcomes, we define the probability of event E as number of elements in E P(E) = number of elements in S When S contains a huge number of elements, however, it becomes practically impossible to write out all the outcomes (in S or even in E) and count them by hand. In such cases, we need to use combinatorics (“sophisticated way of counting”) to perform the counting before we can calculate probabilities. 1. The fundamental principle of counting: For a task with k steps, if there are n1 different ways to perform the first step, n2 different ways to perform the second step, ..., and finally nk different ways to perform the k-th step, then there are n1 n2 ... nk different ways to accomplish the task. The proof is obvious by considering the “tree diagram”: nn21 A task with k steps n1 Step 1 n2 Step 2 Example: A license plate has 3 letters and 4 digits (e.g. HKA 3456). How many different licenses can be formed if repetition of letter or number is not allowed? Solution: Applying the fundamental principle of counting, Position Letter 1 number 26 Letter 2 Letter 3 Digit 1 Digit 2 Digit 3 Digit 4 25 24 10 9 8 7 of choices (1 letter is used already) (can’t use any of previous 2) (can pick any letter) We see there are 26.25.24.10.9.8.7 = 7824000 possible outcomes. Example: r persons are distributed in n rooms randomly. How many possible outcomes are there? Solution: Consider all the r persons lined up. The first person can enter any of the n rooms, so can the second, third, ... r-th person. Hence by the fundamental principle of counting there are a total of n.n.n...n = nr possibilities. Note that the same problem can be messy if one tried to visualize the n rooms being lined up, so choosing the right perspective is often crucial to an efficient solution to the problem. 2. Permutations of n distinct objects Q: Suppose there are n distinct objects, and we wish to permute (i.e. arrange across a line) r of them, where r n. How many possible ways are there? A: Since there are n ways to pick the first object, after which there are n - 1 ways to pick the 2nd, n - 2 ways to pick the 3rd, ..., and finally n - r + 1 ways to pick the r-th object, it follows from the fundamental principle of counting that there are n(n 1)(n 2)...(n r 1) n! permutations of r out of n distinct objects (n r )! This number is also denoted P(n, r) or nPr. Note that if r = n, i.e. all n objects are used (recall 0! = 1), There are n! permutations of n distinct objects Example: How many ways are there to arrange 7 people in a row of 7 chairs? Solution: 7! = 5040 Example: Six men and four women took the same test, and all got different scores. (a) How many different rankings are possible? (b) How many different rankings are possible if men and women are ranked separately? Solution: (a) 10! = 3628800 (b) 6!4! = 17280 3. Permutations of n objects, some of which are indistinguishable Q: Suppose we wish to permute n objects, but n1 of them are alike (i.e. indistinguishable from each other), n2 are alike of another kind, ...., finally nk are alike of a k-th kind. For example, among the 6 letters PEPPER, there are 3 indistinguishable Ps and 2 indistinguishable Es. How many different permutations are there in such cases? A: Let the correct answer be denoted by c. The trick is to pretend we really had all distinguishable Ps (say P1, P2, P3) and Es (E1 and E2, say), then, if there are c arrangements in the indistinguishable case, each arrangement must be further arranged in 3!2! different ways since everything is distinguishable, resulting in a total number of arrangements c 3! 2! which must equal (number of ways to permute 6 different 6! objects) = 6!, hence c = . By the same reasoning, there are 3! 2 ! n! ways to permute n objects, with ni being alike of the i-th kind n1 ! n2 !...... nk ! Example: How many distinct permutations can you form from all the letters of each word: (i) FAST, (ii) NAKAMURA, (iii) PROBABILISTICALLY? Solution: (i) 4! = 24 since all 4 letters are distinct (ii) 8!/3! = 6720 since there are 8 letters among which 3 A’s are indistinguishable (iii) 17!/2!/2!/3!/3! = 2,470,051,584,000 since there are 17 letters among which 2 B’s, 2 A’s, 3 I’s and 3 L’s are identical. 4. Combinations (number of ways to choose things) Q: How many ways are there to choose r out of n different objects without regard to order? A: Suppose the correct answer is c. If the r selected objects could be re-arranged with regard to order, then r! further branches would stem from each of the c options, resulting in a total of cr! ways. On the other hand, we know there are a total of t = n(n-1)(n-2)...(n - r + 1) permutations of r objects out of n. Hence we must have cr! = t, thus c = t/r!, i.e. n(n 1)(n 2)......(n r 1) n! ways to pick r objects out of n r! (n r )! r ! n n! by . This number is called the binomial coefficient; (n r )! r ! r n n n also written as C(n, r) or nCr. Recall that a b a r b n r . r 0 r We often denote To make certain formulas simple and case-independent, we define n n 1, and 0 if i 0 or i n 0 i Example: In how many ways can a committee consisting of 3 men and 2 women be chosen from 7 men and 5 women? Solution: First, to choose 3 men out of 7 there are C(7, 3) ways. Next, to pick 2 women out of 5 there are C(5, 2) ways. So this two-step process has (7.6.5)/(3.2.1) x (5.4)/(2.1) ways of being accomplished. Example: In surveying, the three-point resection method can be used to determine the coordinates of an unknown point by observing from it three stations with known coordinates. The unknown coordinates can then be calculated by a formula (Tienstra’s). For better accuracy and to guard against mistakes, a surveyor plans to observe 5 known stations, apply Tienstra’s formula using 3 stations at a time until he exhausts all possible combinations, and average the results if they all agree well. How many sets of coordinates will he need to calculate before averaging? Solution: To pick 3 out of 5 available stations for each calculation, there are a total of 5!/3!/2! = 10 ways. Example: Soon you will be learning the binomial distribution, one of the most important probability functions. Its derivation will involve the following question: suppose an experiment is repeated n times, and the outcome can only be “success” or “failure” each time. How many different ways are there to have x success (and hence n – x failures) among the n trials? Solution: nCx (imagine you had to pick x “success positions” out of the n available…). Example: A student has to answer 8 out of 10 questions in an exam. (i) How many choices does he have? (ii) How many if he must answer the first 3 questions? Solution: (i) To pick 8 out of 10 questions, there are C(10, 8) = (10.9)/(2.1) = 45 ways. (ii) The first 3 must be answered, so he has to answer 5 out of the remaining 7 and there are C(7, 5) = C(7, 2) = (76)/(21) = 21 ways. Example: Consider a one-dimensional array of N magnets, in which each magnet can only point "up" or "down". (a) How many different configurations can the system have? (b) How many ways are there to have N "up" magnets (and thus N = N - N "down" magnets)? (c) If each "up" magnet carries a spin of +1 and each "down" magnet has spin -1, what are the possible values of the total net spin, S, of the system? (d) For a given S, in how many ways can the system have the same total net spin S? Solution: (a) The first magnet can be either "up" or "down", so can the second magnet, and so on, hence by the fundamental principle of counting, there are 222…2 = 2N different states that the system could assume. (b) One may label the magnets from 1 to N, so the problem is to pick N out of N labels to be "up", with the rest being "down". Hence the answer is N! N ! N ! (this result will be useful when we derive the binomial distribution) (c) S could be any integer between -N ("all-down") and +N ("all-up"). Note that S = N - N (d) Since N = N + N is fixed, specifying N determines N , and hence S, i.e. specifying N "up" magnets enforces a particular net spin S (= N - (N - N) = 2N N). So the answer is the same as that to (b), but rewritten in terms of S (and N), giving N! N S N S ! ! 2 2 Example: A subway train consists of N cars connected in series, m of them being trailer units (TUs) while the rest are electrical units (EUs) powered by the overhead wires. To minimize the chances of immobility in case cars become decoupled, it is required that no TUs are placed next to each other. How many configurations are permissible? (The driver car at each end of the train is ignored in this analysis) Solution: One way to tackle this problem is to line up all the (N - m) EUs, reserving one car space between each pair of EUs. The situation is indicated in the picture below: Space 1 EU1 Space 2 EU2 …… Space (N-m) EUN-m Space (N - m + 1) It is seen that there are (N - m + 1) spaces where it is OK to put in one (or zero) TU; in fact, we will only occupy m of them. So we simply need the number of ways to pick m N m 1 out of (N - m + 1) available spaces, which is . m 5. More complicated situations Facts: there are p 1 (1) solutions to the equation m 1 y1 y 2 ... y m p ; all yi positive integers x1 x 2 ... x m n n m 1 (2) solutions to the equation m 1 all x i non - negative integers Proof: For (1): An equivalent problem is “if there are a total of p (indistinguishable) balls, how many ways are there to distribute them into m boxes, if each box must receive at least one ball?”, which we solve as follows: line up all p balls, and, to the right of each ball (except the very last one) there’s a slot where one could insert a rod as a "separator" to signify the start of the next box. Thus, there are a total of (p - 1) such slots, as indicated by the shaded areas in the first row in the picture below. However, we only need to insert (m - 1) rods (shaded areas in second row) to separate the balls into m groups. Ball # Rod # 1 2 no rod / 1 3 no rod ....... ....... p-1 / m-1 p p 1 Hence there are ways to have the m boxes (each non-empty) carry p balls. m 1 For (2): Take any one solution ( y1 , y2 ,..., ym ) from (1), and construct the vector ( x1 , x 2 ,..., x m ) where xi = yi - 1. Thus, the x’s add up to (p - m), and are all nonp 1 solutions; each is obtained by m 1 negative integers. The underlined problem has p 1 ( p m) m 1 reconstructing a solution from (1). Rewriting as , we see that m 1 m 1 n m 1 there are solutions to the problem “add m non-negative integers together to m 1 have a total of n”. Example: Another way to solve the train problem: refer to the picture below, in which all m TUs are lined up. The x’s denote the number of EU(s) to connect. Since there must be at least one EU between any two adjacent TUs, x2, x3, ... , xm are all positive integers. There can be zero (or more) EU at each end, hence x1 0, xm 0. x1 TU1 x2 TU2 ...... TUm-1 xm TUm xm+1 Note that the x’s must add up to N – m (number of EUs). Now let X1 = x1 + 1, Xm+1 = xm + 1, and Xi = xi for i = 2...m, so the (m + 1) X’s add up to (N - m) + 2 and are all positive integers. This is the situation described by (1). Hence there are ( N m 2) 1 N m 1 = solutions, which is the same answer as before. m (m 1) 1 ********************************************************************* Exercise: Suppose f is a smooth function of k variables, f = f(x1, x2, ..., xk). How many different n-th order partial derivatives of f can be constructed? (This issue comes up in symmetry analysis of PDEs, e.g. see F. Schwarz, Symmetries of Differential Equations: from Sophus Lie to Computer Algebra, SIAM Review 30-3, Sep. 1988) Exercise: An investor has to distribute 20 units of money into 4 investments. How many strategies are possible if (i) all (ii) not all the money must be invested? Assume any investment made must be in integral unit(s) of money including zero. (ans. (i) 1771 (ii) 10626) Exercise: (Ref.: Kittel, Thermal Physics p.24-25): Suppose the energy levels of a quantum-mechanical harmonic oscillator are quantized to n h ( h and being Planck's constant and angular frequency of the oscillator, respectively), where the “quantum number” n can only take on integral values 0,1,2,3,…, etc.. For a system of N such oscillators, with a total energy of = N n h Kh , i 1 i How many ways are there to distribute the total energy Kh among the N oscillators?