DM Unit II CH 2final
DM Unit II CH 2final
Unit-II-Chapter-2
Counting:
Basics of counting
Combinatorics: The study of arrangements of objects or the
number of ways to put things together into various combinations
Enumeration: The counting of objects with certain properties
(an important part of combinatorics)
Enumerate the different telephone numbers possible in INDIA
The allowable password on a computer
The different orders in which runners in a race can reach
3
Basic Counting Principles
Two basic counting principles
Product rule
Sum rule
Product rule: Suppose that a procedure can be broken down
into a sequence of two tasks
If there are n1 ways to do the 1st task, and n2 ways to do the
2nd task after the first task has been done, then there are n1∙n2
ways to do the procedure
4
Example
The chairs of a room to be labeled with a letter and a
positive integer not exceeding 100. What is the largest
number of chairs that can be labeled differently?
There are 26 letters to assign for the 1st part and 100 possible
integers to assign for the 2nd part, so there are 26∙100=2600
different ways to label chairs
5
Product rule
Suppose that a procedure is carried out by performing the
tasks T1, T2, …, Tm in sequence. If each task Ti, i=1, 2, …, n
can be done in ni ways, regardless of how the previous tasks
were done, then there are n1∙n2∙ ..∙nm ways to carry out the
procedure
6
Example
How many different license plates are available if each plate
contains a sequence of 3 letters followed by 3 digits?
License plate _ _ _ _ _ _ : There are 26 choices for each
letter and 10 choices for each digit. So, there are
26∙26∙26∙10∙10∙10 = 17,576,000 possible license plates
7
Counting functions
How many functions are there from a set with m elements to
a set with n elements?
A function corresponds to one of the n elements in the
codomain for each of the m elements in the domain
Hence, by product rule there are n∙n…∙n=nm functions from
a set with m elements to one with n elements
8
Counting one-to-one functions
How many one-to-one functions are there from a set with m elements to one
with n elements?
First note that when m>n there are no one-to-one functions from a set with m
elements to one with n elements
Let m≤n. Suppose the elements in the domain are a1, a2, …, am. There are n ways
to choose the value of the function at a1
As the function is one-to-one, the value of the function at a2 can be picked in n-1
ways (as the value used for a1 cannot be used again)
Using the product rule, there are n(n-1)(n-2)…(n-m+1) one-to-one functions
from a set with m elements to one with n elements
Ex: From a set with 3 elements to one with 5 elements, there are 5∙4∙3=60 one-
to-one functions
9
Example
Suppose the format of telephone numbers in India is
specified by a numbering plan
It consists of 10 digits, with 3-digit area code, 3-digit office
code and 4-digit station code
Each digit can take one form of
X: 0, 1, …, 9
N: 2, 3, …, 9
Y: 0, 1
10
Example
In the old plan, the formats for area code, office code, and
station code are NYX, NNX, and XXXX, respectively
So the phone numbers had NYX-NNX-XXXX
NYX: 8∙2∙10=160 area codes
NNX: 8∙8∙10=640 office codes X: 0, 1, …, 9
N: 2, 3, …, 9
XXXX:10∙10∙10∙10=10,000 station codes Y: 0, 1
14
Sum Rule
If a task can be done either in one of n1 ways or in one of n2
ways, where none of the set of n1 ways is the same as any of
the set of n2 ways, then there are n1+n2 ways to do the task
Example: Suppose either a member of faculty or a student in
CSE is chosen as a representative to a university committee.
How many different choices are there for this representative if
there are 8 members in faculty and 200 students?
There are 8+200=208 ways to pick this representative
15
Sum Rule
If A1, A2, …, Am are disjoint finite sets, then the number of
elements in the union of these sets is as follows
|A1⋃A2 ⋃… ⋃Am|=|A1|+|A2|+…+|Am|
16
More Complex Counting Problems
In a version of the BASIC programming language, the name of a variable
is a string of 1 or 2 alphanumeric characters, where uppercase and
lowercase letters are not distinguished.
Moreover, a variable name must begin with a letter and must be different
from the five strings of two characters that are reserved for programming
use
How many different variables names are there?
Let V1 be the number of these variables of 1 character, and likewise V 2 for
variables of 2 characters
So, V1=26, and V2=26∙36-5=931
In total, there are 26+931=957 different variables
17
Example
Each user on a computer system has a password, which is 6 to 8
characters long, where each character is an uppercase letter or a digit.
Each password must contain at least one digit. How many possible
passwords are there?
Let P be the number of all possible passwords and P=P 6+P7+P8 where
Pi is a password of i characters
P6=366-266=1,867,866,560
P7=367-267=70,332,353,920
P8=368-268=208,827,064,576
P=P6+P7+P8=2,684,483,063,360
18
Example: Internet address
20
Inclusion-Exclusion Principle
Suppose that a task can be done in n1 or in n2 ways, but some
of the set of n1 ways to do the task are the same as some of
the n2 ways to do the task
Cannot simply add n1 and n2, but need to subtract the number
of ways to the task that is common in both sets
This technique is called principle of inclusion-exclusion or
subtraction principle
21
Example
How many bit strings of length 8 either start with a 1 or end
with two bits 00?
1 _ _ _ _ _ _ _: 27=128 ways
_ _ _ _ _ _ 00: 26=64 ways
1 _ _ _ _ _ 00: 25=32 ways
Total number of possible bit strings is 128+64-32=160
22
Inclusion-Exclusion Principle
Using sets to explain
|A1⋃A2|=|A1|+|A2|-|A1⋂A2|
23
Tree Diagrams
How many bit strings of length 4 do not have two consecutive 1s?
In some cases, we can use tree diagrams for counting
24
Example
A playoff between 2 teams consists of at most 5 games. The 1st
team that wins 3 games wins the playoff. How many different ways
are there?
25
Example
Suppose a T-shirt comes in 5 different sizes: S, M, L, XL, and
XXL. Further suppose that each size comes in 4 colors, white,
green, red, and black except for XL which comes only in red, green
and black, and XXL which comes only in green and black. How
many possible size and color of the T-shirt?
26
Pigeonhole Principle
Suppose that a flock of 20 pigeons flies into a set of 19
pigeonholes to roost
Thus, at least 1 of these 19 pigeonholes must have at
least 2 pigeons
Why? If each pigeonhole had at most one pigeon in it, at
most 19 pigeons, 1 per hole, could be accommodated
If there are more pigeons than pigeonholes, then there
must be at least 1 pigeonhole with at least 2 pigeons in it
27
Example
28
Pigeonhole Principle
Theorem 1: If k is a positive integer and k+1 or more
objects are placed into k boxes, then there is at least one
box containing two or more of the objects
Proof: suppose that none of the k boxes contains more
than one object. Then the total number of objects would be
at most k. This is a contradiction as there are at least k+1
objects
Also known as Dirichlet drawer principle
29
Pigeonhole Principle
Corollary 1: A function f from a set with k+1 or more elements
to a set with k elements is not one-to-one
Proof: Suppose that for each element y in the codomain of f we
have a box that contains all elements x of the domain f such
that f(x)=y
As the domain contains k+1 or more elements and the
codomain contain only k elements, the pigeonhole principle
tells us that one of these boxes contains 2 or more elements x
of the domain
This means that f cannot be one-to-one
30
Example
Among any group of 367 people, there must be at least 2 with
the same birthday
How many students must be in a class to guarantee that at least
2 students receive the same score on the final exam, if the
exam is graded on a scale from 0 to 100 points
31
Generalized Pigeonhole Principle
Theorem 2: If N objects are placed into k boxes, then
there is at least one box containing at least⎾N/k⏋objects
Proof: Proof by contradiction. Suppose that none of the
boxes contains more than ⎾N/k⏋-1 objects. Then the
total number of objects is at most k(⎾N/k⏋-
1)<k((N/k+1)-1)=N
where the inequality ⎾N/k⏋<N/k+1 is used
This is a contradiction as there are a total of N objects
32
Example
Q)Among 100 people there are at least ⎾100/12⏋= 9 who
were born in the same month
Q)What is the minimum number of students required in a
discrete mathematics class to be sure that at least 6 will
receive the same grade, if there are 5 possible grades, A, B, C,
D, and F ?
Sol: The minimum number of students A, B, C, D, and F
needed to ensure at least 6 students receive the same grade
is the smallest integer N s.t. ⎾N/5⏋=6. Thus, the smallest
N=5∙5+1=26
33
Example
Q) There are 280 students in a class. Without knowing
anybody’s birthday, what is the largest value of n for which
we can prove using the G.P.P. that at least n students must
have been born in the same month?
Sol:⎡280/12⎤ = ⎡23.3⎤ = 24
34
Example
Show that for every integer n there is a multiple m > 0 of n that has only 0’s and 1’s in its
decimal expansion.
Sol: Consider the n+1 decimal integers 1,11, 111, …, 1…1. They have only n possible
remainders when an integer is divided by n (mod n).
So, take the difference of two that have the same remainder. The result is the answer!
Let n=3. Consider 1, 11, 111, 1111.
1 mod 3 = 1
11 mod 3 = 2
111 mod 3 = 0
1111 mod 3 = 1
1111 − 1 = 1,110 = 3·370.
It has only 0’s and 1’s in its expansion.
Its remainder mod 3 = 0, so it’s a multiple of 3.
35
Example
Q)How many cards must be selected from a standard
deck of 52 cards to guarantee that at least 3 cards of the
same suit are chosen?
Sol: Suppose there are 4 boxes, one for each suit. If N
cards are selected, using the generalized pigeonhole
principle, there is at least one box containing at least
⎾N/4⏋cards
Thus to have ⎾N/4⏋≥ 3 , the smallest N is 2∙4+1=9.
So at least 9 cards need to be selected
36
Example
Q) How many cards must be selected to guarantee that at least
3 hearts are selected?
Sol: We do not use the generalized pigeonhole principle to
answer this as we want to make sure that there are 3 hearts,
not just 3 cards of one suit
Note in the worst case, we can select all the clubs, diamonds,
and spades, 39 cards in all before selecting a single heart
The next 3 cards will be all hearts, so we may need to select
42 cars to guarantee 3 hearts are selected
37
Applications of Pigeonhole Principle
Q) During a month with 30 days, a baseball team plays at least one game a
day, but no more than 45 games. Show that there must be a period of some
number of consecutive days during which the team must play exactly 14
games
Sol: Let aj be the number of games played on or before jth day of the month.
Then a1, a2, …, a30 is an increasing sequence of distinctive positive integers
with 1≤aj ≤45. Moreover a1+14, a2+14, …, a30+14 is also an increasing
sequence of distinct positive integers with 15 ≤aj+14 ≤59
The 60 positive integers, a1, a2, …, a30, a1+14, a2+14, …, a30+14 are all less
than or equal to 59. Hence, by the pigeonhole principle, two of these integers
must be equal, i.e., there must be some i and j with ai=aj+14. This means
38
Applications of Pigeonhole Principle
Example of {aj}: Note all elements are distinct.
Thus, for
1, 2, 4, 5, 7, 8, 10, 11, 13, 14, example, exactly
16, 17, 19, 21, 22, 23, 25, 27, 29, 30, 14 games were
played during
31, 33, 34, 36, 37, 39, 40, 41, 43, 45 days 3 to 11:
Then {aj +14} is the following sequence: 2+1+2+1+2+1+2+1+2
15, 16, 18, 19, 21, 22, 24, 25, 27, 28,
30, 32, 33, 35, 36, 37, 39, 41, 43, 44,
45, 47, 48, 50, 51, 53, 54, 55, 57, 59
In any 60 integers from 1-59 there must be some duplicates, indeed we
find the following ones: 16, 19, 21, 22, 25, 27, 30, 33, 36, 37, 39, 41,
43, 45 39
Ramsey Theory
Example: Assume that in a group of 6 people, each pair of individuals
consists of two friends or 2 enemies. Show that there are either 3 mutual
friends or 3 mutual enemies in the group
Let A be one of the 6 people. Of the 5 other people in the group, there are
either 3 or more who are friends of A, or 3 or more are enemies of A
This follows from the generalized pigeonholes principles, as 5 objects are
divided into two sets, one of the sets has at least ⎾5/2⏋=3 elements
Suppose that B,C and D are friends of A. If any two of these three
individuals are friends, then these two and A form a group of three mutual
friends. Otherwise, B, C and D form a set of three mutual enemies.
40
Ramsey Number
Ramsey number R(m, n) where m and n are positive
integers greater than or equal to 2, denotes the minimum
number of people at a party so that there are either m
mutual friends or n mutual enemies, assuming that every
pair of people at the party are friends or enemies
In the previous example, R(3,3)≤6
We conclude that R(3,3)=6 as in a group of 5 people
where every two people are friends or enemies, there may
not be 3 mutual friends or 3 mutual enemies
41
Permutations & Combinations
Counting:
Find out the number of ways to select a particular number of
elements from a set
Sometimes the order of these elements matter
Example:
In how many ways we can select 3 students from a group of 5
students?
In how many different ways they stand in line for picture?
42
Permutation
In how many ways can we select 3 students from a group of 5 to stand in
line for a picture?
First note that the order in which we select students matters
There are 5 ways to select the 1st student
Once the 1st student is selected, there are 4 ways to select the 2 nd student
in line and then 3 ways to select 3rd student in line. By product rule, there
are 5x4x3=60 ways to select 3 students from a group of 5 students to
stand in line for picture
In how many ways can we arrange all 5 in a line for a picture?
By product rule, we have 5x4x3x2x1=120 ways to arrange all 5 students
in a line for a picture 43
Permutation
A permutation of a set of distinct objects is an ordered
arrangement of these objects
E.g. {A, B, C} → six permutations:ABC, ACB, BAC, BCA, CAB, CBA
An ordered arrangement of r elements of a set is called an r-
permutation
The number of r-permutation of a set with n elements is denoted
by P(n,r). We can find P(n,r) using the product rule
Example: Let S={1, 2, 3}. The ordered arrangement 3, 1, 2 is a
permutation of S. The ordered arrangement 3, 2, is a 2-
permutation of S
44
r-permutation
Theorem 1: The number of r-permutations of a set with n distinct
elements is P(n,r)=n(n-1)(n-2)…(n-r+1)
Proof: Using the product rule, the first element can be chosen in n
ways. There are n-1 ways to chose the 2nd element. Likewise, there are
n -2 ways to choose 3rd element, and so on until there are exactly n-(r-
1)= n-r+1 ways to choose the r-th element.
Thus, there are n∙(n -1)∙(n -2)… ∙(n - r +1) r-permutations of the set
Corollary: If n and r are integers with 0≤r≤n, then
P(n,r)=n!/(n -r)!
By Theorem 1, we know that if n is a positive integer, then P(n,n)=n!
45
r-permutation
Example: How many ways are there to select a 1st prize
winner, a 2nd prize winner, and a 3rd prize winner from 100
different contestants?
As it matters which person wins which prize, the number of
ways to select three prize winners is the number of ordered
selections of three elements from a set of 100 elements, that is,
the number of 3-permutations of a set of 100 elements
P(100,3)=100!/(100-3)!=100x99x98=970,200
46
Example
How many permutations of the letters ABCDEFGH contain
string ABC?
As ABC must occur as a block, we can find the answer by
finding the permutations of 6 letters, the block ABC and the
individual letters, D,E,F,G, and H. As these 6 objects must
occur in any order, there are 6!=720 permutations of the
letters ABCDEFGH in which ABC occurs as a block
47
Exercise
Suppose that there are eight runners in a race. The winner
receives a gold medal, the second-place finisher receives a
silver medal, and the third-place finisher receives a bronze
medal. How many different ways are there to award these
medals, if all possible outcomes of the race can occur and there
are no ties?
Suppose that a salesman has to visit eight different cities. He
must begin his trip in a specified city, but he can visit the other
seven cities in any order he wishes. How many possible orders
can the salesman use when visiting these cities?
48
Combinations
How many different committees of 3 students can be formed from a
group of 4 students?
We need to find the number of subsets with 3 elements from the set
containing 4 students
We see that there are 4 such subsets, one for each of the 4 students
as choosing 4 students is the same as choosing one of the 4 students
to leave out of the group
This means there are 4 ways to choose 3 students for the committee,
where the order in which these students are chosen does not matter
49
r-combination
An r-combination of elements of a set is an unordered
selection of r elements from the set
An r-combination is simply a subset of the set with r elements
The number of r-combinations of a set with n distinct elements
n
is denoted by C(n,r). Note that C(n,r) is also denoted by r and
is called a binomial coefficient
Let S be the set {1, 2, 3, 4}. Then {1, 3, 4} is a 3-combination
from S
We see that C(4,2)=6, as the 2-combination of {a, b, c, d} are 6
subsets {a, b}, {a, c}, {a, d}, {b, c}, {b, d}, and {c, d} 50
r-combination
Theorem 2: The number of r-combinations of a set with n elements,
where n is a nonnegative integer and r is an integer with 0≤r≤n
equals
n!
C (n, r )
r ! ( n r )!
52
Corollary 1
Let n and r be nonnegative integers with r≤n. Then
C(n,r)=C(n,n-r)
Proof: C ( n , r ) n!
r ! ( n r )!
n! n!
C (n, n r )
( n r )! ( n ( n r ))! ( n r )! r !
53
Example
Q) How many ways are there to select 5 players from a 10-
member tennis team?
Sol: Choose 5 out of 10 elements, i.e., C(10, 5)=10!/(5!5!)=252
Q) How many bit strings of length n contain exactly r 1s?
Sol: The positions of r 1s in a bit string of length n form an r-
combination of the set {1,2,3,…n}. Hence, there are C(n,r) bit
strings of length n that contain r 1s.
54
Exercise
Q) A group of 30 people have been trained as astronauts to go on
the first mission to Mars. How many ways are there to select a crew
of six people to go on this mission (assuming that all crew members
has the same job)?
How many ways are there to select a committee to develop a
discrete mathematics course at a school if the committee is to
consist of three faculty members from the mathematics department
and four from the computer science department, if there are nine
faculty members of the mathematics department and 11 of the
computer science department?
55
Binomial coefficients
The number of r-combinations from a set with n elements is
often denoted by nr
Also called as a binomial coefficient as these numbers occur
as coefficients in the expansion of powers of binomial
expressions such as (a+b)n
A binomial expression is simply the sum of two terms, such as
x+y
56
Example
The expansion of (x+y)3 can be found using combinational reasoning
instead of multiplying the there terms out
When (x+y)3=(x+y)(x+y)(x+y) is expanded, all products of a term in
the 1st sum, a term in the 2nd sum, and a term in the 3rd sum are added,
e.g., x3, x2y, xy2, and y3
To obtain a term of the form x3, an x must be chosen in each of the
sums, and this can be done in only one way. Thus, the x3 term in the
product has a coefficient of 1.
To obtain a term of the form x2y, an x must be chosen in 2 of the 3
sums (and consequently a y in the other sum). Hence, the number of 3
2
such terms is the number of 2-combinations of 3 objects, namely ,
57
Example contd…
Similarly, the number of terms of the form xy2 is the number of
ways to pick 1 of the 3 sums to obtain an x (and consequently take
a y from each of the other two sums), which can be done in 1 ways
3
Finally the only way to obtain a y3 term is to choose the y for each
of the three sums in the product, and this can be done in exactly one
way. Consequently,
( x y)3 ( x y)(x y)(x y) ( xx xy yx yy)(x y)
xxx xxy xyx xyy yxx yxy yyx yyy
x3 3x2 y 3xy2 y3
58
The Binomial Theorem
The binomial theorem: Let x and y be variables, and let n
be a nonnegative integer. Then
n
n n j j
( x y) x y
n
j 0 j
59
Example
What is the expansion of (x+y)4?
4
4 4 j j
(x y) 4
x
j0 j
y
60
Example
Q)What is the coefficient of x12y13 in the expansion of (2x-3y)25?
Sol: Note that this expression equals (2x+(-3y))25. By the
Binomial Theorem, we have
25
25
( 2 x ( 3 y )) ( 2 x ) 25 j ( 3 y ) j
25
j 0 j
k k 0
k
k 0
k 0
62
Pascal Identity and Triangle
Pascal’s identity: Let n and k be positive integers with n ≥ k. Then
n 1 n n
k k 1 k
We can prove the Pascal identity with algebraic manipulation of kn
n 1 ( n 1)!
k k!( n k 1)!
n n! kn! kn!
k 1 ( k 1)!( n k 1)! ( k 1)!k ( n k 1)! k!( n k 1)!
n n! ( n k 1) n! ( n k 1) n!
k k!( n k )! k!( n k )!( n k 1) k!( n k 1)!
n n kn! ( n k 1) n! ( k n k 1) n! ( n 1) n! ( n 1)!
k 1 k k!( n k 1)! k!( n k 1)! k!( n k 1)! k!( n k 1)! k!( n k 1)!
63
Pascal’s Triangle
Pascal’s Identity, together with the initial conditions n0 nn 1
for all integers n, can be used to recursively define
binomial coefficients. This recursive definition is useful in
the computation of binomial coefficients since only the
addition of integers is needed to use this recursive
definition
64
Pascal’s Triangle
Pascal’s Identity is the basis for a geometric arrangement of the binomial coefficients in a
n
triangle. The nth row in the triangle consists of the binomial coefficients k , k 0,1,..., n.
This triangle is known as Pascal’s triangle. Pascal’s Identity shows that when two adjacent
binomial coefficients in this triangle are added, the binomial coefficient in the next row
between these two coefficients is produced.
each n - th row, binomial coefficien ts
n 1 n n
k k 1 k
65
Vandermonde’s Identity
Let m, n, and r be nonnegative integers with r not exceeding either
m n r m n
m or n. Then
r k 0 r k k
Proof: Suppose that there are m items in one set and n items in a
second set. Then the total number of ways to pick r elements from
the union of these sets is m r n
Another way to pick r elements from the union is to pick k elements
(0 < k < r ) from the second set and then r – k elements from the
m n
first set. By the product rule, this can be done in
r k k
ways.
Hence, the total number of ways to pick r elements from the union
also equals m r n r m k kn
r
k 0
66
Corollary and Theorem
2
Corollary If n is a nonnegative integer, then 2n n n
n k 0 k
67
Exercises
Find the expansion of (x+y)6?
What is the coefficient of x9 in (2-x)19?
What is the row of Pascal’s triangle containing the binomial
coefficients 9
,0 k 9 ?
k
68
Generalized Permutations and
Combinations
Permutations and combinations allowing repetitions.
How many strings of length r can be formed from the English
alphabet?
How many different ways are possible when we select a dozen
donuts from a box that contains four different kinds of donuts?
Permutations where not all objects are distinguishable.
The number of ways we can rearrange the letters of the word
MISSISSIPPI
69
Permutations with Repetitions
Theorem 1: The number of r-permutations of a set of n objects
with repetition allowed is nr.
Proof: There are n ways to select an element of the set for each of
the r positions in the r-permutation when repetition is allowed,
since for each choice all n objects are available. Hence, by the
product rule, the answer is given as nr .
Example: How many strings of length r can be formed from the
English alphabet?
Answer: 26r
70
Combinations with Repetitions
An example
How many ways are there to select four pieces of fruit from a bowl
containing apples, oranges, and pears if there are at least four pieces of each
type of fruit in the bowl?
In this case, the order in which the pieces are selected does not matter, only
the types of fruit, not the individual piece, matter.
71
Combinations with Repetitions
Example Rephrased: The number of 4-combinations with repetition
allowed from a 3-element set {apple, orange, pear}
All four in same type: i) 4 apples, ii) 4 oranges, iii) 4 pears [3 ways]
Three in same type: two cases for each of 3 apples, 3 oranges, 3 pears i.e., i)3 apples,
1 orange ii) 3 apples, 1 pear iii) 3 oranges, 1 apple iv) 3 oranges 1 pear v) 3 pears 1
apple vi) 3 pears 1 orange [2*3= 6 ways]
Two different pairs with each pair in same type i)2 apples, 2 oranges ii) 2 apples, 2
pears iii) 2 oranges, 2 pears [3 ways]
Only one pair in same type i)2 apples, 1 orange ,1 pear ii) 2 oranges, 1 apple, 1 pear,
iii) 2 pears, 1 apple, 1 orange [3 ways]
Total 15 ways
Can be generalized:
The number of ways to fill 4 slots from 3 categories with repetition allowed
72
Example
How many ways are there to select five bills from a cash box
containing $1 bills, $2 bills, $5 bills, $10 bills, $20 bills, $50 bills,
and $100 bills?
The order in which the bills are chosen doesn’t matter
The bills of each denomination are indistinguishable
There are at least five bills of each type
Sol: C(7–1+5, 5)
= C(11, 5)
= 11! / (5!·6!)
= 462
73
Combinations with Repetitions
Theorem 2: The number of r-combinations from a set with n elements
with repetition allowed is: C(n + r −1,r) = C(n + r −1,n −1)
Other representations with the same meaning
# of ways to fill r slots from n categories with repetition allowed
# of ways to select r elements from n categories of elements with
repetition allowed
74
Example
Suppose that a cookie shop has four different kinds of cookies. How
many different ways can six cookies be chosen? Assume that only the
type of cookie, and not the individual cookies or the order in which
they are chosen, matters.
Sol: The number of ways to choose six cookies is the number of 6-
combinations of a set with four elements. From Theorem 2 this equals
C(4 + 6 − 1, 6) = C(9, 6). Because
C(9, 6) = C(9, 3) = 9!/3!6!=9 · 8 · 7/1 · 2 · 3= 84,
So, there are 84 different ways to choose the six cookies.
75
More Examples
How many solutions does the equation x1 + x2 + x3 = 11 have, where
x1,x2 and x3 are nonnegative integers?
Sol: To count the number of solutions, we note that a solution
corresponds to a way of selecting 11 items from a set with three
elements so that x1 items of type one, x2 items of type two, and x3 items
of type three are chosen. Hence, the number of solutions is equal to the
number of 11-combinations with repetition allowed from a set with
three elements. From Theorem 2 it follows that there are
C(3 + 11 − 1, 11) = C(13, 11) = C(13, 2) =13!/2!11!= 13·12/1 · 2= 78
solutions.
76
More Examples
The number of solutions of this equation can also be found when the variables
are subject to constraints. For instance, we can find the number of solutions
where the variables are integers with x1 ≥ 1, x2 ≥ 2, and x3 ≥ 3. A solution to the
equation subject to these constraints corresponds to a selection of 11 items with
x1 items of type one, x2 items of type two, and x3 items of type three, where, in
addition, there is at least one item of type one, two items of type two, and three
items of type three.
So, a solution corresponds to a choice of one item of type one, two of type two,
and three of type three, together with a choice of five additional items of any
type. By Theorem 2 this can be done in
C(3 + 5 − 1, 5) = C(7, 5) = C(7, 2) =7!/2!5!= 7 · 6/1 · 2= 21 ways. Thus, there
are 21 solutions of the equation subject to the given constraints.
77
Formulae for Combinations and Permutations with and
without Repetition
Permutations with Indistinguishable Objects
Q) How many different strings can be made by reordering the letters of the word SUCCESS?
Sol: Because some of the letters of SUCCESS are the same, the answer is not given by the
number of permutations of seven letters. This word contains three Ss, two Cs, one U, and one
E. To determine the number of different strings that can be made by reordering the letters, first
note that the three Ss can be placed among the seven positions in C(7, 3) different ways,
leaving four positions free. Then the two Cs can be placed in C(4, 2) ways, leaving two free
positions. The U can be placed in C(2, 1) ways, leaving just one position free. Hence E can be
placed in C(1, 1) way. Consequently, from the product rule, the number of different strings
that can be made is C(7, 3)C(4, 2)C(2, 1)C(1, 1)
= (7!/3! 4!)· (4!/2! 2!)· (2!/1! 1!)· (1!/1! 0!) = 7!/3!2!1!1!= 420.
Theorem: The number of different permutations of n objects, where there are n 1
indistinguishable objects of type 1, n2 indistinguishable objects of type 2, . . . , and n k
indistinguishable objects of type k, is
n!/n1! n2! · · · nk! .
79
Distributing Objects into Distinguishable Boxes
Distinguishable (or labeled) objects to distinguishable boxes
Q) How many ways are there to distribute hands of 5 cards to each of four players from the standard deck
of 52 cards?
Sol: Product rule can be used to solve this problem. To begin, note that the first player can be dealt 5 cards
in C(52, 5) ways. The second player can be dealt 5 cards in C(47, 5) ways, because only 47 cards are left.
The third player can be dealt 5 cards in C(42, 5) ways. Finally, the fourth player can be dealt 5 cards in
C(37, 5) ways. Hence, the total number of ways to deal four players 5 cards each is
C(52,5)C(47,5)C(42,5)C(37,5) = (52!/47!5!)· (47!/42!5!)· (42!/37!5!)· (37!/32! 5!)
= 52!/5! 5! 5! 5! 32! .
Theorem: The number of ways to distribute n distinguishable objects into k distinguishable boxes so that ni
objects are placed into box i, i = 1, 2, . . . , k, equals=n!/(n1! n2! · · · nk!)
Indistinguishable (or unlabeled) objects to distinguishable boxes
Q) How many ways are there to place 10 indistinguishable balls into 8 distinguishable bins?
Sol: The number of ways to place 10 indistinguishable balls into eight bins equals the number of 10-
combinations from a set with eight elements when repetition is allowed. Consequently, there are
C(8 + 10 − 1, 10) = C(17, 10) = 17!/10!7!= 19,448
80
Distributing Distinguishable Objects into Indistinguishable
Boxes
Q)How many ways are there to put 4 different employees into 3 indistinguishable offices, when each office
can contain any number of employees?
Sol: We represent the four employees by A, B, C, and D. First, we note that we can distribute employees so that all
four are put into one office, three are put into one office and a fourth is put into a second office, two employees are
put into one office and two put into a second office, and finally, two are put into one office, and one each put into the
other two offices.
Each way to distribute these employees to these offices can be represented by a way to partition the elements A, B, C,
and D into disjoint subsets.
We can put all four employees into one office in exactly one way, represented by {{A,B,C,D}}.
We can put three employees into one office and the fourth employee into a different office in exactly four ways,
represented by {{A,B,C}, {D}}, {{A,B,D}, {C}}, {{A,C,D}, {B}}, and {{B,C,D}, {A}}.
We can put two employees into one office and two into a second office in exactly three ways, represented by {{A,B},
{C,D}}, {{A,C}, {B,D}}, and {{A,D}, {B,C}}.
Finally, we can put two employees into one office, and one each into each of the remaining two offices in six ways,
represented by {{A,B}, {C}, {D}}, {{A,C}, {B}, {D}}, {{A,D}, {B}, {C}}, {{B,C}, {A}, {D}}, {{B,D}}, {A},
{C}}, and {{C,D}, {A}, {B}}.
Counting all the possibilities, we find that there are 14 ways to put four different employees into three
indistinguishable offices. 81
Distributing Indistinguishable Objects into Indistinguishable
Boxes
Q) How many ways are there to pack 6 copies of same book into 4 identical boxes, where each
box can contain as many as six books?
Sol: For each way to pack the books, we will list the number of books in the box with the largest number of
books, followed by the numbers of books in each box containing at least one book, in order of decreasing
number of books in a box. The ways we can pack the books are
6
5,1
4,2
4,1,1
3,3
3,2,1
3,1,1,1
2,2,2
2,2,1,1
So, there are nine allowable ways to pack the books.
82
Generating Permutations and
Combinations
Sometimes permutations or combinations need to be generated, not just
counted.
E.g. suppose that a salesperson must visit six different cities. In which
order should these cities be visited to minimize total travel time? One
way to determine the best order is to determine the travel time for each of the 6! =
720 different orders in which the cities can be visited and choose the one with the
smallest travel time.
E.g. Given a set of six positive integers, find a subset that has 100 as their
sum, if such a subset exists. One way to find these numbers is to generate all
26 = 64 subsets and check the sum of their elements.
83
Generating Permutations
Any set with n elements can be placed in one-to-one correspondence with the set {1,2,3,…,n}.
We can list the permutations of any set of n elements by generating the permutations of the n
smallest positive integers and then replacing these integers with the corresponding elements.
E.g. {a,b,c} ->{1,2,3} Permutation:123->abc, 132 ->acb, 213 -> bac
Lexicographic (or dictionary) ordering of the set of permutations of {1, 2, 3, . , n}.
Permutation a1a2…an precedes the permutation b1b2…bn, if for some k, with 1 ≤ k ≤ n, a1 = b1,
a2 = b2, …, ak-1 = bk-1, and ak < bk. i.e., a permutation of the set of the n smallest positive
integers precedes (in lexicographic order) a second permutation if the number in this
permutation in the first position where the two permutations disagree is smaller than the
number in that position in the second permutation.
E.g. 13245 precedes 13254 ->acbde precedes acbed
E.g. The permutation 23415 of the set {1, 2, 3, 4, 5} precedes the permutation 23514, because
these permutations agree in the first two positions, but the number in the third position in the
first permutation, 4, is smaller than the number in the third position in the second permutation,
5.
84
ALGORITHM: Generating the Next Permutation in Lexicographic Order.
procedure next permutation(a1a2 . . . an: permutation of{1, 2, . . . , n} not equal to n n− 1 . . . 2 1)
j := n − 1
while aj > aj+1
j := j − 1
{j is the largest subscript with aj < aj+1}
k := n
while aj > ak
k := k − 1
{ak is the smallest integer greater than aj to the right of aj }
interchange aj and ak
r := n
s := j + 1
while r > s
interchange ar and as
r := r − 1
s := s + 1
{this puts the tail end of the permutation after the jth position in increasing order}
85
Generating Permutations
Algorithm:
1. From the last digit forward, find the first aj so that aj < aj+1
2. To the right of aj, find the smallest number ak that is greater than aj
3. Swap aj and ak
4. Place all the numbers after jth position in order.
86
Generating Permutations
Q)What is the next permutation in lexicographic order after 362541?
Sol: The last pair of integers aj and aj+1 where aj < aj+1 is a3 = 2 and a4 = 5. The least integer to the
right of 2 that is greater than 2 in the permutation is a5 = 4. Hence, 4 is placed in the third position.
Then the integers 2, 5, and 1 are placed in order in the last three positions, giving 125 as the last
three positions of the permutation. Hence, the next permutation is 364125.
Q)Generate the permutations of the integers 1, 2, 3 in lexicographic order.
Sol: Begin with 123. The next permutation is obtained by interchanging 3 and 2 to obtain 132. Next,
because 3 > 2 and 1 < 3, permute the three integers in 132.
Put the smaller of 3 and 2 in the first position, and then put 1 and 3 in increasing order in positions 2
and 3 to obtain 213.
This is followed by 231, obtained by interchanging 1 and 3, because 1 < 3.
The next larger permutation has 3 in the first position, followed by 1 and 2 in increasing order,
namely, 312.
Finally, interchange 1 and 2 to obtain the last permutation, 321.
We have generated the permutations of 1, 2, 3 in lexicographic order. They are 123, 132, 213, 231,
312, and 321. 87
Generating Combinations
A combination is just a subset, thus we can use the correspondence
between subsets of {a1, a2, …, an} and bit strings of length n.
E.g. bit string 110100 represents subset {a,b,d} of the set {a,b,c,d,e,f}
To find all the subsets, start with the bit string 000..00, with n zeros. Then
successively find the next expansion until the bit string 111..11 is
obtained. At each stage the next expansion is found by locating the first
position from the right that is not a 1, then changing all the 1s to the right
of the position to 0s and making this first 0 (from the right) a 1.
Example:
Find the next bit string after 10 0010 0111
Sol: The first bit from the right that is not a 1 is the fourth bit from the right. Change
this bit to a 1 and change all the following bits to 0s. This produces the next larger bit
string 10 0010 1000
88
Algorithm: Generating the Next Larger Bit String.
Algorithm : Generating The Next Larger Bit String.
procedure next bit string(bn-1bn-2…b1b0: bit string not equal to 11…11)
i :=0
while bi = 1
begin
bi := 0
i := i + 1
end
bi := 1
89
ALGORITHM: Generating the Next r-Combination in
Lexicographic Order.
Algorithm: Generating the Next r-Combination in Lexicographic Order.
procedure next r-combination({a1, a2, . . . , ar }: proper subset of {1, 2, . . . , n} not equal to
{n − r + 1, . . . , n} with a1 < a2 < · · · < ar )
i := r
while ai = n − r + i
i := i − 1
ai := ai + 1
for j := i + 1 to r
aj := ai + j − i
{{a1, a2, . . . , ar } is now the next combination}
90
Example
Algorithm:
1. Sort the combination.
2. From right to left, find the first position i so that ai can be increased.
3. Increase ai by 1.
4. For the numbers to the right of ai(if any), set to increased order starting from ai.
• Q) Find the next larger 4-combination of the set {1, 2, 3, 4, 5, 6} after {1, 2, 5, 6}.
• Sol: The last term among the terms ai with a1 = 1, a2 = 2, a3 = 5, and a4 = 6 such that
• ai ≠ 6 − 4 + i is a2 = 2. To obtain the next larger 4-combination, increment a 2 by 1 to obtain a2 = 3. Then set
a3 = 3 + 1 = 4 and a4 = 3 + 2 = 5. Hence the next larger 4-combination is {1, 3, 4, 5}.
91