0% found this document useful (0 votes)
16 views

Counting

Uploaded by

sawerafatima193
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Counting

Uploaded by

sawerafatima193
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Department Of Artificial

Intelligence, UMT Lahore Campus

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

• Counting has many applications in computer


science and mathematics.

• 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


• Also called the multiplicative rule.
• Suppose that a procedure can be broken into a
sequence of two tasks.
• Assume there are n1 ways to do the first task.

task, there are n2 ways to do the second task.


• Assume for each of these ways of doing the first

• So, there are n1n2 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
6

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

Extended Version of The Product Rule

• A procedure can be broken down into a sequence

T1, T2, … , Tm
of tasks

• Assume each task Ti (i = 1,2, … , m), can be


done in 𝑛𝑖 different ways, regardless of how the
previous tasks were done.
• The procedure can be done in n1. n2 … n m different
ways.
13

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

under the old plan,160 ∗ 640 ∗ 10,000 =


again, it follows that

1,024,000,000
• Under the new plan, 800 ∗ 800 ∗ 10,000 =
17

Example

• How many strings of 4 decimal digits, do not


contain the same digit twice?

• 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

• So the final value of k is n1n2 … n m .


times.
22

The Sum Rule


• Also called the addition rule.
• Assume a task can be done either in one of n1
ways or in one of n2 ways.
• Assume none of the set of n1 ways is the same as
any of the set n2 ways.

then there are n1 + n2 ways to do the task.


• If these tasks can be done at the same time,

• We must make one choice OR a second choice.


23

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:

• There are 18 ways to pick Math major


• There are 25 ways to pick CS major

• By the sum rule, there are 18 + 25= 43 different


ways to pick courses.
28

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

The Product Rule and The Sum


Rule

• Some complicated counting problems


can be solved using both the product
rule and the sum rule.
32

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

• How many license plates can be made using


either three digits followed by three uppercase
English letters or three uppercase English letters
followed by three digits?
D1 D2 D3 L1 L2 L3

L1 L2 L3 D1 D2 D3

103 ∗ 263 + 263 ∗


103
35

Example

• How many license plates can be made using


either two uppercase English letters followed by
four digits or two digits followed by four
uppercase English letters?
L1 L2 D1 D2 D3 D4

D1 D2 L1 L2 L3 L4

262 ∗ 104 + 102 ∗ 264


36

The Inclusion-Exclusion Principle

• Suppose a task can be done in 𝑛1 or in 𝑛2 ways.

• However, some of the set of 𝑛1 ways are the same


as some of the 𝑛2 other ways.

add 𝑛1 and
• To count the number of ways to the task, we

𝑛2 and subtract the number of ways that is


common in 𝑛1
ways and 𝑛2 ways.
37

Exampl ABA 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 ABA B  A
e B
•How many bit strings of length eight either start with
a 1 bit or end with two bits 00?
Solution:

• By inclusion-exclusion principles, the number of such

27 + 26 − 25 = 160.
strings is
39

Exampl ABA 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

• By inclusion-exclusion principles, the number of

27 + 26 − 25 = 160.
such strings is
40

Exampl ABA 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 ABA B  A
e B
• How many uppercase English letter strings of length
three
either start with letter A or end with letter C?
Solution:

• Byinclusion-exclusion principles, the

262 + 262 − 26 = 1326.


number of such strings are:
42

Exampl ABA 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

• By inclusion-exclusion principles, the number of such


strings are:

262 + 262 − 26 = 1326.


43

Exampl ABA 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

The Pigeonhole Principle


• Assume 13 pigeons fly into 12 pigeonholes to rest.
• A least one of 12 pigeonholes must have
at least two pigeons in it.

• If there are more pigeons than pigeonholes,


then there must be at least one pigeonhole with
at least two pigeons in it.
45

The Pigeonhole Principle

• 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

The Generalized Pigeonhole Principle


• Assume 25 pigeons fly into 12 pigeonholes to rest.
• At least one of 12 pigeonholes musthave at
least three pigeons in it.

• When the numberof objectsexceeds a multiple


of the number of boxes.
48

The Generalized Pigeonhole


Principle
• Assume N objects are placed into k boxes.

• So, there is at least one box at


containing N/k objects. least
49

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

• So, there must be at least 9 persons who were born in


the same month.
51

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)

You might also like