Combinatorics

Combinatorics
CS2100
Ross Whitaker
University of Utah
Brain Teaser
•  Three colleagues arrive at a hotel late, and there is one
room left. They decide to share it.
•  The hotel clerk says the room is $30 for the night, and
each person puts $10 down on the counter.
•  The travelers retire to their room.
•  Meanwhile the hotel manager tells the clerk that room is
only $25/night, and he gives the clerk 5, $1 bills, and
tells him to refund the guests their $5.
•  The clerk doesn’t know how to split $5 three ways, so
when he arrives at the room, he decides to give each
guest one dollar, and keeps the remaining $2. •  Analysis:
 
 
 
 
Each guest has paid $10-$1=$9, and thus the guests have
paid $27 total. The clerk has $2 in his pocket $27 + $2 = $29 !!!!!
Where did the other dollar go?
Counting Outcomes of Events
•  How many ways can two winners be
chosen from four competitors (Andres,
Barbara, Chyou, Dinesh)?
•  How many elements are in the set:
 
{{A,B}, {A,C}, {A,D}, {B,C}, {B,D}, {C,D}}
•  What assumptions does this answer
impose on the problem?
 
 
Same person cannot win both prizes
Order does not matter
Prizes Cont.
•  What if there is a “first prize” and a
“second prize”?
•  What if these are two distinct “door
prizes” (different awards)?
•  What if these are two identical “door
prizes” (same awards)?
Are the prizes different? Can a person win both prizes? Yes No Yes 16 10 No 12 6 Generally
Does order maCer? Are repe@@ons allowed? Yes No Yes Ordered list Unordered list No Permuta@on Set Practice
•  Dealing a five-card poker (draw) hand
 
 
Every card is unique (no repetition)
Order does not matter
•  Dealing a two-card black-jack hand
 
 
Order matters (one is down one is up)
Repetition or not…
•  Creating game schedule for sports team
 
 
Order matters
Repetition or not…
•  Filling jar with various types of candy
 
 
Order does not matter
Repeats allowed
Organization in Counting
•  How many permutations are there for
the letters MATH?
 
MATH,
HMAT,
AHTM,
ATMH,
AMTH,
MHAT,
MAHT,
TAHM,
AMHT,
THMA,
TMAH,
ATHM,
THAM,
MHTA,
MTHA,
TAMH,
AHMT,
HMTA,
HTMA,
MTAH,
HAMT,
HATM,
TMHA,
HTAM
•  How do we make sure we have them
all?
Organizing and Counting
•  Organize by letters:
 
 
 
 
 
 
AMHT,
AMTH,
ATHM,
AHTM,
ATMH,
AHMT,
MATH,
MAHT,
MHTA,
MHAT,
MTAH,
MTHA,
HAMT,
HATM,
HMAT,
HMTA,
HTAM,
HTMA,
TAMH,
TAHM,
THAM,
THMA,
TMAH,
TMHA
•  Can organize within each column by
second, third, etc.
Two Prizes for A,B,C,D
•  Two different “door prizes” (order matters, repeats
allowed)
 
 
 
 
AA,
BA,
CA,
DA,
AB,
BB,
CB,
DB,
AC,
BC,
CC,
DC,
AD
BD
CD
DD
•  Two of the same “door prize” (order does not
matter, repeats allowed)
 
 
 
 
 
Idea: impose an order (alphabetical)
AA, AB, AC, AD
--, BB, BC, BD
--, --, CC, CD
--, --, --, DD
Combinatorial Equivalence
•  One-to-one correspondence between finite
sets
•  Recognizing that two sets are the same
size
•  Example:
 
 
 
 
 
How many multiples of 3 are there between 1
and 100 inclusive?
1*3=3, 2*3=6, …, 3*33=99, 3*34=100
This is invertible
{1,…,33}  {1, 3, 6, … 99}
How big is the set {1,…,33}?
Combinatorial Equivalence
•  Example:
 
 
How many ways are there to distribute
three coins (penny, nickel, dime) to 10
children?
How many numbers are in the set
{0, 1, 2, 3, …, 998, 999}?
•  Example:
 
 
The number of binary sequences of length
10
The power set of {1, 2, 3, …, 9, 10}.
Combinatorial Equivalence
•  Example:
 
 
Number of sets of size two from {1, … , 9}
Number of sets of size seven from {1, … ,
9}
•  Example:
 
 
The number of binary sequences of length
10
The power set of {1, 2, 3, …, 9, 10}.
Combinatorial Equivalence
•  Example
 
 
How many positive-integer solutions to (x+y
+z) = 21 ?
How many two-element subsets of {1,…,20}
are there ?
More on Counting
•  License plate
 
Either one or two letters from {A,L,B,M}
followed by four or three numbers
(respectively).
•  Cases:
 
One letter: arrange matrix/table with •  4 columns and 0000-9999 rows.
 
Two letters: •  columns are {AA, AB, AL, AM, BA,…, ML, MM}
•  rows are 000-999
 
40000+16000=56000
Rule of Products
•  If entries in list are created by i)
selecting one of x objects and then ii)
one of y objects, then the list has a
total of x⋅y entries. •  For finite sets A and B:
 
A
B =A⋅B
More on Products
•  How many numbers between 100 and 1000
have three distinct odd digits?
 
Basic rules: three digits, {1,3,5,7,9}, and no
repeats
1
 
3 713
3
1
5
3
7
5
9
9
How many leaves are in this tree?
•  5⋅4⋅3=60
5 715
9 719
Rule of Sums
•  If a count/set can be split two disjoint
pieces of size x and y, then the total
size of the original list is x+y
•  For disjoint sets A and B,  
A∪B=A+B
Rule of Sums
•  Example:
 
 
How many positive integers less than 1000
consist of distinct digits from the set
{1,3,7,9}?
Number of choices from 1-9
•  4
 
Number of choices from 10-99
•  4⋅3=12
 
Number of choices from 100-999
•  4⋅3⋅2=24
 
Total = 4+12+24 = 40
Rule of Sums
•  Example:
 
 
 
How many ways to win a dice game with three
distinguishable dice, where a winning role
contains a “pair”. Forms of winning roles: XXY, XYX, YXX, XXX (X
and Y are distinct)
Number X-Y choices are:
•  6⋅5=30
•  Three different configurations: 30+30+30=90
 
 
Number of solo X choices is 6
Total number of winning roles is 90+6=96
Rule of Sums
•  Example:
 
 
 
If we roll a die 3 times and record the
results as an ordered list, how many ways
are there to role exactly one “1”.
Divide into three sets with “1” in 3
different positions (first, second, third).
Count number of choices for the other two
rolls
•  3⋅(5⋅5)=75
Rule of Sums
•  If a list to count can be split into two
pieces of sizes x and y, and those
pieces have x objects/cases in common,
then the entire list has x+y-z entries
•  Sets:  
A∪B=A+B - A∩B
Algorithms for Counting
•  Odd numbers with distinct digits between
100 and 1000
for each U in {1,3,5,7,9} do
for each H from 1 to 9
if H≠U
for each T from 0 to 9
if T≠H and T≠U
print H,T,U
Typical Mistake
•  Number of ways to roll a sum of 10 on
3 distinct 6-side dice
 
 
 
Choose an element of {1,…6} on first roll
Choose an element of {1,…6} on second roll
The third roll must be 10 minus the sum of
first and second rolls
Example
•  Number of ways to roll a sum of 10 on 3 distinct
6-side dice
 
 
 
Choose an element of {1,…6} on first roll
Choose an element of {1,…6} on second roll so that the
sum is 9 or less
The third roll must be 10 minus the sum of first and
second rolls
•  First two rolls:
 
 
(number of ways to get >)+(number of ways to get ≤9)
= (total number of cases for two rolls)
•  Possibilities for first two rolls:
 
6⋅6 – (3+2+1) = 30
•  Total number of possibilities is 30
General Formulas
•  Total number of ordered lists from {1,
…,n} of length r is nr
•  Number of permutations of length r on
{1,…n} is denoted P(n,r)
•  P(n,n)=(n)(n-1)…(3)(2)(1)=n!
 
“n factorial”
•  P(n,r)=(n!)/(n-r)!
P(n,r)=(n!)/(n-r)!
•  Proof
 
 
What if we assume P(n,n)=n!
How can we prove P(n,n)=n!
Example
•  Geography test
 
 
 
List of 10 countries and 20 exports. Each country has a unique top export which
is in the list (->10 exports in the list are
bogus).
How many possible guesses are there at the
answer to this problem?
Counting with Equivalence Classes
•  How many two-element subsets of {1,2,3,4}
are there?
•  Permutations vs subsets
•  Two permutations are equivalent if they
are different representations of the same
subset
•  Some equivalence classes:
 
 
 
{{1,3}, {3,1}}
{{4,2}, {2,4}}
…
Example
•  How many ways to arrange 6 children in a
circle. •  Consider 6 “slots” they must walk into:
 
P(6,6) = 6! = 720
•  What if two arrangements are the same if
every child is neighbors with the same two
children? (rotate)
 
 
How many ways are there to get the same
“arrangement” in this new definition?
How many distinct arrangements are there by
this new definition?
Choosing Subsets
•  C(n,r) is the number of subsets of {1,…,n}
of size r
 
 
“n choose r”
r-combinations from {1,…n}
•  C(n,r)=P(n,r)/r!
 
 
Proof: Consider a single subset of length r.
There are P(r,r)=r! distinct permutations of this
set that are counted as part of P(n,r). Because every subset is counted r! times, the
total number of r-combinations is P(n,r)/r!
r-Combination Examples
•  How many ways are there to draw a
flush in 5-card poker (draw)?
Combinations
•  A club of 10 women and 8 men is
forming a committee of 5 people.
 
 
 
 
How many different committees are
possible?
How many committees contain exactly 3
women
How many committees contain at least 3
women
Suppose Jack and Jill refuse to work
together.
Binomials
•  (1+x)n = (1+x)⋅…⋅(1+x) [n times]
•  Expand algebraically
 
 
(1+x)⋅(1+x) = (1⋅1)+(1⋅x)+(x⋅1)+(x⋅x) =
1+2x+x2
(1+x)2⋅(1+x) = (1⋅1⋅1)+(1⋅x⋅1)+(x⋅1⋅1)+
(x⋅x⋅1)+(1⋅1⋅x)+(1⋅x⋅x)+(x⋅1⋅x)+(x⋅x⋅x) =
1+3x+3x2+x3
Binomial Theorem
•  The coefficient of the kth term of an
expansion of (1+x)n is C(n,k)
•  Examples:
 
 
(1+x)6
(1+x2)6
Pascal’s Triangle
Binomial Expansions
•  (1+x)n •  What if we substitute x=1?
•  What does this say about the rows of
Pascal’s triangle?
•  What does this say about the sum of kcombinations?
Binary Sequences
•  Basic model for counting other things
•  E.g. How many sequences are there with
five 1’s and three 0’s?
•  Idea: place the 1’s and then the 0’s
•  Place the 1’s: C(8,5)
•  Place the 0’s: C(3,3)
•  C(8,5)⋅C(3,3)=C(8,5)
Binary Sequences
•  Theorem: the number of binary
sequences with r 1’s and n-r 0’s is
C(n,r)=C(n,n-r)
Example
•  How many 10-letter sequences from the set
{m,a,t} contain exactly 3 instances of the
letter m?
•  Idea: first place the m’s into the ten slots
 
C(10,3)
•  How many ways are there to put {a,t} into
the remaining 7 slots?
 
27
•  Answer: C(10,3)⋅27
Example
•  From the previous answer, how many of
those 10-letter strings contain exactly 4
a’s?
•  Algorithm:  
Place the m’s •  C(10,3)
 
Place the a’s
•  C(7,4)
 
Place the t’s
•  C(3,3)
•  Total: C(10,3)⋅C(7,4)
Examples
•  How many (distinguishable) arrangements
of the letters in MISSISSIPPI?
•  Does it matter what order our
“algorithm” uses?
Unordered Lists (w/Repitition)
•  How many ways can we fill a bag with
10 pieces of 3 different kinds of fruit?
•  How many solutions are there for
nonnegative numbers in?
 
x+y+z=10
•  Show these are the same question.
Unordered Lists
1.  Bag containing r items chosen from n
types
2.  Binary sequence of length r+n-1
containing exactly r 0’s
•  How are they the same?
•  What is the solution to #2?
Bags (Unordered Lists)
Correspondence. To each sequence A,B,C,
associate the binary sequence consisting
of A 0’s, 1, B 0’s, 1, C 0’s.
Examples.
A=3,B=3,C=4 ↔ 000100010000
A=0,B=9,C=1 ↔ 100000000010
A=0,B=0,C=10 ↔ 110000000000
A=4,B=3,C=3 ↔ 000010001000
et cetera
Theorem
•  For natural numbers n and r
1.  The number of solutions to the equation
x1+…+xn=r using nonnegative integers is
C(r+n-1,r).
2.  The number of unordered lists of length r
taken from a set of size n (w/repetition)
is C(r+n-1,r)
3.  The number of ways r items can be
choosen from among n types is C(r+n-1,r)
Example
•  How many different outcomes are
possible from 4 tosses of a 6-sided die?
•  How many of those outcomes sum to 14?
Summary on Data Types
Recursive Counting
•  Reduce the counting problem to an easier
problem and some modification to that
problem.
•  E.g. Find a recursive model for the number
of games that will occur in the first round
of a football tournament with n teams.
 
 
Consider the tourney with n-1 teams. What changes in the schedule when the new
team signs up?
Recursive Counting
•  What is the recursive model for P(n,r),
the number of r-permutations of {1,
…,n}?
 
 
 
Choose an element of {1,…,n} for the first
position. Remove this element from the set to get
{1,…,n-1}.
Fill in the rest of the r-1 positions from
the n-1 set, which is P(n-1,r-1)
Permutations Continued
•  Thus, P(n,r)=n⋅P(n-1,r-1)
•  What is the base case?  
P(n,0)=1 for all n
Example
•  Let bn be the number of subsets of {1,…,n}
that do not contain consecutive numbers.
E.g. for 4 we have
 
{1,3}, {2,4}, {1,4}, {1}, {2}, {3}, {4}, {}
•  Find a recursive model for bn
 
 
Clearly all the elements in bn-1 can be in bn
Additional elements would include the new
element n, but we must exclude any subsets
that contain n-1
•  This would be all of the elements in bn-2
•  bn = bn-1 + bn-2 Towers of Hanoi
•  How to get discs from left peg to right
•  Rules:  
 
One disc moves at a time to any peg
No disc can reside above a smaller disc on
the same peg
Towers of Hanoi
•  Recursive solution
•  For an n tower, the n-1 solution to the
center peg would allow the largest disc
to move all the way right.
•  Then apply the n-1 solution to go from
the center to right
Towers of Hanoi
•  What is the recursive description of the
number of moves needed to solve the
the n sized TofH problem Hn
•  Move n-1 stack from left to middle is
Hn-1
•  Move n disc from left to right is 1
•  Move n-1 stack from middle to right is
Hn-1
•  Total is Hn = 2Hn-1 + 1
Towers of Hanoi
•  Use induction to show that the number
of moves for a n-sized Hanoi problem is
Hn = 2n-1 Examples
•  Find a recursive model for an the
number of n-digit numbers that do not
contain the digit 0
•  Find a recursive model for an, the
number of ways to cover 2 n grid of
squares with 1 2 pieces