Uploaded by Isaac Mwankupisha

factorial-perms-combs

advertisement
Intro to Maths for CS:
The Factorial Function, Permutations and
Combinations
Joshua Knowles
School of Computer Science, University of Birmingham
Term 1, 2015-16
(Slides by John Barnden)
Textbook Segment
(Mainly:) 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
Term 1, 2014–15
2 / 15
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, irrespective of their sizes)
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
Term 1, 2014–15
3 / 15
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 collapsio-ex-bordum (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
Term 1, 2014–15
4 / 15
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
Term 1, 2014–15
5 / 15
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
Term 1, 2014–15
6 / 15
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
Term 1, 2014–15
7 / 15
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
N
So when N > ce we get c N multiplied by a worse-than-exponential
function,
(N/ce)
,
√
not to mention the further multiplication by 2πN.
Barnden (SoCS)
Intro to Maths for CS: The Factorial Function, Permutations and Combinations
Term 1, 2014–15
8 / 15
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
Term 1, 2014–15
9 / 15
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
Term 1, 2014–15
10 / 15
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 a motivation for 0! being 1.
Barnden (SoCS)
Intro to Maths for CS: The Factorial Function, Permutations and Combinations
Term 1, 2014–15
11 / 15
Actually Calculating
NC
G
Instead of using
N!
G !(N − G )!
it’s usually easier to use
N(N − 1)(N − 2)...(N − G + 1)
G!
especially when G is fairly small.
Barnden (SoCS)
Intro to Maths for CS: The Factorial Function, Permutations and Combinations
Term 1, 2014–15
12 / 15
An Especially Important Case for Computer Science
An especially important case:
NC
2
=
N(N−1)
2
This crops up in CS in the case, e.g., of sorting algorithms.
There is a concern about the number of pairs of items one can select from those to
be sorted,
as you might end up comparing each item to every other.
But from the above formula, the number of such comparisons is roughly
proportional to N 2 , the number of items.
We can do better than this. Some algorithms only do a number of comparisons that
is roughly proportional to N log2 N.
EXERCISE: Compare N C2 to 10N log2 N
as N takes the values 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024.
Barnden (SoCS)
Intro to Maths for CS: The Factorial Function, Permutations and Combinations
Term 1, 2014–15
13 / 15
Combinations and Probability: A Simple Example
QUESTION: What’s the probability of getting H heads if you flip a fair coin N times?
ANSWER: It’s just the number of possible combinations of size H out of a set of N
coins, divided by the number 2N of possible flip-sequences of N coins.
ANOTHER ANSWER: It’s the number of possible combinations of size H out of N
multiplied by the probability of just the coins in a given combination showing a head.
That probability is 1/(2N ).
This second answer can be adapted to the case of a biased coin (i.e., where the chance of
showing a head is different from 1/2).
Barnden (SoCS)
Intro to Maths for CS: The Factorial Function, Permutations and Combinations
Term 1, 2014–15
14 / 15
Download