L 24 25 Counting
L 24 25 Counting
(Discrete Mathematics)
Counting Techniques
Lecture 24-25
Lecture Outline
• Counting
– Product Rule
– Sum Rule
– Product and Sum rule mix questions
– Inclusion-Exclusion Principle
• 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
– …
Basic Counting Principles
6
The Product Rule
• Suppose that a procedure can be broken into a
sequence of two tasks.
• Assume there are ways to do the first task.
• Assume for each of these ways of doing the
first task, there are ways to do the second task.
• So, there are ways to do the procedure.
– This applies when doing the “procedure” is made
up of separate tasks
– We must make one choice AND a second choice
The Product Rule
Example
T-1
F-1 T-2
T-3
T-1
F-2 T-2
T-3
Ice cream
T-1
F-3 T-2
T-3
T-1
F-4 T-2
T-3
Example
Math CS
• Solution:
• Break the procedure into tasks 18 25
– Task 1: Math major
– Task 2: CS major
– By product rule, There are 18 * 25 = 450 ways to pick courses.
Example
• 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.
Example
• 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.
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? Office
A L2 L3
(Start with Letter A)
1 26 26
A L2 L3
(Start with Letter A and same letter not repeated)
1 25 24
Example
• The format of telephone numbers in North America is
specified by a numbering plan.
X N Y
– Let X denote a digit between 0 and 9.
– Let N denote a digit between 2 and 9. 10 8 2
– Let Y denote a digit between 0 and 1.
– In the old plan, The format of telephone numbers is NYX-
NNX-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.
Example
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 Y
Solution: 10 8 2
• Solution:
d 4 d3 d 2 d1
10 9 8 7
Example
• Solution:
• Task i: traversing the i-th loop. (1im)
• By the product rule, the nested loops traversed times.
• So the final value of k is .
The Sum Rule
The sum rule
25
The Sum Rule
• Also called the addition rule.
• Assume a task can be done either in one of
ways or in one of ways.
• Assume none of the set of ways is the same
as any of the set ways.
• If these tasks can be done at the same time,
then there are ways to do the task.
• We must make one choice OR a second choice.
26
The Sum Rule
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:
37+83=120
37 83
Extended Version of The Sum Rule
23 15 19
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 )
• Solution: k k 1;
Example
• What is the value of k after the following code
has been executed?
• Solution:
L1 L2 L3 D1 D2 D3
Example
L1 L2 D1 D2 D3 D4
D1 D2 L1 L2 L3 L4
Example
How many three-digit integers are divisible by 5?
Solution:
Integers that are divisible by 5, end either in 5 or in 0.
CASE-I (Integers that end in 0)
There are nine choices for the left-most digit (the digits 1 through 9)
and ten choices for the middle digit.(the digits 0 through 9)
Hence, total number of 3 digit integers that end in 0 is
9 choices (1 to 9) 10 choices (0 to 9) 1 choice (digit 0)
9x10x1= 90
Example
How many three-digit integers are divisible by 5?
Solution:
Integers that are divisible by 5, end either in 5 or in 0.
CASE-II (Integers that end in 5)
There are nine choices for the left-most digit (the digits 1 through 9)
and ten choices for the middle digit.(the digits 0 through 9)
Hence, total number of 3 digit integers that end in 5 is
9 choices (1 to 9) 10 choices (0 to 9) 1 choice (digit 5)
9x10x1= 90
Finally, by sum rule, the number of 3 digit integers that are
divisible by 5 is
90 + 90 = 180
The Inclusion-Exclusion Principle
The Inclusion-Exclusion Principle
AB A B AB
Example
AB A B AB
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
Chapter Reading
Topic # 6.1(The Basics of Counting)