Intro to Maths for CS: The Factorial Function, Permutations and Combinations John Barnden School of Computer Science, University of Birmingham Week 8, Term 1, 2013–14 Textbook Segment (Maily:) Segment on Factorials and Combinations in Programme on Binomials: 7th. Ed: Programme F.7 6th. Ed: Programme F.9 CAUTION: The 6th Ed of the book uses the term “combinations” non-standardly to cover permutations as well as combinations. On the other hand, the 7th Ed uses “combinations” as I do in these slides, but prefers the term “arrangements” to “permutations,” even though the latter term is standard. Barnden (SoCS) Intro to Maths for CS: The Factorial Function, Permutations and Combinations Week 8, Term 1, 2013–14 2 / 11 Introduction In maths and CS one very often ends up considering ◮ Different orders in which an algorithm could do some things, including different sequences of choices an algorithm could make about something, or different orderings of things in the world: different permutations. E.g., different orders in which all the students in this class could line up outside my office. ◮ Different subsets of a certain size of a set of things (a special case of the earlier issue of considering different subsets of a set) E.g., how many study-groups of size 5 could be formed from the students in this class? The subsets in this area of maths are usually called combinations. ◮ Issues of probability that involve the above, especially the combinations. Barnden (SoCS) Intro to Maths for CS: The Factorial Function, Permutations and Combinations Week 8, Term 1, 2013–14 3 / 11 Intro, contd How many different orders of N students? There are N possibilities for the first person. For each of these N possibilities, there are N − 1 possibilities for the second. For each of the N(N − 1) possibilities for the first two students, there are N − 2 possibilities for the third. For each of the N(N − 1)(N − 2) possibilities for the first three students, there are N − 3 possibilities for the fourth. And so on, ad collapseam-ex-bordome (a new Latin phrase!) This fascinating process stops at the last student— there’s only one possibility left for who he/she is. RESULT: there are N(N − 1)(N − 2) . . . 1 ways of ordering all N of you outside my office. Barnden (SoCS) Intro to Maths for CS: The Factorial Function, Permutations and Combinations Week 8, Term 1, 2013–14 4 / 11 Factorial Function “N factorial” is N(N − 1)(N − 2) . . . 1. Written N! in ordinary math’l notation. Often pronounced “N shriek”. and one may well shriek ... Barnden (SoCS) Intro to Maths for CS: The Factorial Function, Permutations and Combinations Week 8, Term 1, 2013–14 5 / 11 Growth of Factorial Function: 1 Note: 1! 2! 3! 4! 5! 6! 7! 8! N! = N × (N − 1)! = = = = = = = = 1 2 6 (you’re OK so far) 24 (get slightly nervous) 120 (feel anxious?) 720 (consult shrink) 5,040 (squeak) (quietly shriek) 40,320 20! ≈ 2.43 × 1018 43! ≈ 6.04 × 1052 Barnden (SoCS) (when N > 1 anyway) (VERY LOUDLY SHRIEK) (FALL ON FLOOR) Intro to Maths for CS: The Factorial Function, Permutations and Combinations Week 8, Term 1, 2013–14 6 / 11 A Convention 0! = 1 This is just one of those things that turns out convenient to stipulate: i.e., a convention. We could equally have stipulated that 0! was undefined, but then certain things would have been more awkward to express. One can try to invent some intutive plausibility for it: If I had no students in my class, there would be only one way of ordering them outside my office? A better argument arises from the case of “combinations” — see later. Of course, you might have claimed there are NO ways of ordering an empty set of students—you can’t do any ordering if there aren’t any students. But stipulating 0!=0 would have turned out even more inconvenient than leaving it undefined. Barnden (SoCS) Intro to Maths for CS: The Factorial Function, Permutations and Combinations Week 8, Term 1, 2013–14 7 / 11 Growth of Factorial Function: 2 How big does N! get in general? ASIDE: Stirling’s approximation is that N! is about √ 2πN (N/e)N where e is a special, important constant in maths equal to about 2.718. Hence N! is eventually more-than-exponentially-worse than any exponential function c N for a constant c > 1. The above formula equals √ 2πN (N/ce)N c N So when N > ce we get c N multiplied by a worse-than-exponential function, (N/ce)N , √ not to mention the further multiplication by 2πN. Barnden (SoCS) Intro to Maths for CS: The Factorial Function, Permutations and Combinations Week 8, Term 1, 2013–14 8 / 11 An Observation The particular exponential function e x of x is usually said to be “the exponential function” of x. It’s of special importance. Barnden (SoCS) Intro to Maths for CS: The Factorial Function, Permutations and Combinations Week 8, Term 1, 2013–14 9 / 11 Combinations Consider choosing study-groups of size G from a class of N students. How many possible ways of doing this are there? NOTE: A group doesn’t have an internal ordering. To form a group by sequentially choosing G students: There are N choices for the first student. There are N − 1 choices for the second. There are N − (G − 1) = N − G + 1 choices for the last one. So, we could choose N(N − 1) . . . (N − G + 1) different sequences each of which contains G students. Note that this value is just N!/(N − G )! . (Exercise: invent examples for yourself of that, using various values of N and G .) Barnden (SoCS) Intro to Maths for CS: The Factorial Function, Permutations and Combinations Week 8, Term 1, 2013–14 10 / 11 Combinations, contd But HOLD ON: if we listed all the possible sequences of G students that we could get by that method, we would list each possible combination in all possible orders. There are G ! possible orders for each group. So we’ve listed each group G ! times. So the number of different groups is actually the above number divided by G !, i.e. N! G !(N − G )! This is notated as one of N CG or N G . And observe that this number is the same as the number of study-groups of size N − G . Choosing G people out of N is effectively the same as choosing the other N − G . N CG = N CN−G When G = N we get our motivation for 0! being 1. Barnden (SoCS) Intro to Maths for CS: The Factorial Function, Permutations and Combinations Week 8, Term 1, 2013–14 11 / 11