2 Combinatorics
2 Combinatorics
Computer Scientists
Insert LaTeX
Auto
Submission
15
Piech, CS109, Stanford University
How Many Bit Stings?
24 start with 01 24 end with 10
Problem: A 6-bit string is sent over a 010000 000010
network. The valid set of strings recognized 010001 000110
by the receiver must either start with "01" 010010 001010
or end with "10". How many such strings 010011 001110
are there? 010100 010010
010101 010110
010110 011010
010111 011110
Answer 011000 100010
011001 100110
011010 101010
011011 101110
011100 110010
011101 110110
011110 111010
011111 111110
Set A Set B
Piech, CS109, Stanford University
End Review
BOBA,ABOB,OBBA…
18
Piech, CS109, Stanford University
Permutations I
19
Orderings of Letters
How many letter orderings are possible for the following strings?
1. CHRIS
This is Jerry’s dog, Doris. She puts her little Doris paw
up to her chin when she’s thinking.
Step 3: Step 5:
Step 1: Chose 5th letter
Chose first letter Chose 3rd letter
Step 2: Step 4:
Chose 2nd letter Chose 4th letter
H C S I R
Step 3: Step 5:
Step 1: Chose 5th letter
Chose first letter Chose 3rd letter
(3 options) (1 option)
(5 options)
Step 2: Step 4:
Chose 2nd letter Chose 4th letter
(4 options) (2 options)
Piech, CS109, Stanford University 23
Permutations
25
Piech, CS109, Stanford University
Unique 6-digit passcodes with six smudges
26
Piech, CS109, Stanford University
Permutations II
27
Summary of Combinatorics
Counting tasks on 𝑛 objects
Distinct
(distinguishable)
Distinct Some
(distinguishable) distinct
𝑛!
1,0,1,0,0
30
Piech, CS109, Stanford University
Sort 𝑛 distinct objects
31
Piech, CS109, Stanford University
Sort 𝑛 distinct objects
32
Piech, CS109, Stanford University
Sort 𝑛 distinct objects
Steps:
1. Choose 1st can 5 options
2. Choose 2nd can 4 options
…
5. Choose 5th can 1 option
33
Piech, CS109, Stanford University
How many ways can we sort coke cans!
# of permutations =
Piech, CS109, Stanford University 35
Order 𝑛
Sort semi-distinct objects distinct objects
𝑛!
permutations Permutations
permutations
of distinct objects = considering some
objects are indistinct
× of just the
indistinct objects
permutations Permutations
___________ =
permutations
of distinct objects
considering some
objects are indistinct
× of just the
indistinct objects
Permutations
of just the
indistinct objects
𝑛!
.
𝑛1 ! 𝑛2 ! ⋯ 𝑛𝑟 !
How many letter orderings are possible for the following strings?
1. BOBA
2. MISSISSIPPI
This is Jerry’s dog, Doris. She puts her little Doris paw
up to her chin when she’s thinking.
How many letter orderings are possible for the following strings?
4!
1. BOBA =
2!
= 12
11!
2. MISSISSIPPI =
1!4!4!2!
= 34,650
boba bboa
boab bbao
bboa boba
bbao boab
baob babo
babo baob
obba abob
obab abbo
obba aobb
obab aobb
oabb abbo
oabb abob
boba bboa
boab bbao
bboa boba
bbao boab
baob babo
babo baob
obba abob
obab abbo
obba aobb
obab aobb
oabb abbo
oabb abob
6!
Total = 5 ×
2!
= 1,800 passcodes
Piech, CS109, Stanford University 46
Summary of Combinatorics
Counting tasks on 𝑛 objects
Distinct Some
(distinguishable) distinct
𝑛!
𝑛!
𝑛1 ! 𝑛2 ! ⋯ 𝑛𝑟 !
Piech, CS109, Stanford University 47
02b_combinations_i
Combinations I
48
Summary of Combinatorics
Counting tasks on 𝑛 objects
Distinct
Distinct Some
(distinguishable) distinct
𝑛!
𝑛!
𝑛1 ! 𝑛2 ! ⋯ 𝑛𝑟 !
Piech, CS109, Stanford University 49
Combinations with cake
There are 𝑛 = 20 people.
How many ways can we choose 𝑘 = 5 people to get cake?
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
1. 𝑛 people
get in line
𝑛! ways
Piech, CS109, Stanford University 51
Combinations with cake
There are 𝑛 = 20 people.
How many ways can we choose 𝑘 = 5 people to get cake?
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
1. 𝑛 people 2. Put first 𝑘
get in line in cake room
𝑛! ways 1 way
Piech, CS109, Stanford University 52
Combinations with cake
There are 𝑛 = 20 people.
How many ways can we choose 𝑘 = 5 people to get cake?
6 7 8 9 10 11 12 13
14 15 16 17 18 19 20
1. 𝑛 people 2. Put first 𝑘
get in line in cake room
𝑛! ways 1 way
Piech, CS109, Stanford University 53
Combinations with cake
There are 𝑛 = 20 people.
How many ways can we choose 𝑘 = 5 people to get cake?
6 7 8 9 10 11 12 13
14 15 16 17 18 19 20
1. 𝑛 people 2. Put first 𝑘 3. Allow cake
get in line in cake room group to mingle
𝑘! different
𝑛! ways 1 way permutations lead to
the same mingle
Piech, CS109, Stanford University 54
Combinations with cake
There are 𝑛 = 20 people.
How many ways can we choose 𝑘 = 5 people to get cake?
6 7 8 9 10 11 12 13
14 15 16 17 18 19 20
1. 𝑛 people 2. Put first 𝑘 3. Allow cake 4. Allow non-cake
get in line in cake room group to mingle group to mingle
𝑘! different
𝑛! ways 1 way permutations lead to
the same mingle
Piech, CS109, Stanford University 55
Combinations with cake
There are 𝑛 = 20 people.
How many ways can we choose 𝑘 = 5 people to get cake?
𝑛! 1 1 𝑛
= 𝑛! × 1 × × =
𝑘! 𝑛 − 𝑘 ! 𝑘! 𝑛−𝑘 ! 𝑘
4. Overcounted:
any ordering
1. Order 𝑛 2. Take first 𝑘 3. Overcounted: of unchosen
distinct as chosen any ordering of group is
objects chosen group is same choice
same choice
Piech, CS109, Stanford University 57
Combinations
𝑛! 1 1 𝑛𝑛 Binomial
= 𝑛! × 1 × × =
𝑘! 𝑛 − 𝑘 ! 𝑘! 𝑛−𝑘 ! 𝑘𝑘 coefficient
𝑛 𝑛
Fun Fact:
𝑘
= 𝑛−𝑘
6 6!
= = 20 ways
3 3! 3!
def main():
def main():
cards = make_deck()
total = math.comb(52, 5)
all_hands = itertools.combinations(cards, 5)
print(total)
for hand in all_hands:
print(hand)
69
Summary of Combinatorics
Counting tasks on 𝑛 objects
Distinct
Distinct Some 𝑛
(distinguishable) distinct Distinct Indistinct
𝑘
𝑛!
𝑛!
𝑛1 ! 𝑛2 ! ⋯ 𝑛𝑟 !
Piech, CS109, Stanford University 70
Balls and urns Hash tables and distinct strings
How many ways are there to hash 𝑛 distinct strings to 𝑟 buckets?
Steps:
1. Bucket 1st string
madagascar japan
2. Bucket 2nd string
italy
south korea …
𝑛. Bucket 𝑛th string
1 2 r 𝑟 𝑛 outcomes
Distinct
Distinct Some 𝑛
(distinguishable) distinct Distinct Indistinct
𝑘
𝑛!
𝑛! 𝑟𝑛
𝑛1 ! 𝑛2 ! ⋯ 𝑛𝑟 !
Piech, CS109, Stanford University 72
Servers and indistinct requests
How many ways are there to distribute 𝑛 indistinct web requests to 𝑟 servers?
request request
Goal
Server 1 has 𝑥1 requests,
request request
Server 2 has 𝑥2 requests,
…
Server 𝑟 has 𝑥𝑟 requests
…
constraint: σ𝑟𝑖=1 𝑥𝑖 = 𝑛
1 2 r
Consider the
following
generative
process…
Piech, CS109, Stanford University 75
The divider method: A generative proof
How many ways can we assign 𝑛 = 5 indistinct children to 𝑟 = 4 distinct
bicycle helmet styles?
Goal Order 𝑛 indistinct objects and 𝑟 − 1 indistinct dividers.
0.
1. Make objects and dividers distinct
1. Order 𝑛 distinct
objects and 𝑟 − 1
distinct dividers
𝑛+𝑟−1 !
Piech, CS109, Stanford University 77
The divider method: A generative proof
How many ways can we assign 𝑛 = 5 indistinct children to 𝑟 = 4 distinct
bicycle helmet styles?
Goal Order 𝑛 indistinct objects and 𝑟 − 1 indistinct dividers.
0.
1. Make objects and dividers distinct
Example solution: 2 + 3 + 2 + 5 + 5 + 3
Distinct
Distinct Some
(distinguishable) distinct Distinct Indistinct
𝑛! 𝑛
𝑛! 𝑟𝑛 𝑛+𝑟−1
𝑛1 ! 𝑛2 ! ⋯ 𝑛𝑟 ! 𝑘
𝑟−1
Piech, CS109, Stanford University 90
Summary of Combinatorics
Counting tasks on 𝑛 objects
Distinct
Distinct Some
(distinguishable) distinct Distinct Indistinct
𝑛! 𝑛
𝑛! 𝑟𝑛 𝑛+𝑟−1
𝑛1 ! 𝑛2 ! ⋯ 𝑛𝑟 ! 𝑘
𝑟−1
Piech, CS109, Stanford University 91
Ready for the first 6 problems (and the rest on Friday)
https://fr.wikipedia.org/wiki/Probl%C3%A8me_des_partis