COUNTING
Lecture # 21
1
COUNTING
In combinatorics we generally solved counting problems.
Example: How many books you have you can easily
count them.
Sometimes counting problems become complex and it is
difficult to solve it.
In counting problem the important step is we must
realized that which thing we are counting, if you do that
step then to solve the actual problem is not a big deal.
2
INTRODUCTION
Combinatorics is the mathematics of counting and arranging
objects.
Counting of objects with certain properties (enumeration) is
required to solve many different types of problem.
For example: Counting is used to:
Determine number of ordered or unordered arrangement of
objects.
Generate all the arrangements of a specified kind which is
important in computer simulations.
3
How many such passwords are there.
Analyze the chance of winning games, lotteries etc.
Determine the complexity of algorithms.
4
BASIC COUNTING PRINCIPLES
The two basic counting principles are:
Product Rule
Sum Rule
They can be used to solve many different counting
problems.
5
EXAMPLE
Your institute is offering 7 courses in computer science
and 3 courses in mathematics. You are asked to choose
only one course.
How many choices you have?
You can select either one computer science course or
mathematics.
7 cs courses = 7 choices
3 math courses = 3 choices
Total number of choices = 7 + 3 (basically by applying
sum rule)
6
THE SUM RULE
If one event can occur in n1 ways.
A second event can occur in n2 (different) ways.
Then the total number of ways in which exactly one of the
events (i.e., first or second) can occur is
n 1 + n 2.
7
EXAMPLE
A student can choose a computer project from one of the
three lists. The three lists contain 23, 15 and 19 possible
projects, respectively.
How many possible projects are there to choose from?
SOLUTION:
23 choices are in first list,
15 choices are in second list,
19 choices are in third list. Hence, there are
So, we have total number of choices
= 23 + 15 + 19 = 57 projects to choose from.
8
GENERALIZED SUM RULE
If one event can occur in n1 ways,
a second event can occur in n2 ways,
a third event can occur in n3 ways,
……………………………………..
then there are
n 1 + n2 + n3 + …
ways in which exactly one of the events can occur.
9
SUM RULE IN TERMS OF SETS
If A1, A2, …, Am are finite disjoint sets, then the number of
elements in the union of these sets is the sum of the
number of elements in them.
If n(Ai) denotes the number of elements in set Ai, then
n(A1A2 … Am) = n(A1) + n(A2) + … + n(Am)
where
AiAj = if i j
10
EXAMPLE
Suppose
There are 7 different optional courses in Computer
Science and
3 different optional courses in Mathematics.
A student who wants to take one optional course of each
subject, there are:
7 3 = 21 choices.
11
THE PRODUCT RULE
If one event can occur in n1 ways and if for each of these
n1 ways, a second event can occur in n2 ways.
Then the total number of ways in which both events occur
is n1· n2
12
EXAMPLE
The chairs of an auditorium are to be labeled with two
characters, a letter followed by a digit.
What is the largest number of chairs that can be labeled
differently?
13
SOLUTION
The procedure of labeling a chair consists of two events,
namely,
Assigning one of the 26 letters: A, B, C, …, Z and
Assigning one of the 10 digits: 0, 1, 2, …, 9
By product rule, there are 26 10 = 260 different ways
that a chair can be labeled by both a letter and a digit.
14
GENERALIZED PRODUCT RULE
If some event can occur in n1 different ways, and if,
following this event, a second event can occur in n2
different ways, and following this second event, a third
event can occur in n3 different ways, …, then the number
of ways all the events can occur in the order indicated is
n 1· n 2· n 3· …
15
PRODUCT RULE IN TERMS OF SETS
If A1, A2, …, Am are finite sets, then the number of
elements in the Cartesian product of these sets is the
product of the number of elements in each set.
If n(Ai) denotes the number of elements in set Ai, then
n(A1 A2 … Am) = n(A1)· n(A2)· …· n(Am)
16
EXERCISE
Find the number n of ways that an organization consisting of
15 members can elect a president, treasurer, and secretary.
(assuming no person is elected to more than one position)
SOLUTION:
The president can be elected in 15 different ways;
The treasurer can be elected in 14 different ways;
The secretary can be elected in 13 different ways.
Thus, by product rule, there are
n = 15 x 14 x 13 = 2730
different ways in which the organization can elect the
officers.
17
EXERCISE
There are four bus lines between A and B; and three bus
lines between B and C.
Find the number of ways a person can travel:
(a) By bus from A to C by way of B;
(b) Round trip by bus from A to C by way of B;
(c) Round trip by bus from A to C by way of B, if the
person does not want to use a bus line more than
once.
18
SOLUTION
(a) There are 4 ways to go from A to B and 3 ways to go from B
to C;
hence there are
4 x 3 = 12 ways to go from A to C by way of B.
(b) The person will travel from A to B to C to B to A for the
round trip.
i.e (A →B →C →B →A)
The person can travel
4
4 ways
3 3
from4 A to B and 3 way from B to
C and back. A B C B A
i.e.,
Thus there are 4 x 3 x 3 x 4 = 144 ways to travel the round trip.
19
(c) The person can travel 4 ways from A to B and 3 ways
from B to C, but only 2 ways from C to B and 3 ways
from B to A,
Since bus line cannot be used more than once. Thus
4 3 2 3
i.e., A B C B A
Hence there are 4 x 3 x 2 x 3 = 72 ways to travel the round
trip without using a bus line more than once.
20
EXERCISE
How many bit strings of length 8
(i) begin with a “1”?
(ii) begin and end with a “1”?
21
SOLUTION
(i) If the first bit (left most bit) is a 1, then it can be filled in
only one way.
Each of the remaining seven positions in the bit string can
be filled in 2 ways (i.e., either by 0 or 1).
Hence,
there are 1 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 27 = 128 different
bit strings of length 8 that begin with a 1.
22
(ii) If the first and last bit in an 8 bit string is a 1, then only
the intermediate six bits can be filled in 2 ways, i.e. by a 0
or 1.
Hence there are 1 x 2 x 2 x 2 x 2 x 2 x 2 x 1 = 26 = 64
different bit strings of length 8 that begin and end with a
1.
23
EXERCISE
Suppose that an automobile license plate has three letters
followed by three digits.
How many different license plates are possible?
24
SOLUTION
Each of the three letters can be written in 26 different
ways, and each of the three digits can be written in 10
different ways.
letters digits
26 ways each 10 ways each
Hence, by the product rule, there is a total of
26 x 26 x 26 x 10 x 10 x 10 = 17,576,000
different license plates possible.
25
(b) How many license plates could begin with A and end on
0?
SOLUTION:
The first and last place can be filled in one way only, while
each of second and third place can be filled in 26 ways and
each of fourth and fifth place can be filled in 10 ways.
letters digits
A 0
one way 26 ways each 10 ways each one way
Number of license plates that begin with A and end in 0 are 1 x
26 x 26 x 10 x 10 x 1 = 67600
26
EXERCISE
A variable name in a programming language must be
either a letter or a letter followed by a digit.
How many different variable names are possible?
27
SOLUTION
First consider variable names one character in length.
Since such names consist of a single letter, there are 26 variable
names of length 1.
Next, consider variable names two characters in length.
Since the first character is a letter, there are 26 ways to choose
it. The second character is a digit, there are 10 ways to choose it.
Hence, to construct variable name of two characters in length,
there are 26 x10 = 260 ways.
Finally, by sum rule, there are 26 + 260 = 286 possible variable
names in the programming language.
28
EXERCISE
A computer access code word consists of from one to
three letters of English alphabets with repetitions allowed.
How many different code words are possible.
29
SOLUTION
Number of code words of length 1 = 261
Number of code words of length 2 = 262
Number of code words of length 3 = 263
Hence, the total number of code words
= 261 + 262 + 263
= 18,278
30
NUMBER OF ITERATIONS OF A
NESTED LOOP
Determine how many times the inner loop will be iterated
when the following algorithm is implemented and run
for i: = 1 to 4
for j : = 1 to 3
[number of statements]
next j
next i
31
SOLUTION
The outer loop is iterated four times, and during each
iteration of the outer loop, there are three iterations of the
inner loop.
Hence, by product rules the total number of iterations of
inner loop is 4· 3 = 12
32
EXERCISE
Determine how many times the inner loop will be iterated
when the following algorithm is implemented and run.
for i = 5 to 50
for j: = 10 to 20
[number of statements]
next j
next i
33
SOLUTION
The outer loop is iterated 50 - 5 + 1 = 46 times and
Inner loop iterate for 20 - 10 + 1 = 11 times.
Hence by product rule, the total number of iterations of
the inner loop is 46.11 = 506
34
EXERCISE
Determine how many times the inner loop will be iterated
when the following algorithm is implemented and run.
for i = 1 to 4
for j: = 1 to i
[number of statements]
next j
next i
35
The outer loop is iterated 4 times, but during each iteration of
the outer loop, the inner loop iterates different number of times.
For first iteration of outer loop, inner loop iterates 1 times.
For second iteration of outer loop, inner loop iterates 2 times.
For third iteration of outer loop, inner loop iterates 3 times.
For fourth iteration of outer loop, inner loop iterates 4 times.
Hence, total number of iterations of inner loop = 1 + 2 + 3 + 4 =
10
36
THE PIGEONHOLE PRINCIPLE
37
THE PIGEONHOLE PRINCIPLE
Its very useful principle for many counting problems.
Suppose that a flock of 20 pigeons flies into a set of 19
pigeonholes to roost. Because there are 20 pigeons but
only 19 pigeonholes, at least one of these 19 pigeonholes
must have at least two pigeons in it.
38
This illustrates a general principle called the pigeonhole
principle, which states that if there are more pigeons
than pigeonholes, then there must be at least one
pigeonhole with at least two pigeons in it.
39
40
REVISION OF FUNCTIONS
Clearly the above relation is not a function because 2 does
not have any image under this relation. Note that if want to
made it relation we have to must map the 2 into some
element of B which is also the image of some element of
A. Now
41
The above relation is a function because it satisfy the
conditions of functions(as each element of 1st set have the
images in 2nd set).
42
PIGEONHOLE PRINCIPLE
We define a function from A to B. If A has more elements
then B. B must have element which is image of more than
one element of A.
43
The following is a function.
The above relation is a function because it satisfy the
conditions of functions(as each element of 1st set have the
images in 2nd set). Therefore the above is also a function.
44
PIGEONHOLE PRINCIPLE
A function from a set of k + 1 or more elements to a set of
k elements must have at least two elements in the domain
that have the same image in the co-domain.
If k + 1 or more pigeons fly into k pigeonholes then at
least one pigeonhole must contain two or more pigeons.
45
EXAMPLES
1. Among any group of 367 people, there must be at least
two with the same birthday, because there are only 366
possible birthdays.
2. In any set of 27 English words, there must be at least
two that begin with the same letter, since there are 26
letters in the English alphabet.
46
EXERCISE
What is the minimum number of students in a class to be
sure that two of them are born in the same month?
SOLUTION:
There are 12 (= n) months in a year.
The pigeonhole principle shows that among
any 13 (= n + 1) or more students there must be
at least two students who are born in the same month.
47
EXERCISE
Given any set of seven integers, must there be two that have
the same remainder when divided by 6?
SOLUTION:
The set of possible remainders that can be obtained when
an integer is divided by six is {0, 1, 2, 3, 4, 5}.
This set has 6 elements.
Thus by the pigeonhole principle if 7 = 6 + 1 integers are
each divided by six, then at least two of them must have the
same remainder.
48
EXERCISE
How many integers from 1 through 100 must you pick in
order to be sure of getting one that is divisible by 5?
SOLUTION:
There are 20 integers from 1 through 100 that are
divisible by 5.
Hence there are 80 integers from 1 through 100 that are
not divisible by 5.
Thus by the pigeonhole principle 81 = 80 + 1 integers
from 1 though 100 must be picked in order to be sure of
getting one that is divisible by 5.
49
EXERCISE
Let A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. Suppose six integers are chosen
from A. Must there be two integers whose sum is 11.
SOLUTION:
The set A can be partitioned into five subsets:
{1, 10}, {2, 9}, {3, 8}, {4, 7}, and {5, 6} each consisting
of two integers whose sum is 11.
These 5 subsets can be considered as 5 pigeonholes.
If you pick six elements. Then you have to pick one set from above sets.
If 6 = (5 + 1) integers are selected from A, then by the pigeonhole
principle at least two must be from one of the five subsets. But then the
sum of these two integers is 11.
50
GENERALIZED PIGEONHOLE
PRINCIPLE
Imagine we have 10 pigeonholes, and we want to ensure
that at least one pigeonhole have more than 2 pigeons in
it.
If you have 20 pigeons then you can have 2 pigeons in
each pigeonholes.
If you have 21 pigeons then you can have 2 pigeons in
each pigeonholes and in 1 pigeonhole you have more than
2 pigeons in it.
51
GENERALIZED PIGEONHOLE
PRINCIPLE
A function from a set of nk + 1 or more elements to a set
of n elements must have at least k + 1 elements in the
domain that have the same image in the co-domain.
If n k + 1 or more pigeons fly into n pigeonholes then at
least one pigeonhole must contain k + 1 or more pigeons
52
EXERCISE
Suppose a laundry bag contains many red, white, and blue
socks.
Find the minimum number of socks that one needs to
choose in order to get two pairs (four socks) of the same
color.
53
SOLUTION
Here there are n = 3 colors (pigeonholes)
and k + 1 = 4 or k = 3.
Thus among any nk + 1 = 33 + 1 = 10 socks (pigeons),
at least four have the same color.
54
FLOOR & CEILING FUNCTIONS
Given any real number x, the floor of x, denoted x, is the
largest integer smaller than or equal to x.
Example: 3.4 = 3 and 5 = 5
Given any real number x, the ceiling of x, denoted x, is
the smallest integer greater than or equal to x.
Example: 3.4 = 4 and 5 = 5
55
EXAMPLE
Compute x and x for each of the following values of x.
a. 25/4 b. 0.999 c. –2.01
SOLUTION:
a. 25/4 = 6 + ¼ = 6
25/4 = 6 + ¼ = 6 + 1 = 7
b. 0.999 = 0 + 0.999 = 0
0.999 = 0 + 0.999 = 0 + 1 = 1
c. –2.01 = –3 + 0.99 = –3
–2.01 = –3+ 0.999 = –3 + 1 = –2
56
PIGEONHOLE PRINCIPLE
If N pigeons fly into k pigeonholes then at least one
pigeonhole must contain N /k or more pigeons.
57
EXAMPLE
We have 100 people and we want to know how many of
them are born in same month?
Solution:
n = 12 months (pigeonholes)
The required no. is 100/12 = 8 + 1/3 = 9 who were
born in the same month.
58
EXERCISE
What is the minimum number of students required in a
Discrete Mathematics class to be sure that at least six will
receive the same grade, if there are five possible grades,
A, B, C, D, and F.
59
SOLUTION
We want to ensure that there are 6 students who have the
same grade that is
N/5 = 6 (given)
The smallest such integer is
N = 5(6-1)+1 = 5 5 + 1 = 26
Thus 26 is the minimum number of students needed to be
sure that at least 6 students will receive the same grades.
60