You are on page 1of 52

Counting

Chapter 6

Counting
Combinatorial Analysis
The study of the numbers of ways of
selecting, or arranging, objects from a
finite set.
Many applications in network analysis,
discrete probability, analysis of
algorithms, graph theory, computational
geometry,

Basic Counting Principles


The product rule
If there are
3 ways to go from a to b, and
5 ways to go from b to c,
number of ways from a to c is

Examples
How many bit strings of length n?
How many subsets of a set of
cardinality n?
How many functions are there from a
set with m elements to a set with n
elements?

The sum rule


Suppose our choices are like this
Pick one of the sets A or B
Pick an element of the chosen set

Suppose A and B are disjoint


The number of choices is

|A| + |B|

Example
University president is either a student
or a professor
There are 1000 professors, 25000
students
How many possible the presidents?

The inclusion exclusion


principle (subtraction rule)

Suppose our choices are like this


Pick one of the sets A or B
Pick an element of the chosen set

Suppose A and B are not disjoint


The number of choices is

|A| + |B| - |A B|

Example
How many bit strings of length 8 either
start with 1 or end with 00?

Double Counting in Product


Rule (division rule)

How many ways to seat four people


around a circular when two seatings are
considered the same if each person has
the same left and right neighbors

Answer
Choose seat 1, order clockwise
4 people for seat 1
3 people for seat 2 etc
4x3x2x1 = 24 ways

Double counting? Each choice of seat 1


yields the same seating. There are 4
ways to choose seat 1. So each seating
is counted for times
The answer is 24/4

Permutations
How many ways to order n distinct
objects
n! by the product rule

r-permutations
How many ways to choose an ordered
set of r things out of n distinct objects
Again by the product rule
n (n-1)(n-2)(n-r+1)

Notation
P(n,r) : number of permutations of a set
with n elements
Can you find a formula for P(n,r)?
P(n, r) = n (n-1)(n-2)(n-r+1)
= n! / (n-r)!
Note: 0! = 1

What if we didnt care about


the order

Must remove the repetitions

Combination formula

P(n, r )
n!
C (n, r ) =
=
P(r , r ) r!(n r )!

Combinations
An r-combination of elements of a set S,
is an unordered selection of r elements
from S.

Notation
C(n,r): the number of r combinations of
an n element set
Also denoted by
n

And called the binomial coefficient

Combination formula

P(n, r )
n!
C (n, r ) =
=
P(r , r ) r!(n r )!

A useful identity
Let n and r be non-negative integers
with r n. Then C(n,r) = C(n,n-r)
Proof: ?

Binomial Coefficients
Application of combinations

Polynomial expansion
(x+y)3
= (x+y)(x+y)(x+y)
= x3+ x2y + x2y + x2y + xy2+ xy2+ xy2+ y3
= x3+ 3 x2y + 3 xy2+ y3
The coefficient of x2y is 3 because when choosing x
twice and y once, there are C(3,2) = C(3,1) = 3 ways
to choose where the x comes from
The coefficient of xy2 is 3 because when choosing x
once and y twice, there are C(3,2) = C(3,1) = 3 ways
to choose where the y comes from

Polynomial expansion
Consider: (x+y)5=x5+5x4y+10x3y2+10x2y3+5xy4+y5
To obtain the x5 term
Each time you multiple by (x+y), you select the x
Thus, of the 5 choices, you choose x 5 times: C(5,5) = 1
Alternatively, you choose y 0 times: C(5,0) = 1

To obtain the x4y term


Four of the times you multiply by (x+y), you select the x
The other time you select the y

Thus, of the 5 choices, you choose x 4 times: C(5,4) = 5


Alternatively, you choose y 1 time: C(5,1) = 5

To obtain the x3y2 term


C(5,3) = C(5,2) = 10

The binomial theorem

n n j j
( x + y ) = x y
j = 0 j
n

Polynomial expansion: Binomial


theorem
What is the coefficient of x12y13 in (x
+y)25?
C(25,12)

What is the coefficient of x12y13 in


(2x-3y)25?
Rephrase it as (2x+(-3y))25
C(25, 12) (2x)12 (-3y)25-12
So coefficient should be C(25, 12)212 (-3)13.

Combinations with Repetition


There are C(n+r-1, r) ways of choosing r
objects with repetition from n classes
(i.e r-combinations with repetition from a
set with n elemens)

Example
Cookie shop has 4 types of cookies
How many ways to buy 4 cookies?

Pigeonhole principle
If k+1 or more objects (pigeons) are
placed into k boxes, then there is at
least one box containing two or more of
the objects

Pigeonhole principle examples


In a group of 367 people, there must be
two people with the same birthday
As there are 366 possible birthdays

In a set of 27 English words, at least


two words must start with the same
letter
As there are only 26 letters

Generalized pigeonhole principle


If N objects are placed into k boxes,
then there is at least one box containing
N/k objects

Generalized pigeonhole principle


Among 100 people, there are at least X
people born in the same month. X = ?
X = 100/12 = 9

How many students must there be in a


class to ensure that 6 students get the
same grade (one of A, B, C, D, or F)?
The boxes are the grades. Thus, k = 5
Hence, N/5 = 6
Lowest possible value for N is 26

Tree diagrams
A systematic way of counting
exhaustively
Starting from the root, branch at every
choice: count the number of leaves
Generalizes the product rule

Example
How many different ways a best of five
series can end
Each game is win or lose, no ties.
Example 22 on page 395

ADDITIONAL EXAMPLES

Pigeonhole Examples

A bowl contains 10 red and 10 yellow balls


How many balls must be selected to ensure 3 balls of the
same color?
One solution: consider the worst case

Consider 2 balls of each color


You cant take another ball without hitting 3, Thus, the answer is 5

Via generalized pigeonhole principle

How many balls are required if there are 2 colors, and one color must
have 3 balls?
number of boxes: k = 2, We want N/k = 3
What is the minimum N?
N=5

Example
Show that among any n+1 positive integers
not exceeding 2n, there must be an integer
that divides one of the other integers.
See the solutions for quiz 6
There are two solutions
One by induction
One by pigeonhole

Alternative Solution By
Piegonhole

Among any n+1 positive integers not


exceeding 2n, there must be an integer that
divides one of the other integers.
Let a1, a2, , an+1 be such integers.
For any j, aj could be written as 2kjq where qj is
odd.
The integers q1, q2, , qn+1 are all odd positive
integers less than 2n.
Since there are only n odd numbers less than 2n,
qi = qj by the Pigeonhole principle.
Let q be qi = qj. Then ai = 2kiq and aj = 2kjq.
If i<j, ai | aj. Otherwise, aj | ai.
j

Counting Passwords
Passwords can be 6 to 8 characters
long (letters or digits)
Not case sensitive
Each password contains at least one
digit
How many passwords are there?

The traveling salesperson


problem (TSP)

A salesperson must visit n cities


For each pair of cities, we are given the
distance between them
The objective is to find the shortest tour to
visit them
Suppose we wanted to solve this problem by
enumerating all possible tours
How many are there?

Example
How many permutations of {a, b, c, d, e, f, g}
end with a?
Note that the set has 7 elements

The last character must be a


The rest can be in any order

Thus, we want a 6-permutation on the set {b,


c, d, e, f, g}
P(6,6) = 6! = 720

Combinations example
How many different poker hands are
there (5 cards)?
C(52, 5) = 2,598,960

How many different (initial) blackjack


hands are there?
C(52, 2) = 1,326

Examples

How many bit strings of length 10 contain:


exactly four 1s?

Find the positions of the four 1s


Does the order of these positions matter?
Positions 2, 3, 5, 7 is the same as positions 7, 5, 3,
2
Thus, the answer is C(10,4) = 210

at most four 1s?

There can be 0, 1, 2, 3, or 4 occurrences of 1


Thus, the answer is:
C(10,0) + C(10,1) + C(10,2) + C(10,3) + C(10,4)
= 1+10+45+120+210 = 386

Example
Suppose there are 9 teams in the west
conference and 11 teams in the east
conference
We would like to form an NBA
committee by picking 3 executives from
the West and 4 from the East.
Each team can send at most 1
executive
How many possible committees?

Answer
9! 11!
C (9,3) C (11,4) =

= 84 220 = 27,720
3!6! 4!7!

Question
How many ways are there for 4 horses
to finish if ties are allowed?

Solution by cases

Answer

No ties: The number of permutations is P(4,4) = 4! = 24


Two horses tie
There are C(4,2) = 6 ways to choose the two horses that tie
There are P(3,3) = 6 ways for the groups to finish
A group is either a single horse or the two tying horses

By the product rule, there are 6*6 = 36 possibilities for this case

Two groups of two horses tie


There are C(4,2) = 6 ways to choose the two winning horses
The other two horses tie for second place

Three horses tie with each other


There are C(4,3) = 4 ways to choose the two horses that tie
There are P(2,2) = 2 ways for the groups to finish
By the product rule, there are 4*2 = 8 possibilities for this case

All four horses tie


There is only one combination for this

By the sum rule, the total is 24+36+6+8+1 = 75

Combinations with repetition


A cash box contains $1, $2, $5, $10,
$20, $50, and $100 bills
How many ways to select 5 bills if
The order of the bills is not important
The bills are indistinguishable
There are > 5 bills of each type

Solution technique
Imagine we have a cash box for seven
types of bills with a compartment for
each type.
Represent this using 6 separators
100 | 50 | 20 | 10 | 5 | 2 | 1

For each bill place a * in the


corresponding compartment
That is: ** | | * | **| | |
is the selection of two $100, one $20,
two $10 bills

Solution (cont)
So the problem has been reduced to
the number of ways of arranging six |s
and five *s
Whats the answer?
C(6+5, 5)

In general
There are C(n+r-1, r) ways of choosing r
objects with repetition from n classes
(i.e r-combinations with repetition from a
set with n elemens)

Example
a+b+c = 11
a,b,c are non-negative integers
How many solutions are there?

Other Problems involving


indistinguishable objects

How many ways of arranging the letters


S, U, C, C, E, S, S
The answer is not 7!

Other Problems involving


indistinguishable objects

How many ways of distributing hands of


five cards to four players (from a 52
card deck)?

You might also like