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

counting rule in descrete structure

The document discusses counting principles in discrete structures, focusing on the product and sum rules, which are used to determine the number of ways to perform tasks. It includes numerous examples to illustrate these principles, such as selecting majors, labeling chairs, and calculating possible telephone numbers. Additionally, it covers the inclusion-exclusion principle and the pigeonhole principle, providing a comprehensive overview of counting applications in computer science and mathematics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

counting rule in descrete structure

The document discusses counting principles in discrete structures, focusing on the product and sum rules, which are used to determine the number of ways to perform tasks. It includes numerous examples to illustrate these principles, such as selecting majors, labeling chairs, and calculating possible telephone numbers. Additionally, it covers the inclusion-exclusion principle and the pigeonhole principle, providing a comprehensive overview of counting applications in computer science and mathematics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Discrete Structures

Lecture - 13
Counting
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
• …
Basic Counting Principles
• Two basic counting principles
• The product rule
• The product rule applies when a procedure is made up of separate
tasks.
• The sum rule
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.
• Assume for each of these ways of doing the first task,
there are n2 ways to do the second task.
• 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
The Product Rule
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
• Count different ways of doing each task and then use the
product rule
• Task 1 can be done in 18 different ways and for each of these
ways, Task 2 can be done in 25 different ways.
• By product rule, There are 18 * 25 = 450 ways to pick courses.
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:
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
• Count different ways of doing each task and then use the
product rule
• Task 1 can be done in 26 different ways and for each of these
ways, Task 2 can be done in 100 different ways.
• 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?
• Solution:
• Break the procedure into tasks
• Task 1: assigning an office to employee 1
• Task 2: assigning an office to employee 2
• Count different ways of doing each task and then use the
product rule
• Task 1 can be done in 12 different ways and for each of these
ways, Task 2 can be done in 11 different ways.
• By product rule, There are 12 *11 = 132 ways to assign offices to
two employees.
Extended Version of The Product Rule

• A procedure can be broken down into a sequence of tasks


T1 , T2 , … , Tm
• 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.
Example
• How many different bit strings of length seven are there?
• Solution:
• Break the procedure into tasks
• Task 1: assigning bit 1 to 0 or 1
• Task 2: assigning bit 2 to 0 or 1
• …
• Task 7: assigning bit 7 to 0 or 1
• Count different ways of doing each task and then use the
product rule
• Each task can be done in 2 different ways.
• By product rule, There are 27 = 128 different bit strings of length
seven.
Example
• The format of telephone numbers in North America is
specified by a numbering plan.
• Let X denote a digit between 0 and 9.
• Let N denote a digit between 2 and 9.
• 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
• How many strings of 4 decimal digits…

a) Do not contain the same digit twice?

b) End with an even digit?


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)
• By the product rule, the nested loops traversed n1n2 … n m
times.
• So the final value of k is n1 n2 … nm .
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.
• If these tasks can be done at the same time, then there
are n1 + n2 ways to do the task.
• We must make one choice OR a second choice.
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:

• 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.
Example
• Assume there are 37 members of the mathematics faculty
and 83 mathematics majors.
• Suppose that either a member of the mathematics faculty
or a student who is a mathematics major is chosen as a
representative to a university committee.
• How many different ways to choose this representative?
• Solution:
• There are 37 ways to choose a faculty member.
• There are 83 ways to choose a student.
• By the sum rule, there are 37+83=120 different ways to
choose such a representative.
Extended Version of The Sum Rule

• Suppose a task can be done in one of n1 ways, in one of


n2 ways, … , or in one of n m ways.

• Assume none of the set of ni ways of doing the task is the


same as any of the set of nj ways, for all pairs 𝑖 and 𝑗 with
1 ≤ 𝑖 < 𝑗 ≤ 𝑚.

• The task can be done in n1 + n2 + ⋯ + n m different ways.


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:
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 .
The Product Rule and The Sum Rule

• Some complicated counting problems can be solved


using both the product rule and the sum rule.
Example
• 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?
• Task 1: choose a variable name with one character.
• Task 2: choose a variable name with two alphanumeric
characters.
Example
• On the internet, each computer is assigned an internet
address.
• An address is a string of 32 bits. It begins with a network number
(netid) followed by a host number (hostid).
• There are three forms of addresses.
• Class A addresses consists of 0, followed by a 7-bit netid and a 24-bit
hostid.
• Class B addresses consists of 10, followed by a 14-bit netid and a 16-
bit hostid.
• Class C addresses consists of 110, followed by a 21-bit netid and a 8-
bit hostid.
• There are several restrictions:
• 1111111 is not available as the netid of a class A network.
• Hostids do not consists of all 0s and all 1s are not available for use in any
network.
• How many different addresses are available for computers on
the Internet.
Example
0 1 2 3 8 16 24 31
Class A 0 netid hostid

Class B 1 0 netid hostid

Class C 1 1 0 netid hostid

• Task 1: choose a Class A address.


• Task 2: choose a Class B address.
• Task 3: choose a Class C address.
Example
• Since the netid of class A cannot be 1111111, the number of
Class A netids is 27 - 1.

• Since the hostid cannot consist all 0s and all 1s, the number of
Class A hostids is 224 - 2.
• By the product rule, the number of task 1 is (27 - 1)(224 - 2) =
2,130,706,178.

• By the product rule, the number of Class B netids is 214.


• Since the hostid cannot consist all 0s and all 1s, the number of
Class B hostids is 216 - 2.
• By the product rule, the number of task 2 is 214 (216 - 2)
=1,073,709,056.
Example
• By the product rule, the number of Class C netids is 221.
• Since the hostid cannot consist all 0s and all 1s, the
number of Class C hostids is 28 − 2.
• By the product rule, the number of task 1 is 221 (28 − 2) =
532,676,608.

• By the sum product, the number of Internet addresses are


the number of task 1 + the number of task 2 + the number
of task 3 = 3,737,091,842.
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.

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


𝑛2 and subtract the number of ways that is common in 𝑛1
ways and 𝑛2 ways.
Example
• |A𝖴B|?
• Solution:
• Task 1: choose an element from A.
• Task 2: choose an element from B.
• Since A has |A| elements, the number of task 1 is |A|.
• Since B has |B| elements, the number of task 2 is |B|.
• The elements that are in both A and B are common in task 1 and
task 2 and the number of them are |A∩B|.
• By inclusion-exclusion principles, the number of task 1 and task 2
(= |A𝖴B|) is |A|+|B|- |A∩B|.
Example
• How many bit strings of length eight either start with a 1
bit or end with two bits 00?
• Solution:
• Task 1: choose a bit strings of length eight starts with 1.
• Task 2: choose a bit strings of length eight ends with 00.
• Since the first bit must be 1 in task 1, by the product rule, the
number of task 1 is 27.
• Since the last two bits must be 00 in task 2, by the product rule, the
number of task 2 is 26.
• The strings of length eight that start with 1 and end 00 are common
in task 1 and task 2 and the number of them are 25.
• By inclusion-exclusion principles, the number of such strings is
27 + 26 − 25 = 160.
Example
• 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 applicants majored in neither computer
science and business?
• Solution:
• First find the number of applicants that are majored in
computer science or business, then subtract it from the total
number of applicants
• Task 1: choose an applicant majored in computer science
• Task 2: choose an applicant majored in business.
• The number of task 1 is 220.
• The number of task 2 is 147.
Example

• The applicants that are majored both in computer science


and business are common in task 1 and task 2 and the
number of them are 51.

• By inclusion-exclusion principles, the number of task 1


and task 2 is 220+147-51=316.

• So, the number of applicants majored in neither computer


science and business is 350-316=34.
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.
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.
Example
• Show that among any group of 367 people, there must be
at least two with the same birthday.

• Solution:

• To use pigeonhole principle, first find boxes and objects.


• Suppose that for each day of a year, we have a box that contains a
birthday that occurs on that day.
• The number of boxes is 366 and the number of objects is 367.
• By the pigeonhole principle, at least one of these boxes contains
two or more birthdays.
• So, there must be at least two people with the same birthday.
Example
• Show that in any group of 27 English words, there must
be at least two that begin with the same letter.

• Solution:
• To use pigeonhole principle, first find boxes and objects.
• Suppose that for each letter, we have a box that contains a word
that begins with that letter.
• The number of boxes is 26 and the number of objects is 27.
• By the pigeonhole principle, at least one of these boxes contains
two or more words.
• So, there must be at least two words that begin with the same
letter.
The Generalized Pigeonhole Principle
• Assume 25 pigeons fly into 12 pigeonholes to rest.
• A least one of 12 pigeonholes must have at least three
pigeons in it.
The Generalized Pigeonhole Principle

• Assume N objects are placed into k boxes.

• So, there is at least one box containing at least


N/k objects.
Floor and Ceiling
• floor(x) = X is the largest integer that is less than or
equal to x and ceiling(x) = X is the smallest integer that
is greater than or equal to x
• The floor and ceiling functions give you the nearest
integer up or down.

Sample value x Floor 𝒙 Ceiling 𝒙

12/5 = 2.4 2 3

2.7 2 3

−2.7 −3 −2

−2 −2 −2
Example
• Show among 100 people there are 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 least one of these
boxes contains at least 100/12 = 9 persons.
• So, there must be at least 9 persons who were born in the same
month.
Example
• 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.
• Solution:
• To use pigeonhole principle, first find boxes and objects.
• Suppose that for each grade, we have a box that contains students
who got that grade.
• The number of boxes is 5, by the generalized pigeonhole principle,
to have at least 6 (= N/5 ) students at the same box, the total
number of the students must be at least N = 5 . 5 + 1 = 26.
Exercise Questions

Chapter # 5
Topic # 5.1
Q 1,2,3,4,5,6,7,8,9,10,11,26,27,28,29,30,31-a,b
Chapter Reading
Chapter # 5
Topic # 5.1(The Basics of Counting)
Topic # 5.2 (The Pigeonhole Principle)

You might also like