Cs1022 computer programming principles
This presentation is the property of its rightful owner.
Sponsored Links
1 / 26

CS1022 Computer Programming & Principles PowerPoint PPT Presentation


  • 77 Views
  • Uploaded on
  • Presentation posted in: General

CS1022 Computer Programming & Principles. Lecture 6.1 Combinatorics (1). Plan of lecture. Motivation Addition principle Multiplication principle Analysing selection problems k -Samples k -Permutations k -Combinations k -Selections Examples. Why combinatorics ?.

Download Presentation

CS1022 Computer Programming & Principles

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cs1022 computer programming principles

CS1022Computer Programming & Principles

Lecture 6.1

Combinatorics (1)


Plan of lecture

Plan of lecture

  • Motivation

  • Addition principle

  • Multiplication principle

  • Analysing selection problems

  • k-Samples

  • k-Permutations

  • k-Combinations

  • k-Selections

  • Examples

CS1022


Why combinatorics

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”?

CS1022


A scenario timetabling

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!

CS1022


Addition principle

Addition principle

  • 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 Bdisjoint 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 is

    n1  n2

CS1022


Multiplication principle

Multiplication principle

  • 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

CS1022


Addition principle with sets

Addition principle with sets

  • Suppose A is the set of n1 options/outcomes

  • Suppose B is the set of n2options/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 AorB

CS1022


Multiplication principle with sets

Multiplication principle with sets

  • 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 setof n2 options/outcomesfor 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|

CS1022


Analysing information needs

Analysing information needs

  • 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

CS1022


Analysing selection problems 1

Analysing selection problems (1)

  • 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?)

CS1022


Analysing selection problems 2

Analysing selection problems (2)

  • 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

CS1022


K samples

k-Samples

  • Consider

    • Selection of k objects from a set of n objects where

    • Order mattersand 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 gives

    n n  ...  n = nkpossible k-samples

k

CS1022


K permutations 1

k-Permutations (1)

  • Consider

    • Selection of k objects from a set of n objects where

    • Order mattersand 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 gives

    P(n, k)  n(n – 1)(n – k 1) n!/ (n – k)!

    possible k-permutations

CS1022


K permutations 2

k-Permutations (2)

  • 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 have

      P(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

CS1022


K combinations 1

k-Combinations (1)

  • Consider

    • Selection of k objects from a set of n objects where

    • Order does not matterand 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

CS1022


K selection 1

k-Selection (1)

  • Consider

    • Selection of k objects from a set of n objects where

    • Order does not matterand 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

CS1022


K selection 2

k-Selection (2)

  • 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)

CS1022


K selection 3

k-Selection (3)

  • 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 is

    C((n k – 1), (n – 1))  (n k – 1)!

    ((n k – 1) – (n – 1))! (n – 1)!

     (n k – 1)!

    k! (n – 1)!

CS1022


K selection 4

k-Selection (4)

  • 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

CS1022


Summarising

Summarising...

  • 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!

CS1022


Example national lottery 1

Example: National Lottery (1)

  • 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

CS1022


Example national lottery 2

Example: National Lottery (2)

  • 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

CS1022


Example choosing people 1

Example: Choosing people (1)

  • 12 candidates for a committee of 5 people

  • How many committees?

    • Order does not matter

    • Elements not repeated

  • There are

    possible committees

CS1022


Example choosing people 2

Example: Choosing people (2)

  • 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

CS1022


Example choosing people 3

Example: Choosing people (3)

  • 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

CS1022


Further reading

Further reading

  • R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd. 2002. (Chapter 6)

  • Wikipedia’s entry

  • Wikibooks entry

CS1022


  • Login