02: combinatorics - stanford universityโฌยฆย ยท are the same (indistinguishable or indistinct), and...
TRANSCRIPT
02: CombinatoricsLisa Yan and Jerry CainSeptember 16, 2020
1
Lisa Yan and Jerry Cain, CS109, 2020
Quick slide reference
2
3 Permutations II 02a_permutations
17 Combinations I 02b_combinations_i
29 Combinations II 02c_combinations_ii
37 Buckets and The Divider Method LIVE
Todayโs discussion thread: https://us.edstem.org/courses/2678/discussion/124109
Permutations II
3
02a_permutations
Lisa Yan and Jerry Cain, CS109, 2020
Summary of Combinatorics
4
Sort objects(permutations)
Distinct(distinguishable)
Counting tasks on ๐ objects
Choose ๐ objects(combinations)
Put objects in ๐buckets
Lisa Yan and Jerry Cain, CS109, 2020
Sort ๐ distinct objects
5
# of permutations =
Lisa Yan and Jerry Cain, CS109, 2020
Summary of Combinatorics
6
Sort objects(permutations)
Distinct(distinguishable)
Somedistinct
Counting tasks on ๐ objects
Choose ๐ objects(combinations)
Put objects in ๐buckets
๐!
Lisa Yan and Jerry Cain, CS109, 2020
Sort semi-distinct objects
7
All distinct Some indistinct
๐!Order ๐distinct objects
Lisa Yan and Jerry Cain, CS109, 2020
Sort semi-distinct objectsHow do we find the number of permutations considering
some objects are indistinct?
By the product rule, permutations of distinct objects is a two-step process:
8
permutationsof distinct objects
permutationsconsidering some
objects are indistinct
Permutationsof just the
indistinct objectsร=
Lisa Yan and Jerry Cain, CS109, 2020
Sort semi-distinct objects
9
permutationsof distinct objects
permutationsconsidering some
objects are indistinct=___________
How do we find the number of permutations consideringsome objects are indistinct?
By the product rule, permutations of distinct objects is a two-step process:
รPermutations
of just theindistinct objects
Permutationsof just the
indistinct objects
Lisa Yan and Jerry Cain, CS109, 2020
General approach to counting permutationsWhen there are ๐ objects such that
๐! are the same (indistinguishable or indistinct), and๐" are the same, and
โฆ๐# are the same,
The number of unique orderings (permutations) is
๐!๐!! ๐"!โฏ๐#!
.
10
For each group of indistinct objects,Divide by the overcounted permutations.
Lisa Yan and Jerry Cain, CS109, 2020
Sort semi-distinct objects
How many permutations?
11
๐!๐!! ๐"!โฏ๐#!
Order ๐ semi-distinct objects
Lisa Yan and Jerry Cain, CS109, 2020
Summary of Combinatorics
12
Sort objects(permutations)
Distinct(distinguishable)
Somedistinct
Counting tasks on ๐ objects
Choose ๐ objects(combinations)
Put objects in ๐buckets
๐!๐!
๐"! ๐#!โฏ๐$!
Lisa Yan and Jerry Cain, CS109, 2020
Strings
How many orderings of letters are possible for the following strings?
1. BOBA
2. MISSISSIPPI
13
๐!๐!! ๐"!โฏ๐#!
Order ๐ semi-distinct objects
๐ค
Lisa Yan and Jerry Cain, CS109, 2020
Strings
How many orderings of letters are possible for the following strings?
1. BOBA
2. MISSISSIPPI
14
= !!#!= 12
= $$!$!!!!!#!
= 34,650
๐!๐!! ๐"!โฏ๐#!
Order ๐ semi-distinct objects
Lisa Yan and Jerry Cain, CS109, 2020
Unique 6-digit passcodes with six smudges
15
Total = 6!= 720 passcodes
How many unique 6-digit passcodes are possible if a phone password uses each of six distinct numbers?
๐!๐!! ๐"!โฏ๐#!
Order ๐ semi-distinct objects
Lisa Yan and Jerry Cain, CS109, 2020
Unique 6-digit passcodes with five smudges
16
How many unique 6-digit passcodes are possible if a phone password uses each of five distinct numbers?
Steps:1. Choose digit to repeat 5 outcomes2. Create passcode (sort 6 digits:
4 distinct, 2 indistinct)
Total = 5 ร !!#!
= 1,800 passcodes
๐!๐!! ๐"!โฏ๐#!
Order ๐ semi-distinct objects
Combinations I
17
02b_combinations_i
Lisa Yan and Jerry Cain, CS109, 2020
Summary of Combinatorics
18
Sort objects(permutations)
Distinct(distinguishable)
Somedistinct
Distinct
Counting tasks on ๐ objects
Choose ๐ objects(combinations)
Put objects in ๐buckets
๐!๐!
๐"! ๐#!โฏ๐$!
Lisa Yan and Jerry Cain, CS109, 2020
Combinations with cakeThere are ๐ = 20 people.How many ways can we choose ๐ = 5 people to get cake?
19
Consider the following generative processโฆ
Lisa Yan and Jerry Cain, CS109, 2020
Combinations with cakeThere are ๐ = 20 people.How many ways can we choose ๐ = 5 people to get cake?
20
1. ๐ people get in line
1 2 3 4 65 7 8 109
11 12 13 14 1615 17 18 2019
๐! ways
Lisa Yan and Jerry Cain, CS109, 2020
Combinations with cakeThere are ๐ = 20 people.How many ways can we choose ๐ = 5 people to get cake?
21
2. Put first ๐in cake room
1. ๐ people get in line
1 way
1 2 3 4 65 7 8 109
11 12 13 14 1615 17 18 2019
๐! ways
Lisa Yan and Jerry Cain, CS109, 2020
Combinations with cake
22
2. Put first ๐in cake room
1. ๐ people get in line
There are ๐ = 20 people.How many ways can we choose ๐ = 5 people to get cake?
6 7 8 109 11 12 13
14 15 16 17 18 2019
๐! ways 1 way
Lisa Yan and Jerry Cain, CS109, 2020
Combinations with cake
23
๐! different permutations lead to
the same mingle
2. Put first ๐in cake room
1. ๐ people get in line
There are ๐ = 20 people.How many ways can we choose ๐ = 5 people to get cake?
3. Allow cake group to mingle
6 7 8 109 11 12 13
14 15 16 17 18 2019
๐! ways 1 way
Lisa Yan and Jerry Cain, CS109, 2020
Combinations with cake
24
4. Allow non-cake group to mingle
3. Allow cake group to mingle
2. Put first ๐in cake room
1. ๐ people get in line
There are ๐ = 20 people.How many ways can we choose ๐ = 5 people to get cake?
6 7 8 109 11 12 13
14 15 16 17 18 2019
๐! ways 1 way๐! different
permutations lead to the same mingle
Lisa Yan and Jerry Cain, CS109, 2020
Combinations with cake
25
4. Allow non-cake group to mingle๐ โ ๐ ! different
permutations lead to the same mingle
3. Allow cake group to mingle
2. Put first ๐in cake room
1. ๐ people get in line
There are ๐ = 20 people.How many ways can we choose ๐ = 5 people to get cake?
๐! ways 1 way๐! different
permutations lead to the same mingle
Lisa Yan and Jerry Cain, CS109, 2020
Combinations
A combination is an unordered selection of ๐ objectsfrom a set of ๐ distinct objects.
The number of ways of making this selection is
๐!๐! ๐ โ ๐ !
= ๐! ร 1 ร1๐!ร
1๐ โ ๐ !
=๐๐
26
1. Order ๐distinct objects
2. Take first ๐as chosen
3. Overcounted:any ordering of chosen group is same choice
4. Overcounted:any ordering of unchosen group is same choice
Lisa Yan and Jerry Cain, CS109, 2020
Combinations
A combination is an unordered selection of ๐ objectsfrom a set of ๐ distinct objects.
The number of ways of making this selection is
๐!๐! ๐ โ ๐ !
= ๐! ร 1 ร1๐!ร
1๐ โ ๐ !
=๐๐
Note: !
!"# = !#
27
Binomial coefficient
๐๐
Lisa Yan and Jerry Cain, CS109, 2020
Probability textbooks
How many ways are there to choose 3 booksfrom a set of 6 distinct books?
28
ways63
=6!3! 3!
= 20
๐๐
Choose ๐ of ๐ distinct objects
Combinations II
29
02c_combinations_ii
Lisa Yan and Jerry Cain, CS109, 2020
Summary of Combinatorics
30
Sort objects(permutations)
Distinct(distinguishable)
Somedistinct
Distinct
1 group ๐ groups
Counting tasks on ๐ objects
Choose ๐ objects(combinations)
Put objects in ๐buckets
๐๐๐!
๐!๐"! ๐#!โฏ๐$!
Lisa Yan and Jerry Cain, CS109, 2020
General approach to combinations
The number of ways to choose ๐ groups of ๐ distinct objects such thatFor all ๐ = 1,โฆ , ๐, group ๐ has size ๐&, andโ&'!# ๐& = ๐ (all objects are assigned), is
๐!๐!! ๐"!โฏ๐#!
=๐
๐!, ๐", โฏ , ๐#
31
Multinomial coefficient
Lisa Yan and Jerry Cain, CS109, 2020
Datacenters
32
A. !"#,%," = 60,060
B. !"#
$%
"" = 60,060
C. 6 โ 1001 โ 10 = 60,060D. A and BE. All of the above
Datacenter # machinesA 6B 4C 3
13 different computers are to be allocated to 3 datacenters as shown in the table:How many different divisions are possible?
๐๐!, ๐", โฏ , ๐#
Choose ๐ of ๐ distinct objects into ๐ groups of size ๐!, โฆ ๐"
๐ค
Lisa Yan and Jerry Cain, CS109, 2020
Datacenters
33
A. !"#,%," = 60,060
B. !"#
$%
"" = 60,060
C. 6 โ 1001 โ 10 = 60,060D. A and BE. All of the above
Datacenter # machinesA 6B 4C 3
13 different computers are to be allocated to 3 datacenters as shown in the table:How many different divisions are possible?
๐๐!, ๐", โฏ , ๐#
Choose ๐ of ๐ distinct objects into ๐ groups of size ๐!, โฆ ๐"
Lisa Yan and Jerry Cain, CS109, 2020
Datacenters
34
Datacenter # machinesA 6B 4C 3
13 different computers are to be allocated to 3 datacenters as shown in the table:How many different divisions are possible?
๐๐!, ๐", โฏ , ๐#
Choose ๐ of ๐ distinct objects into ๐ groups of size ๐!, โฆ ๐"
A. !"#,%," = 60,060
Strategy: Combinations into 3 groupsGroup 1 (datacenter A): ๐" = 6Group 2 (datacenter B): ๐# = 4Group 3 (datacenter C): ๐) = 3
Lisa Yan and Jerry Cain, CS109, 2020
Datacenters
35
A. !"#,%," = 60,060
Datacenter # machinesA 6B 4C 3
13 different computers are to be allocated to 3 datacenters as shown in the table:How many different divisions are possible?
๐๐!, ๐", โฏ , ๐#
Choose ๐ of ๐ distinct objects into ๐ groups of size ๐!, โฆ ๐"
B. !"#
$%
"" = 60,060
Strategy: Combinations into 3 groupsGroup 1 (datacenter A): ๐" = 6Group 2 (datacenter B): ๐# = 4Group 3 (datacenter C): ๐) = 3
Strategy: Product rule with 3 steps1. Choose 6 computers for A $%
&2. Choose 4 computers for B '
(3. Choose 3 computers for C %
%
Lisa Yan and Jerry Cain, CS109, 2020
Datacenters
36
A. !"#,%," = 60,060
Datacenter # machinesA 6B 4C 3
13 different computers are to be allocated to 3 datacenters as shown in the table:How many different divisions are possible?
๐๐!, ๐", โฏ , ๐#
Choose ๐ of ๐ distinct objects into ๐ groups of size ๐!, โฆ ๐"
B. !"#
$%
"" = 60,060
Strategy: Combinations into 3 groupsGroup 1 (datacenter A): ๐" = 6Group 2 (datacenter B): ๐# = 4Group 3 (datacenter C): ๐) = 3
Strategy: Product rule with 3 steps1. Choose 6 computers for A $%
&2. Choose 4 computers for B '
(3. Choose 3 computers for C %
%
Your approach will determine if you use binomial/multinomial coefficients or factorials.
02: Combinatorics (live)Lisa YanSeptember 16, 2020
37
Lisa Yan and Jerry Cain, CS109, 2020
Reminders: Lecture withโข Turn on your camera if you are able, mute your mic in the big roomโข Virtual backgrounds are encouraged (classroom-appropriate)
38
Todayโs discussion thread: https://us.edstem.org/courses/2678/discussion/124109
Lisa Yan and Jerry Cain, CS109, 2020
Summary of Combinatorics
39
Sort objects(permutations)
Distinct(distinguishable)
Somedistinct
Distinct
1 group ๐ groups๐!
๐"! ๐#!โฏ๐$!๐๐
๐๐", ๐#, โฏ , ๐$
๐!
Counting tasks on ๐ objects
Choose ๐ objects(combinations)
Put objects in ๐buckets
Review
Lisa Yan and Jerry Cain, CS109, 2020
Summary of Combinatorics
40
Sort objects(permutations)
Distinct(distinguishable)
Somedistinct
Distinct
1 group ๐ groups
Indistinct?
Counting tasks on ๐ objects
Choose ๐ objects(combinations)
Put objects in ๐buckets
๐๐
๐๐", ๐#, โฏ , ๐$
๐!๐!
๐"! ๐#!โฏ๐$!
Think Slide 42 is a question to think over by yourself (~1min).
Post any clarifications here or in chat!https://us.edstem.org/courses/2678/discussion/124109
41
๐ค(by yourself)
Lisa Yan and Jerry Cain, CS109, 2020
A trick questionHow many distinct (distinguishable) ways are there to group 6 indistinct(indistinguishable) objects into 3 groups, where groups A, B, and C have sizes 1, 2, and 3, respectively?
42
A. !",$,%
B. !!"!$!%!
C. 0
D. 1E. Both A and BF. Something else
๐ค(by yourself)
Lisa Yan and Jerry Cain, CS109, 2020
A trick questionHow many distinct (distinguishable) ways are there to group 6 indistinct(indistinguishable) objects into 3 groups, where groups A, B, and C have sizes 1, 2, and 3, respectively?
43
A. !",$,%
B. !!"!$!%!
C. 0
D. 1E. Both A and BF. Something else
A (fits 1) B (fits 2) C (fits 3)
Lisa Yan and Jerry Cain, CS109, 2020
Probability textbooks1. How many ways are there to choose 3 books
from a set of 6 distinct books?
44
๐๐
Choose ๐ of ๐ distinct objectsReview
Think Slide 46 is a question to think over by yourself (~2min).
Post any clarifications here or in chat!https://us.edstem.org/courses/2678/discussion/124109
45
๐ค(by yourself)
Lisa Yan and Jerry Cain, CS109, 2020
Probability textbooks1. How many ways are there to choose 3 books
from a set of 6 distinct books?
2. What if we do not want to read both the 9th and 10th edition of Ross?
46
ways63
=6!3! 3!
= 20
A. !" โ !
# = 5 ways
B. !!"!"!#!
= 10
C. 2 โ %# + %
" = 16
๐๐
Choose ๐ of ๐ distinct objects
Ask: https://us.edstem.org/courses/2678/discussion/124109 ๐ค
D. !" โ %
& = 16
E. Both C and D
F. Something else
Lisa Yan and Jerry Cain, CS109, 2020
Probability textbooks1. How many ways are there to choose 3 books
from a set of 6 distinct books?
2. What if we do not want to read both the 9th and 10th edition of Ross?
47
ways63
=6!3! 3!
= 20
๐๐
Choose ๐ of ๐ distinct objects
Strategy 1: Sum Rule
Lisa Yan and Jerry Cain, CS109, 2020
Probability textbooks1. How many ways are there to choose 3 books
from a set of 6 distinct books?
2. What if we do not want to read both the 9th and 10th edition of Ross?
48
ways63
=6!3! 3!
= 20
๐๐
Choose ๐ of ๐ distinct objects
Strategy 2: โForbidden methodโ (unofficial name)
Forbidden method: It is sometimes easier to exclude invalid cases than to include cases.
Interlude for jokes/announcements
49
Lisa Yan and Jerry Cain, CS109, 2020
Announcements
50
Problem Set #1
Out: todayDue: Friday 9/25, 1:00pmCovers: through Friday 9/18
Python tutorial (2 timeslots)
When: Friday 12:00-1:00am PTFriday 2:00-3:00pm PT
Recorded? yesNotes: to be posted online
Getting help
Ed discussion: find study buddies!Office hours: start todayhttps://web.stanford.edu/class/cs109/stanfo
rd/staff.html
Section sign-ups/Acquaintance form
Form released: later todayForm due: Saturday 5:00pm 9/19Results: latest Sunday
Buckets and The Divider Method
51
Lisa Yan and Jerry Cain, CS109, 2020
Summary of Combinatorics
52
Sort objects(permutations)
Distinct(distinguishable)
Somedistinct Distinct Indistinct
Distinct
1 group ๐ groups
Counting tasks on ๐ objects
Choose ๐ objects(combinations)
Put objects in ๐buckets
๐๐
๐๐", ๐#, โฏ , ๐$
๐!๐!
๐"! ๐#!โฏ๐$!
Lisa Yan and Jerry Cain, CS109, 2020
Steps:1. Bucket 1st string2. Bucket 2nd string
โฆ๐. Bucket ๐th string
๐* outcomes
Balls and urns Hash tables and distinct strings
53
ssdfsskdfsd
viridian city
fooandbaz
boba
1 2 r
How many ways are there to hash ๐ distinct strings to ๐ buckets?
โฆ
Lisa Yan and Jerry Cain, CS109, 2020
Summary of Combinatorics
54
Sort objects(permutations)
Distinct(distinguishable)
Somedistinct Distinct Indistinct
Distinct
1 group ๐ groups
Counting tasks on ๐ objects
Choose ๐ objects(combinations)
Put objects in ๐buckets
๐๐
๐๐", ๐#, โฏ , ๐$
๐*๐!๐!
๐"! ๐#!โฏ๐$!
Lisa Yan and Jerry Cain, CS109, 2020
Servers and indistinct requests
55
1 2 r
How many ways are there to distribute ๐ indistinct web requests to ๐ servers?
โฆ
request
request
request
request
GoalServer 1 has ๐ฅ! requests,Server 2 has ๐ฅ" requests,โฆServer ๐ has ๐ฅ# requests (the rest)
Lisa Yan and Jerry Cain, CS109, 2020
Simple example: ๐ = 3 requests and ๐ = 2 servers
56
Lisa Yan and Jerry Cain, CS109, 2020
Bicycle helmet salesHow many ways can we assign ๐ = 5 indistinct children to ๐ = 4 distinct bicycle helmet styles?
57
Lisa Yan and Jerry Cain, CS109, 2020
Bicycle helmet sales1 possible assignment outcome:
58
Goal Order ๐ indistinct objects and ๐ โ 1 indistinct dividers.
Consider the following generative processโฆ
Lisa Yan and Jerry Cain, CS109, 2020
The divider method: A generative proofHow many ways can we assign ๐ = 5 indistinct children to ๐ = 4 distinct bicycle helmet styles?
59
1. Make objects and dividers distinct0.
Goal Order ๐ indistinct objects and ๐ โ 1 indistinct dividers.
Lisa Yan and Jerry Cain, CS109, 2020
The divider method: A generative proofHow many ways can we assign ๐ = 5 indistinct children to ๐ = 4 distinct bicycle helmet styles?
60
๐ + ๐ โ 1 !
1. Make objects and dividers distinct0.
Goal Order ๐ indistinct objects and ๐ โ 1 indistinct dividers.
1. Order ๐ distinct objects and ๐ โ 1distinct dividers
Lisa Yan and Jerry Cain, CS109, 2020
The divider method: A generative proofHow many ways can we assign ๐ = 5 indistinct children to ๐ = 4 distinct bicycle helmet styles?
61
๐ + ๐ โ 1 !
2. Make ๐ objects indistinct
1. Make objects and dividers distinct0.
Goal Order ๐ indistinct objects and ๐ โ 1 indistinct dividers.
1. Order ๐ distinct objects and ๐ โ 1distinct dividers 1
๐!
Lisa Yan and Jerry Cain, CS109, 2020
The divider method: A generative proofHow many ways can we assign ๐ = 5 indistinct children to ๐ = 4 distinct bicycle helmet styles?
62
2. Make ๐ objects indistinct
1๐!
1๐ โ 1 !
Goal Order ๐ indistinct objects and ๐ โ 1 indistinct dividers.
3. Make ๐ โ 1 dividers indistinct
๐ + ๐ โ 1 !
1. Order ๐ distinct objects and ๐ โ 1distinct dividers
1. Make objects and dividers distinct0.
Lisa Yan and Jerry Cain, CS109, 2020
The divider method
The number of ways to distribute ๐ indistinct objects into ๐ buckets is equivalent to the number of ways to permute ๐ + ๐ โ 1 objects such that
๐ are indistinct objects, and๐ โ 1 are indistinct dividers:
63
Total = (๐ + ๐ โ 1)! ร&!!
ร&
("& !
๐ + ๐ โ 1๐ โ 1
= outcomes
Lisa Yan and Jerry Cain, CS109, 2020
Positive integer equations can be solved with the divider method.
Integer solutions to equationsHow many integer solutions are there to the following equation:
๐ฅ! + ๐ฅ" +โฏ+ ๐ฅ# = ๐,
where for all ๐, ๐ฅ& is an integer such that 0 โค ๐ฅ& โค ๐?
64
๐ + ๐ โ 1๐ โ 1
Divider method(๐ indistinct objects, ๐ buckets)
Lisa Yan and Jerry Cain, CS109, 2020
Lecture withBreakout Rooms for working through lecture exercises
โฆ We may incorporate some of these during lectureโฆ You are always welcome to exit breakout rooms if you are more comfortable staying in the main room
65
Breakout Rooms
Introduce yourself!
Then check out the three questions on the next slide (Slide 67).Post any clarifications here:https://us.edstem.org/courses/2678/discussion/124109
Breakout Room time: 5 minutes
Weโll then all come back as a big group to go over our approach.
66
๐ค
Lisa Yan and Jerry Cain, CS109, 2020
You have $10 million to invest in 4 companies (in $1 million increments).1. How many ways can you fully allocate your $10 million?2. What if you want to invest at least $3 million in company 1?3. What if you donโt have to invest all your money?
67
Venture capitalists ๐ + ๐ โ 1๐ โ 1
Divider method(๐ indistinct objects, ๐ buckets)
๐ค
Lisa Yan and Jerry Cain, CS109, 2020
You have $10 million to invest in 4 companies (in $1 million increments).1. How many ways can you fully allocate your $10 million?
68
Venture capitalists. #1 ๐ + ๐ โ 1๐ โ 1
Divider method(๐ indistinct objects, ๐ buckets)
Set up
๐ฅ+: amount invested in company ๐๐ฅ+ โฅ 0
๐ฅ! + ๐ฅ" + ๐ฅ/ + ๐ฅ0 = 10Solve
Lisa Yan and Jerry Cain, CS109, 2020
You have $10 million to invest in 4 companies (in $1 million increments).1. How many ways can you fully allocate your $10 million?2. What if you want to invest at least $3 million in company 1?
69
Venture capitalists. #2 ๐ + ๐ โ 1๐ โ 1
Divider method(๐ indistinct objects, ๐ buckets)
Set up
๐ฅ+: amount invested in company ๐
๐ฅ! + ๐ฅ" + ๐ฅ/ + ๐ฅ0 = 10Solve
Lisa Yan and Jerry Cain, CS109, 2020
You have $10 million to invest in 4 companies (in $1 million increments).1. How many ways can you fully allocate your $10 million?2. What if you want to invest at least $3 million in company 1?3. What if you donโt have to invest all your money?
70
Venture capitalists. #3 ๐ + ๐ โ 1๐ โ 1
Divider method(๐ indistinct objects, ๐ buckets)
Set up
๐ฅ+: amount invested in company ๐๐ฅ+ โฅ 0
๐ฅ! + ๐ฅ" + ๐ฅ/ + ๐ฅ0 โค 10โ Solve
Lisa Yan and Jerry Cain, CS109, 2020
Summary of Combinatorics
71
Counting tasks on ๐ objects
Sort objects(permutations)
Choose ๐ objects(combinations)
Put objects in ๐buckets
Distinct(distinguishable)
Somedistinct Distinct Indistinct
Distinct
1 group ๐ groups
โข Determine if objects are distinctโข Use Product Rule if several steps โข Use Inclusion-Exclusion if different cases
Lisa Yan and Jerry Cain, CS109, 2020
See you next timeโฆ
72