Counting
Counting
CC1041 - Discrete
Structures By
Arshi Iftikhar
2
Lecture Outline
• Counting
• Product Rule
• Sum Rule
• Product and Sum rule mix
questions
• Inclusion-Exclusion Principle
• The Pigeonhole Principle
3
Counting Applications
• For example,
• Counting the number of operations used by an algorithm
to study its time complexity
• Counting the successful outcomes of experiments
• Counting all the possible outcomes of experiments
• …
4
Basic Counting
Principles
• Two basic counting
principles
• The product rule
• The sum rule
5
The Product
Rule
7
Exampl
e
8
Example
• There are 18 math majors and 25 CS majors
• How many ways are there to pick one math major
and one CS major?
• Solution:
• Break the procedure into tasks
• Task 1: Math major
• Task 2: CS major
• By product rule, There are 18 * 25 = 450 ways to pick
courses.
9
Example
• There are 32 microcomputers in a computer
center. Each microcomputer has 24 ports. How
many different ports to a microcomputer in the
center are there.
• Solution:
• Break the procedure into tasks
• Task 1: Choosing a microcomputer
• Task 2: Choosing a port
• By product rule, There are 32*24 = 768 ways to choose a
port to a microcomputer.
10
Example
• The chairs of an auditorium are to be labeled with
a letter and a positive integer not exceeding
100. How many chairs can be labeled differently?
• Solution:
• Break the procedure into tasks
• Task 1: assigning one of the 26 letters
• Task 2: assigning one of the 100 possible integers
• By product rule, There are 26*100 = 2600 ways to assign
labels to
the chairs.
11
Example
• A new company with just two employees, rents a
floor of a building with 12 offices. How many
ways are there to assign different offices to these
two employees?
• Solution:
• Break the procedure into tasks
• Task 1: assigning an office to employee 1
• Task 2: assigning an office to employee 2
• By product rule, There are 12 *11 = 132 ways to assign
offices to two employees.
12
T1, T2, … , Tm
of tasks
Exampl
e
• How many different bit strings of length seven
are there?
•• Solution: b7 b6 b5 b4 b3 b2 b1
Break the procedure into
tasks 2 2 2 2 2 2 2
• Task 1: assigning bit 1 to 0 or
1
• Task 2: assigning bit 2 to 0 or
1
• Count
• …
different ways of doing each task and then
use the product rule
• Task 7: assigning bit 7 to 0 or
• Each task can be done in 2 different ways.
• By product rule, There are 27 = 128 different bit strings
1
of length seven.
14
Example
• How many uppercase English letter strings of
length three are there?
L1 L2 L3
263
26 26 26
• Other versions of above question:
L1 L 2 L 3
(Same letter not repeated)
26 25 24
A L2 L3
(Start with Letter A)
1 26 26
A L2 L3
(Start with Letter A and s ame letter not repeated)
1 25 24
15
Exampl
e
• The formatof telephone numbers
in North America is specified by a
X N Y
numbering plan.
• Let X denote a digit between 0
and 9. 10 8 2
• Let N denote a digit between 2
andthe
• In 9. old plan, The format of telephone numbers is
• Let Y denote a digit between 0
NYX-NNX-
and 1.
XXXX.
• In the new plan, The format of telephone numbers is
NXX-NXX- XXXX.
• How many north American telephone numbers
are possible under the old plan and under the new
plan.
16
Exampl
e
In the old plan, the formats of the area code, office
code, and station code are NYX, NNX, and XXXX,
respectively, so that telephone numbers had the form
NYX-NNX-XXXX. X N
• 8 ∗ 2 ∗ 10 = 160 area codes with
Solution:
Y
10 8 2
8 ∗∗810
∗ 10 = 640
format NYX.
• 10
• ∗ 10 ∗ 10office
= 10,000
codesstation
with codes with format
format NNX.
XXXX.
• Consequently, applying the product rule
1,024,000,000
• Under the new plan, 800 ∗ 800 ∗ 10,000 =
17
Example
• Solution:
d4 d3 d2 d1
10 9 8 7
10 ∗ 9 ∗ 8 ∗ 7 = 5040 𝑤𝑎𝑦𝑠
18
Example
• How many different license plates are available
if each plate contains a sequence of three
letters followed by three digits?
L1 L2 L3 D1 D2 D3
26 26 26 10 10 10
• 26 ∗ 26 ∗ 26 ∗ 10 ∗ 10 ∗ 10 =
17,576,000 possible license plates
19
Example
• If repetitions of letters is not
allowed:
L1 L 2 L3 D1 D2 D3
26 * 25* 24
26 25 24 10 10 10
*10*10*10
• If repetitions of letters and digits are not
allowed:
L1 L 2 L3 D1 D2 D3
26 * 25* 24 *10 *9
26 25 24 10 9 8
*8
• If sequence of letters is LHE and repetitions of
digits are not allowed:
L H E D1 D 2 D 3
1*1*1*10 *9
1 1 1 10 9 8
*8
20
Example
• What is the value of k after the following code
has been executed?
int k 0;
for(int i 1;i 7; i )
for(int j 1;j 8; j )
for(int n 1;n 10;n )
k k 1;
• Solution:
• 7 ∗ 8 ∗ 10 = 560
21
Example
• What is the value of k after the following code
has been executed?
• Solution:
• Task i: traversing the i-th loop. (1≤i≤m)
n1 n2 … n m
• By the product rule, the nested loops traversed
The Sum
Rule
24
Exampl
e
25
Example
• There are 18 math majors and 25 CS majors
• How many ways are there to pick one math major
or one CS major?
• Solution:
Example
• A student can choose a computer project from
one of three lists. The three lists contains 23, 15
and 19 possible projects. No project is on more
than one list.
• How many possible projects are there to choose
from?
• Solution:
23 + 15 + 19 = 57
29
Example
• What is the value of k after the following code
has been executed?
int k 0;
for(int i 1;i 7; i )
k k 1;
for(int j 1;j 8; j )
k k 1;
for(int n 1;n 10; n )
k k 1;
• Solution:
• 7 + 8 + 10 = 25
30
Example
• What is the value of k after the following code
has been executed?
• Solution:
• Task i: traversing the i-th loop. (1 ≤ 𝑖 ≤ 𝑚 )
• By the sum rule, the final value of k is n1 + n2 +
⋯ + nm .
31
Example
• In a version of the computer language BASIC, the
name
of a variable is a string of one character or
alphanumeric characters, where two
lowercase
uppercase letters are not distinguished. and (An
alphanumeric character is either one of the 26
English letters or one of the 10 digits.) Moreover,
a variable name must begin with a letter. How
many different variable names are there in this
version of BASIC?
33
L D A L D
Exampl 26 10 26 10
36
e
• In a version of the computer language BASIC, the
name of a variable is a string of one
character or two alphanumeric characters,
where uppercase and lowercase letters are
not distinguished. (An alphanumeric character is
either one of the 26 English letters or one of the
10 digits.) Moreover, a variable name must begin
with a letter. How many different variable names
are there in this version of BASIC?
• V1 (L): variable name with one character. (int a)
• V2 (LA): variable name with two
• 𝑉1 = 26, 𝑉2 = 26 ∗ 36 = 936
alphanumeric characters. (int aa; int a1;)
• 𝑉1 + 𝑉2 = 26 + 936 = 962
34
Example
Example
add 𝑛1 and
• To count the number of ways to the task, we
Exampl ABA B A
e B
• How many bit strings of length eight either start
with a 1 bit or end with two bits 00?
38
Exampl ABA B A
e B
•How many bit strings of length eight either start with
a 1 bit or end with two bits 00?
Solution:
27 + 26 − 25 = 160.
strings is
39
Exampl ABA B A
e B
• How many bit strings of length eight either start
with a 1 bit or end with two bits 00?
Solution:
• Task 1: bit strings of length eight starts with 1.
• Task 2: bit strings of length eight ends with 00.
• By the product rule, the number of task 1 is 27.
• By the product rule, the number of task 2 is 26.
25.
• Common in task 1 and task 2 and the number of them are
27 + 26 − 25 = 160.
such strings is
40
Exampl ABA B A
e B
• How many uppercase English letter strings of
length three
either start with letter A or end with letter C?
41
Exampl ABA B A
e B
• How many uppercase English letter strings of length
three
either start with letter A or end with letter C?
Solution:
Exampl ABA B A
e B
• How many uppercase English letter strings of
length three either start with letter A or end with
letter C?
Solution:
• Task 1: Strings start with letter A.
• Task 2: Strings end with letter C.
• By the product rule, the number of task 1 is 262.
• By the product rule, the number of task 2 is 262.
26.
• Common in task 1 and task 2 and the number of them are
Exampl ABA B A
e B
• A computer company receives 350 applications
for a job. Suppose that 220 of them majored in
computer science, 147 of them majored in
business and 51 of them majored both in
computer science and business. How many of
these
businessapplicants majored in neither computer
Total 350
science
? nor
CS 220
B 147
• Solutio
CS B
• n:
By inclusion-exclusion principle, 220+147-51=316.
51
• So, the number of applicants majored in neither
computer science nor business is 350-316=34.
44
• K ⊆ 𝐙+
• Assume k+1 or more objects are placed into k
boxes.
• So, there is at least one box containing two or
more of the objects.
46
Example
• Among a group of 367 people (randomly chosen),
there must be at least two with the same
birthday, because there are only 365 possible
birthdays.
• In any group of 27 English words (randomly
chosen), there must be at least two that begin
with the same letter, because there are 26 letters
in the English alphabet.
• Among a set of 15 or more students, at least 3
are born
on the same day of the week.
47
Exampl
e
• Show among100 peoplethereare at least 9 who
were born in the same month.
• Solution:
• To use pigeonhole principle, first find boxes and
objects.
• Suppose that for each month, we have a box that contains
persons who was born in that month.
• The number of boxes is 12 and the number of objects
is100.
• By the generalized pigeonhole principle, at
100/12 = 9 persons.
least one of these boxes contains at least
Exercise
Questions
Chapter # 6
Topic # 6.1
Q
1,2,3,4,5,6,7,8,9,10,11,28,29,30,31,32,33-
a,b
52
Chapter Reading
Chapter # 6
Topic # 6.1(The Basics of
Counting) Topic # 6.2 (The
Pigeonhole Principle)