Lecture 15 Counting.
Lecture 15 Counting.
Lecture 15 Counting.
Counting (Ch. 6)
Counting??
What??
Are we first graders??
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
Combinatorics
Combinatorics: study of arrangement of objects
Enumeration: counting of objects with certain
properties
5
Basic Counting Principles
2
Product Rule
Sum Rule
6
The Product Rule
• Also called the multiplication rule
• If there are n1 ways to do task 1, and n2
ways to do task 2:
– Then there are n1n2 ways to do both tasks in
sequence
– This applies when doing the “procedure” is
made up of separate tasks.
– We must make one choice AND a second
choice
7
EXAMPLE 1
at page# 386
• A new company with just two employees, Sanchez and
Patel, 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: Possible ways of allotment of any office to first
employee = 12
Task 2: Possible ways of allotment of any of the remaining
office to second employee = 11
• (Product Rule) All possible ways of allotment of offices
to two employees = 12 * 11 = 132
8
EXAMPLE 2
at page# 386
• The chairs of an auditorium are to be labeled with an uppercase
English letter followed by a positive integer not exceeding 100. What
is the largest number of chairs that can be labeled differently?
• Solution:
• Break the procedure into tasks:
assigning to the seat one of the 26 uppercase English letters,
and then assigning to it one of the 100 possible integers.
• The product rule shows that there are 26 · 100 = 2600 different ways
that a chair can be labeled.
11
EXAMPLE 4
at page# 386
• 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 product rule
Each task can be done in 2 different ways. because each bit is either
0 or 1.
By product rule, There are 27= 128 different bit strings of length
seven.
12
EXAMPLE 5
at page# 387
• How many different license plates can be made if each plate contains a sequence of three
uppercase English letters followed by three digits (and no sequences of letters are
prohibited, even if they are obscene)?
• Solution:
• Break the procedure into tasks
Task 1: choose letter 1
Task 2: choose letter 2
Task 3: choose letter 3
Task 4: choose digit 1
Task 5: choose digit 2
Task 6: choose digit 3
• Count different ways of doing each task and then use the product
rule
By product rule, There are 26 . 26 . 26 .10 .10 . 10 = 17576000 different
license plates.
13
Product Rule Example
• Sample question
– There are 18 math majors and 25 CS majors
– How many ways are there to pick one math
major and one CS major?
14
More Examples
• What is the value of k after the following code,
where n1, n2, . . . , nm are +ive integers:
15
The Sum Rule
• Also called the addition rule
• If there are n1 ways to do task 1, and n2 ways to do
task 2
– If these tasks can be done at the same time, then…
– Then there are n1+n2 ways to do one of the two tasks
– We must make first choice OR a second choice
16
Sum Rule Examples
More sample questions
• How many strings of 4 decimal (non-zero)digits…
• Have exactly three digits that are 9s?
– The string can have:
• The non-9 as the first digit
• OR the non-9 as the second digit
• OR the non-9 as the third digit
• OR the non-9 as the fourth digit
• Thus, we use the sum rule
– For each of those cases, there are 9 possibilities for the non-
9 digit (any number other than 9)
– Thus, the answer is 9+9+9+9 = 36
17
Sum Rule Examples
• Sample question
– There are 18 math majors and 25 CS majors
– How many ways are there to pick one math major or
one CS major?
• Total is 18 + 25 = 43
18
• n1 + n 2 + … + n m
19
Example# 12
at Page# 389
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 choices are there for this representative if
there are 37 members of the mathematics faculty and 83 mathematics
majors and no one is both a faculty member and a student?
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.
Exercise Q. 1,3
20
Extended version of the sum rule
• 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 nm 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 I
and j with 1 ≤ i < j ≤ m.
The task can be done in n1+n2+…+nm different ways.
21
Example# 13
at Page# 390
A student can choose a computer project from one
of three lists. The three lists contain 23, 15, and 19
possible projects, respectively. No project is on
more than one list. How many possible projects are
there to choose from?
Solution:
• The student can choose a project from the first list,
the second list or the firs list.
• By the sum rule, there are 23+15+19=57 ways to
choose a project.
22
Misc Examples
There are 18 mathematics majors and 325
computer science majors at a college
• In how many ways can two representatives be
picked so that one is a mathematics major and
the other is a computer science major?
18 . 325
• In how many ways can one representative be
picked who is either a mathematics major or a
computer science major?
18 + 325
23
• A multiple-choice test contains 10 questions. There
are four possible answers for each question.
In how many ways can a student answer the
questions on the test if the student answers every
question?
Solution:
4 * 4 * …. * 4 (10 times)= 410
In how many ways can a student answer the
questions on the test if the student can leave answers
blank?
Solution:
510 24
The product rule and the sum rule
• Some complicated counting problems can be
solved using both the product rule and the sum
rule.
25
Example# 17
at Page# 391
• 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.
26
• Task 1: choose a Class A address.
• Task 2: choose a Class B address.
• Task 3: choose a Class C address.
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.
27
• By the product rule, the number of Class B netids is 2 14.
• 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.
• By the product rule, the number of Class C netids is 2 21.
• 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.
28
• A particular brand of shirt comes in 12 colors, has
a male version and a female version, and comes
in three sizes for each gender. How many
different types of this shirt are made?
• Solution:
12 * ( 3 + 3)
29
• A committee is formed consisting of one
representative from each of the 50 states in the
United States, where the representative from a
state is either the governor or one of the two
senators from that state. How many ways are
there to form this committee?
• (1+1+1)50
30
• 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?
• Solution:
103 * 263 + 263 * 103
31
• 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?
• Solution:
262*104 + 102*264
32
Recommended exercises
• 3,7,11,13,15,21,31,38,39,59
33
6.2 What is the Pigeon-Hole Principle?
• The Pigeon-Hole Principle states that if p items (Pigeons) are
put into h pigeonholes with p>h, then at least one pigeonhole
must contain more than one item.
e o n.
ig
n ep
th an o
m o re
in s
c onta
n h ole
ige o
s p
Th i
Example Problem
Here is how we se
up the problem. We
have 3 holes, for the
grades, and 25
students.
A B C
48
49
Example# 5, 6
at Page# 401 and 402
50
The Last slide. VIS
• How many numbers must be selected from the
set {1, 2, 3, 4 , 5, 6} to guarantee that at least one
pair of these numbers add up to 7?
• Solution:
Total numbers = 6
Possible ways to add up to 7= (1,6), (2, 5), (3, 4)
We have to select at least 4 numbers, so that at least
one pair from above 3 pairs is selected.
51
Recommended exercises
• 3,5,9,13,21,25,31
52