CS1022 Computer Programming & Principles Lecture 6.1 Combinatorics (1)

Post on 11-Dec-2015

217 views 2 download

Tags:

Transcript of CS1022 Computer Programming & Principles Lecture 6.1 Combinatorics (1)

CS1022 Computer Programming &

Principles

Lecture 6.1Combinatorics (1)

Plan of lecture• Motivation• Addition principle• Multiplication principle• Analysing selection problems• k-Samples• k-Permutations• k-Combinations• k-Selections• Examples

2CS1022

Why combinatorics?• We sometimes need to consider the question– How many elements are there in set A?

• If too many elements in a set, and we need to process the set, then this might take too long– What’s “too long”?– Example: how many outfits can one wear?

• Blue trousers and yellow shirt• Black trousers and yellow shirt• Etc.

• When we are proposing a computational solution we need to think of feasible solutions– In realistic situations, will the program finish in “good time”?

3CS1022

A scenario: timetabling• Every term we organise your timetable

– Day/time for lectures, practicals, etc.– Compulsory modules must be allowed– Not all choices possible, though

• How many ways are there to– Arrange lecture, practical and lab times– For all modules, for the whole university,– Using up all time slots, 9AM-5PM?

• Answer: many!• A program to compute a timetable

needs to consider every option– It is going to take a long time...– International timetabling competition!

4CS1022

• Disjoint choices/events don’t influence one another– Choice of cakes in a shop – first choice influences second

one as there will be fewer cakes– Throwing a die twice – 2nd time not influenced by 1st time

• Suppose A and B disjoint choices/events– The number of options/outcomes for A is n1

– The number of options/outcomes for B is n2

• Total number of options/outcomes for A or B isn1 n2

Addition principle

5CS1022

• Suppose a sequence of k choices/events– With n1 options/outcomes for 1st choice/event

– With n2 options/outcomes for 2nd choice/event– ...– With nk options/outcomes for kth choice/event

• Total number of possible options/outcomes for the sequence is

n1 n2 ... nk

Multiplication principle

6CS1022

• Suppose A is the set of n1 options/outcomes

• Suppose B is the set of n2 options/outcomes• Sets A and B are disjoint, that is, A B • So |A B| |A| |B|• That is, A B contains n1 n2 elements– There are n1 n2 possible outcomes for A or B

Addition principle with sets

7CS1022

• We can “interpret” multiplication principle in terms of sets too– Let A1 be set of n1 options/outcomes for 1st choice/event

– Let A2 be set of n2 options/outcomes for 2nd choice/event– ...– Let Ak be set of nk options/outcomes for kth choice/event

• Sequence of k events is an element of Cartesian product A1 A2 ... Ak – Set has cardinality |A1| |A2| ... |Ak|

Multiplication principle with sets

8CS1022

• Suppose we need to create a way to identify cars– What kind of licence plate would you propose?

• Some ways are better than others– How many cars could we licence with a 6-digit licence?– Answer: 1,000,000, that is from 000000 to 999999– UK: 40,000,000 vehicles so we need more digits...

• Alternative – licence with 4 letters and 3 digits– Examples: ABCD 123, GHHH 234, etc.– How many cars can we licence?– Answer: 26 26 26 26 10 10 10 = 456,976,000

Analysing information needs

9CS1022

• Suppose we are offered 3 kinds of sweets– Aniseed drops (A)– Butter mints (B) and – Cherry drops (C)

• How many ways can we choose 2 sweets?– Can we choose the same sweet twice? (AA, BB or CC?)– Does the order matter? (Is BA the same as AB?)

Analysing selection problems (1)

10CS1022

• Cases to consider– Repeats allowed and order matters• 9 possibilities: AA, AB, AC, BA, BB, BC, CA, CB, CC

– Repeats allowed and order does not matter• 6 possibilities: AA, AB, AC, BB, BC, CC

– Repeats not allowed and order matters• 6 possibilities: AB, AC, BA, BC, CA, CB

– Repeats not allowed and order does not matter• 3 possibilities: AB, AC, BC

Analysing selection problems (2)

11CS1022

• Consider – Selection of k objects from a set of n objects where– Order matters and repetition is allowed

• Any such selection is called a k-sample• Since repetition is allowed, – There are n ways of choosing the 1st object, and– There are n ways of choosing the 2nd object, and so on– Until all k objects have been selected

• By the multiplication principle this givesn n ... n = nk possible k-samples

k-Samples

12CS1022k

• Consider – Selection of k objects from a set of n objects where– Order matters and repetition is not allowed

• Any such selection is called a k-permutation– Total number of k-permutations is denoted by P(n, k)

• Since repetition is not allowed, – There are n ways of choosing the 1st object– There are (n – 1) ways of choosing the 2nd object– There are (n – 2) ways of choosing the 3rd object, and so on– Up to (n – k 1) ways of choosing the kth object

• By the multiplication principle this givesP(n, k) n(n – 1)(n – k 1) n!/ (n – k)!

possible k-permutations

k-Permutations (1)

13CS1022

• How many 4-letter “words” can we write with distinct letters from the set a, g, m, o, p, r?

• Solution: – A “word” is any ordered selection of 4 different letters

P(n, k) n! (n – k)!

We have in our scenario n 6 and k 4, so we haveP(6, 4) 6!/ (6 – 4)! 6!/2!

(6 5 4 3 2 1)/(2 1) (6 5 4 3 2 1)/(2 1) 360

possible k-permutations

k-Permutations (2)

14CS1022

• Consider – Selection of k objects from a set of n objects where– Order does not matter and repetition is not allowed

• Any such selection is called a k-combination– Total number of k-combinations is denoted by C(n, k)

• By the multiplication principle:– The number of permutations of k distinct objects selected from n

objects is– The number of unordered ways to select the objects multiplied by the

number of ways to order them• Hence, P(n, k) C(n, k) k! and so there are

C(n, k) P(n, k) n! k! (n – k)! k!

possible k-combinations

k-Combinations (1)

15CS1022

• Consider – Selection of k objects from a set of n objects where– Order does not matter and repetition is allowed

• Any such selection is called a k-selection• Since selection is unordered, – We arrange the k objects so that like objects grouped

together and separate the groups with markers– There are n ways of choosing the 2nd object, and so on

k-Selection (1)

16CS1022

• Example: unordered selection of 5 letters from collection a, b and c, with repetitions– Selection of two a’s, one b and two c’s: aa|b|cc– Selection of one a’s, and four c’s: a| |cccc– Seven slots: five letters and two markers– Different choices: ways to insert 2 markers into 7 slots– That is, C(7, 2)

k-Selection (2)

17CS1022

• Unordered selection of k objects from a set of n objects with repetition allowed requires

(n – 1) markers and k objects• Therefore, there are (n – 1) k slots to fill– The number of k-selections is the number of ways of

putting (n – 1) markers into the (n – 1) k slots• Therefore number of k-selections from n objects isC((n k – 1), (n – 1)) (n k – 1)! ((n k – 1) – (n – 1))! (n – 1)! (n k – 1)! k! (n – 1)!

k-Selection (3)

18CS1022

• Five dice are thrown. How many different outcomes are possible?

• Solution:– Each of the dice shows one of six outcomes– If 5 dice are thrown, the number of outcomes is the

unordered selection of 5 objects with repetition allowed– C((n k – 1), (n – 1)), with n 6 and k 5– This gives

C((6 5 – 1), (6 – 1)) C(10, 5) 10!/(5! 5!) 252

k-Selection (4)

19CS1022

• What we have so far can be summarised as

• We need to analyse the problem to choose formula– They all need as input n and k– If we choose the wrong formula, we still get a result...– But the result will not be the right answer!

Summarising...

20CS1022

Order matters Order does not matterElements repeated k-sample: nk k-selection:

Elements not repeated k-permutation: k-combination:

)!(

!

kn

n

)!1(!

)!1– (

nk

kn

!)!(

!

kkn

n

• Twice-weekly draw selects 6 different numbers– Random selection from {1, 2, ..., 49}– People pick their numbers before draw (and pay for this)

• Let’s calculate the odds of hitting the jackpot– Winning numbers: one of the unordered selection of six

numbers from 49 possibilities– That is, C(49, 6) = 13,983,816 different ways– The odds are 1 to 13,983,816

Example: National Lottery (1)

21CS1022

• Smaller prizes for getting 5, 4 or 3 numbers right– If you choose exactly 3 numbers right you get £10

• What are the odds of getting £10?• Pre-selection of numbers means choosing three

correct and three incorrect numbers– There are C(6,3) ways of selecting 3 correct numbers– There are C(43,3) ways of selecting 3 incorrect numbers– Total number of winning combinations is

– Odds of 13,983,816/246,820 57 to 1

Example: National Lottery (2)

22CS1022

820,246!3!40

!43

!3!3

!6(43,3)(6,3)

CC

• 12 candidates for a committee of 5 people• How many committees?– Order does not matter– Elements not repeated

• There are

possible committees

Example: Choosing people (1)

23CS1022

792!5!7

!12

!5)!512(

!12(12,5)

C

Order matters Order does not matter

Elements repeated nk

Elements not repeated

)!(

!

kn

n

)!1(!

)!1– (

nk

kn

!)!(

!

kkn

n

• We want to know about two people, Mary & Peter• How many committees contain both Mary & Peter?• Reasoning– If Mary & Peter are in the committee, then we need only

consider the remaining 3 people to complete the group– There are 10 people to fill in the 3 places– Order does not matter– Elements not repeated

• There are

possible committees with Mary and Peter in them

Example: Choosing people (2)

24CS1022

120!3!7

!10

!3)!310(

!10(10,3)

C

Order matters Order does not matter

Elements repeated nk

Elements not repeated

)!(

!

kn

n

)!1(!

)!1– (

nk

kn

!)!(

!

kkn

n

• How many do not contain neither Mary or Peter?• Reasoning– If Mary & Peter are excluded, then we have to select 5

people from remaining 10 people– Order does not matter– Elements not repeated

• There are

possible committees without Mary and Peter

Example: Choosing people (3)

25CS1022

253!5!5

!10

!5)!510(

!10(10,5)

C

Order matters Order does not matter

Elements repeated nk

Elements not repeated

)!(

!

kn

n

)!1(!

)!1– (

nk

kn

!)!(

!

kkn

n

Further reading• R. Haggarty. “Discrete Mathematics for

Computing”. Pearson Education Ltd. 2002. (Chapter 6)

• Wikipedia’s entry• Wikibooks entry

26CS1022