Processing math: 100%
Lecture Notes: Counting
Preliminaries
Course Content
Tuesday Preview
Question: How many different burgers are possible?
- Cook time: Rare, Medium, Well-Done
- Bun: Sesame, Plain, Brioche
- Patty: Beef, Turkey, Veggie
- Toppings: Lettuce, Tomato, Onions, Ketchup, Mayo, Mustard
Answer: 33⋅26=1728 possible burgers.
Thursday Review
- Question: How many sequences of four lowercase letters are possible, if the sequence can’t start with “cat” or “dog?”
- Answer: 264−26−26=456,924
- Question: How ways are there to arrange the letters in the word cat? Write them out.
- Answer: 3! = 6 permutations. cat, cta, act, atc, tac, tca.
Motivation
- Password policies are designed to encourage use of strong passwords: passwords which an attacker would have difficulty guessing.
- Ask: Which of these password policies do you think would result in stronger passwords?
- basic20: “Use at least 20 characters.”
- comp8: “Use at least 8 characters, including: a lowercase letter, an uppercase letter, a digit, and a symbol.”
- Answering this question requires more than just math, because the passwords people choose follow patterns: birthdays, animals, common words, etc.
- Researchers at Carnegie Mellon ran studies in which participants wrote passwords according to different password composition policies. They found that that if an attacker was allowed few guesses, the comp8 policy led to fewer successful guesses. But if an attacker was allowed many guesses, the basic20 policy was better. Source
- Draw: A graph showing the number of comp8 passwords guessed exceeding basic20 as the number of guesses increases.
- Ask: What do you think are the most common passwords people used? Source
- 123456
- password
- 12345678
- qwerty
- 123456789
- Suppose that passwords are generated randomly, instead of people choosing them.
- Ask: Which method of generating passwords would result in stronger passwords?
- 20 randomly chosen lowercase letters.
- 8 characters chosen randomly from: lowercase letters, uppercase letters, digits, and symbols.
2620≈2⋅1028
(26+26+10+31)8=938≈6⋅1015
- This is an example of counting, the topic of today’s lecture.
Definition
- Write: Definition: In mathematics, counting means figuring out “how many” using a formula (i.e., without counting one at a time).
- Ask: How many ways are there to roll a total of 6 with two standard six-sided dice?
- Let’s enumerate the possibilities.
- Draw: A 6x6 matrix with rows labeled “First Roll” and columns labeled “Second Roll,” and 1 to 6.
- Next, fill in the cells with the sums.
- Answer: five ways: 1 + 5, 2 + 4, 3 + 3, 4 + 2, 5 + 1
- Ask: For n∈N and n>1, how many ways are there to get a total of n by adding together two numbers between 1 and n?
- If the first number is x, and the second number is y:
- x=1,y=n−1
- x=2,y=n−2
- …
- x=n−2,y=2
- x=n−1,y=1
- Suppose n=10. Then there are n−1=9 different ways.
- Suppose n=2. Then there is only n−1=1 way.
Theorems
- Next, we will see some theorems which are useful for counting.
- Write: Theorem: The Sum Rule. If sets A and B are disjoint, the size of their union is the sum of their sizes:
|A∪B|=|A|+|B|
- Ask: Suppose a class has 8 freshmen and 10 sophomores. How many total students are in the class?
- Ask: Suppose there are 100 balls in a jar, and all are red or green. If there are 37 red balls, how many green balls are there?
- Write: Theorem: The Product Rule. The size of a Cartesian product of two sets is the product of their sizes:
|A×B|=|A|⋅|B|
- Ask: In a class with 8 freshmen and 10 sophomores, suppose the professor wants to form a team with a freshmen and a sophomore. How many possible teams are there?
- We can also form combinations of more than two sets.
- Write: Theorem: The Generalized Product Rule. Let S be a set of sequences of length k. Suppose that there are n1 choices for the 1st element of a sequence, n2 for the 2nd, …, and nk for the kth element. Then
|S|=n1⋅n2⋅…⋅nk
- Ask: How many different license plates are there, if a license plate must be a sequence of 3 letters followed by 3 digits?
26⋅26⋅26⋅10⋅10⋅10=263⋅103=17,576,000
- When we have the same number of choices for each element, the formula is simpler.
- Write: Corollary: The number of length-n strings from an alphabet of size m is mn.
- For example, forming a password from 20 randomly chosen lowercase letters.
Some Challenging Cases
- Write: How many 7-digit phone numbers are there, if a phone number can’t start with a 0, 1, or the sequence 911?
- We need to work through this in multiple steps.
- Step 1: Count the numbers that don’t start with 0 or 1:
P={d1d2d3d4d5d6d7:d1∈{2,…,9},di∈{0,…,9} for 2≤i≤7}
P={2,…,9}×{0,…,9}6
By the product rule:
|P|=8⋅106=8,000,000
- Step 2: We need to remove the numbers which start with 911 from P, since these are invalid. How many seven-digit numbers start with 911?
N={911d4d5d6d7:di∈{0,…,9} for 4≤i≤7}
|N|=13⋅104=10,000
- Step 3: Number of valid phone numbers:
|P|−|N|=8,000,000−10,000=7,990,000
- Counting using subsets is a good technique, but sometimes a creative solution lets you arrive at the solution faster.
- Write: How many odd four-digit numbers are there with no leading zeros and no repeated digits?
- Numbers will have the form n=d1d2d3d4.
- For n to be odd, d4∈{1,3,5,7,9}, so 5 options.
- After choosing d4, we know d1∈{1,…,9}−{d4}, so there are 8 options for d1.
- Next, we know we know d2∈{0,…,9}−{d1,d4}, so there are 8 options for d2.
- Next, we know we know d3∈{0,…,9}−{d1,d2,d4}, so there are 7 options for d3.
- So in total: 8⋅8⋅7⋅5=2240
- It was important to start with d4, to ensure n was odd. If instead we choose numbers d1,d2,d3,d4, then the number of choices for d4 would have depended on which odd numbers had already been used. This would have required analyzing several cases.
Permutations
- Suppose you are trying to count the number of sequences, where each character can only be used one.
- Write: How many ways are there to arrange the 26 lowercase letters? Each letter can only be used one.
- Answer: For the 1st letter, there are 26 choices.
- For the 2nd letter, there are 25 choices.
- For the 3rd letter, there are 24 choices.
- …
- For the 26th letter, there is only one choice.
- So the total number of arrangements is: 26⋅25⋅24⋅…⋅3⋅2⋅1=26!
- The exclamation mark is used to represent the factorial function.
- Write: Definition: n factorial, written n!, is the product of integers from 1 to n inclusive.
- Arrangements of collections are known as permutations: each choice leaves one fewer choice for the next element.
- Write: Definition: A permutation of a set S arranges the elements of S in a list, with each element appearing exactly once.
- Formally, a permutation of set S is a bijection p:{0,1,…,|S|−1}→S which represents the arrangement of the elements of S in the order:
p(0),p(1),…,p(|S|−1)
- p is a bijection, so the list includes each element of S exactly once.
- Consider the set {a, b, c}. Write the six permutations.
- Write: Theorem: The number of permutations of a set of size n is n!.
Cyclically Equivalent
Omitted due to time.
Multisets
- In a set, each element can only appear once.
- How can we count the permutations of a collection containing duplicate elements?
- Write: How many ways can the letters of the word ZOO be arranged?
- Use different colors to differentiate between the Os:
- If we treated each letter differently, then there would be six permutations. But the Os are the same, so there are really only three permutations.
- Before we see the formula for permutations, we need a mathematical way to represent collections with duplicates.
- Write: Definition: A multiset is a pair M=⟨S,μ⟩ where S is a finite set and μ is a function μ:S→N. For any element x∈S, μ(x) is the multiplicity of x in M. The size of M=⟨S,μ⟩ is the sum of the multiplicities of the members of S:
∑x∈Sμ(x)
- For example, for the word ZOO:
S={Z,O}
μ(Z)=1 and μ(O)=2
- Write: Definition: Let M=⟨S,μ⟩ be a multiset of size m. A (multiset) permutation of M is a mapping p from {0,1,…,m−1} to S such that the number of times an element x occurs as the value of p is equal to its multiplicity in M. That is, for each x∈S
|{i:p(i)=x}|=μ(x)
- For example, the sequence OOZ is the permutation p of this multiset with the values: p(0)=O,p(1)=O,p(2)=Z
- Write: Theorem: Let M=⟨S,μ⟩ be a multiset of size m. Then the number of permutations of M is
m!∏x∈Sμ(x)!
- Suppose all the multiplicities are 1. Then the denominator is 1s.
- Suppose there is a single element with multiplicity m. Then the numerator and denominator are both m!.
- For ZOO:
3!1!⋅2!=62=3
- For BANANA:
6!1!⋅2!⋅3!=72012=60
Permutations of Subsets
The following material is from Chapter 23: Counting Subsets.
- Suppose a jar contains 5 marbles of 5 different colors.
- Ask: How many permutations are possible?
- Answer: 5!=120
- But how many orderings are possible if fewer than 5 marbles are selected?
- Write: How many different permutations of 3 marbles can be chosen from a jar containing 5 different marbles?
- Draw: First, let’s write out a few permutations:
- 1, 2, 3, 4, 5
- 1, 2, 3, 5, 4
- 1, 2, 4, 3, 5
- 1, 2, 4, 5, 3
- …
- Now, what if we ignore the elements after the first 3?
- Meaning, if the first 3 elements are the same, we consider the permutations equivalent.
- In our example, for each arrangement of the first 3 elements, there are 2 ways to arrange the remaining 2 elements.
- We know there are 120 possible permutations of 5 elements, so there are 120 ÷ 2 = 60 possible permutations of the first 3 elements.
- Let’s formalize this with a theorem.
- Write: Theorem: Permutations of Subsets. The number of permutations of k elements drawn from a set of size n is denoted nPk and can be calculated:
nPk=n!(n−k)!
- Let’s see a few examples:
- Picking 3 of the 5 marbles: 5P3=5!(5−3)!=5!2!=60
- What if we pick all the marbles? 5P5=5!(5−5)!=5!
- What if we pick 1 marble? 5P1=5!(5−1)!=5!4!=5
- What if we pick 4 marbles? 5P4=5!(5−4)!=5!1!=5!
- Ask: How many different ways are there to award Gold, Silver, and Bronze medals to a group of 10 competitors?
10P3=10!(10−3)!=10!7!=720
Combinations
- So far, we have paid attention to the order of elements.
- But what if order doesn’t matter?
- Write: A jar contains 5 marbles of 5 different colors. If 3 marbles are selected from the jar, how many different sets of marbles are possible?
- There are 5P3=5!2!=60 permutations of 3 marbles chosen from 5.
- Draw: Let’s write out a few permutations:
- 1, 2, 3, 4, 5
- 1, 3, 2, 4, 5
- 2, 1, 3, 4, 5
- 2, 3, 1, 4, 5
- …
- But this overcounts the number of different sets of 3 marbles by the number of different ways 3 marbles can be arranged: 3!
5P33!=5!3!2!=10
- When order doesn’t matter, this is called a combination
- Write: Definition: A combination of k elements from a set S of n elements is a subset of S of cardinality k. The number of combinations of k elements chosen from a set of n distinct elements is denoted nCk or (nk) and is pronounced “n choose k.”
- Write: Theorem: The number of combinations can be calculated:
(nk)=n!k!(n−k)!
- Omitted proof for time.
- Let’s work through a couple examples.
- Write: How many different 7 card starting hands are possible from a standard 52 card deck?
52C7=(527)=52!7!(52−7)!=133,784,560
- Ask: The Powerball lottery draws five white balls (numbered 1-69), then one red ball (numbered 1-26). The order of the white balls doesn’t matter. How many different permutations are possible?
(695)⋅26=69!5!(69−5)!⋅26=292,201,338
- So each time you play, you about a 1 in 300 million chance of winning. Don’t play the lottery!
Partitioning Labeled Subsets
- Write: A jar contains 11 marbles, labeled 1 through 11. In how many ways can they be placed in four boxes, with 4 marbles in the 1st box, 3 marbles in the 2nd box, 2 marbles in the 3rd box, and the 2 remaining marbles in the 4th box?
- There are:
- There are (114) ways to pick 4 marbles for the 1st box.
- From the 7 remaining marbles, there are (73) to pick 3 marbles for the 2nd box.
- From the 4 remaining marbles, there are (42) to pick 2 marbles for the 3rd box.
- From the 2 remaining marbles, there are (22) to pick 2 marbles for the 4th box.
- When the compute the product of these choices, we see that many terms cancel:
11!4!⋅7!⋅7!3!⋅4!⋅4!2!⋅2!⋅2!2!⋅0!=11!4!⋅3!⋅2!⋅2!
- We will formalize this with a theorem.
- Write: Theorem: Let S be a set of n elements, and let k1,k2,…,km be integers such that ∑mi=1ki=n. Then the number of ways to partition S into m disjoint labeled subsets, where the ith subset has size ki, is equal to:
n!k1!⋅k2!⋅…⋅km!
- Ask: How many different ways are there to deal 7 card starting hands to yourself and your friend from a standard 52 card deck?
52!7!⋅7!⋅38!≈6⋅1015
Omitted For Time
The rest of Chapter 23 covers:
- Partitioning into indistinguishable subsets
- Selection with replacement
- Partitioning into parts