CS 441 Discrete Mathematics for CS
Lecture 16
Counting
Milos Hauskrecht
[email protected]
5329 Sennott Square
CS 441 Discrete mathematics for CS M. Hauskrecht
Course administration
Midterm exam:
• Thursday, March 6, 2014
Homework assignment 8:
• due after Spring break
• An opportunity to practice mathematical induction,
and basic counting for the midterm
Course web page:
http://www.cs.pitt.edu/~milos/courses/cs441/
CS 441 Discrete mathematics for CS M. Hauskrecht
1
Counting
• Assume we have a set of objects with certain properties
• Counting is used to determine the number of these objects
Examples:
• Number of available phone numbers with 7 digits in the local
calling area
• Number of possible match starters (football, basketball) given
the number of team members and their positions
CS 441 Discrete mathematics for CS M. Hauskrecht
Basic counting rules
• Counting problems may be very hard, not obvious
• Solution:
– simplify the solution by decomposing the problem
• Two basic decomposition rules:
– Product rule
• A count decomposes into a sequence of dependent counts
(“each element in the first count is associated with all
elements of the second count”)
– Sum rule
• A count decomposes into a set of independent counts
(“elements of counts are alternatives”)
CS 441 Discrete mathematics for CS M. Hauskrecht
2
Product rule
A count can be broken down into a sequence of dependent counts
• “each element in the first count is associated with all
elements of the second count”
Example:
• Assume an auditorium with a seat labeled by a letter and
numbers in between 1 to 50 (e.g. A23). We want the total
number of seats in the auditorium.
• 26 letters and 50 numbers
• How to count?
• One solution: write down all seats (objects) and count them
A-1 A-2 A-3 …A-50 B-1… Z-49 Z-50
1 2 3 50 51 … (n-1) n eventually we get it
CS 441 Discrete mathematics for CS M. Hauskrecht
Product rule
A count can be broken down into a sequence of dependent counts
• “each element in the first count is associated with all
elements of the second count”
Example:
• assume an auditorium with a seat labeled by a letter and
numbers in between 1 to 50 (e.g. A23). We want the total
number of seats in the auditorium.
• 26 letters and 50 numbers
• A better solution?
• For each letter there are 50 numbers
• So the number of seats is 26*50 = 1300
• Product rule: number of letters * number of integers in [1,50]
CS 441 Discrete mathematics for CS M. Hauskrecht
3
Product rule
A count can be broken down into a sequence of dependent counts
• “each element in the first count is associated with all
elements of the second count”
• Product rule: If a count of elements can be broken down into a
sequence of dependent counts where the first count yields n1
elements, the second n2 elements, and kth count nk elements, by
the product rule the total number of elements is:
• n = n1*n2* …* nk
CS 441 Discrete mathematics for CS M. Hauskrecht
Product rule
Example:
• How many different bit strings of length 7 are there?
• E.g. 1011010
• Is it possible to decompose the count problem and if yes how?
Yes.
• Count the number of possible assignments to bit 1
• For the first bit assignment (say 0) count assignments to bit 2
00
0 or
or 0 1
1
Total assignments to first 2 bits: 2*2=4
CS 441 Discrete mathematics for CS M. Hauskrecht
4
Product rule
Example:
• How many different bit strings of length 7 are there?
• E.g. 1011010
• Is it possible to decompose the count problem and if yes how?
• Yes.
– Count the number of possible assignments to bit 1
– For the specific first bit count possible assignments to bit 2
– For the specific first two bits count assignments to bit 3
– Number of assignments to the first 3 bits: 2*2*2=8
CS 441 Discrete mathematics for CS M. Hauskrecht
Product rule
Example:
• How many different bit strings of length 7 are there?
• E.g. 1011010
• Is it possible to decompose the count problem and if yes how?
• Yes.
– Count the number of possible assignments to bit 1
– For the specific first bit count possible assignments to bit 2
– For the specific first two bits count assignments to bit 3
– Gives a sequence of n dependent counts and by the product
rule we have:
n = 2* 2 * 2 * 2 * 2 * 2 *2 = 27
CS 441 Discrete mathematics for CS M. Hauskrecht
5
Product rule
Example:
The number of subsets of a set S with k elements.
• How to count them?
• Hint: think in terms of bitstring representation of a set?
• Assume each element in S is assigned a bit position.
• If A is a subset it can be encoded as a bitstring: if an element is
in A then use 1 else put 0
• How many different bitstrings are there?
– n = 2* 2* …2 = 2k
k bits
CS 441 Discrete mathematics for CS M. Hauskrecht
Sum rule
A count decomposes into a set of independent counts
• “elements of counts are alternatives”, they do not depend on
each other
Example:
• You need to travel in between city A and B. You can either fly,
take a train, or a bus. There are 12 different flights in between A
and B, 5 different trains and 10 buses. How many options do
you have to get from A to B?
• We can take only one type of transportation and for each only
one option. The number of options:
• n = 12+5+10
Sum rule:
• n = number of flights + number of trains + number of buses
CS 441 Discrete mathematics for CS M. Hauskrecht
6
Sum rule
A count decomposes into a set of independent counts
• “elements of counts are alternatives”
• Sum rule: If a count of elements can be broken down into a set
of independent counts where the first count yields n1 elements,
the second n2 elements, and kth count nk elements, by the sum
rule the total number of elements is:
• n = n1+n2+ …+ nk
CS 441 Discrete mathematics for CS M. Hauskrecht
Beyond basic counting rules
• More complex counting problems typically require a
combination of the sum and product rules.
Example: A login password:
• The minimum password length is 6 and the maximum is 8. The
password can consist of either an uppercase letter or a digit.
There must be at least one digit in the password.
• How many different passwords are there?
CS 441 Discrete mathematics for CS M. Hauskrecht
7
Beyond basic counting rules
Example: A password for the login name.
• The minimum password length is 6 and the maximum is 8. The
password can consist of either an uppercase letter or a digit.
There must be at least one digit in the password.
• How to compute the number of possible passwords?
Step 1:
• The password we select has either 6,7 or 8 characters.
• So the total number of valid passwords is by the sum rule:
• P = P6+P7+P8
The number of passwords of length 6,7 and 8 respectively
CS 441 Discrete mathematics for CS M. Hauskrecht
Beyond basic counting rules
Step 1:
• The password we select has either 6,7 or 8 characters.
• So the total number of valid passwords is by the sum rule:
• P = P6+P7+P8
The number of passwords of length 6,7 and 8 respectively
Step 2
• Assume passwords with 6 characters (upper-case letters):
• How many are there?
• If we let each character to be at any position we have:
– P6-nodigits = 266 different passwords of length 6
CS 441 Discrete mathematics for CS M. Hauskrecht
8
Beyond basic counting rules
Step 1:
• The password we select has either 6,7 or 8 characters.
• So the total number of valid passwords is by the sum rule:
• P = P6+P7+P8
The number of passwords of length 6,7 and 8 respectively
Step 2
• Assume passwords with 6 characters
(either digits + upper case letters):
• How many are there?
• If we let each character to be at any position we have:
– P6-all = (26+10)6=(36)6 different passwords of length 6
CS 441 Discrete mathematics for CS M. Hauskrecht
Beyond basic counting rules
Step 2
But we must have a password with at least one digit. How to
account for it?
A trick. Split the count of all passwords of length 6 into to two
mutually exclusive groups:
• P6-all = P6-digits + P6-nodigits
1. P6-digits – count when the password has one or more digits
2. P6-nodigits – count when the password has no digits
• We know how to easily compute P6-all and P6-nodigits
– P6-all = 366 and P6-nodigits = 266
– Then P6-digits = P6-all – P6-nodigits
CS 441 Discrete mathematics for CS M. Hauskrecht
9
Beyond basic counting rules
Step 1:
the total number of valid passwords is by the sum rule:
• P = P6+P7+P8
• The number of passwords of length 6,7 and 8 respectively
Step 2
The number of valid passwords of length 6:
P6= P6-digits = P6-all – P6-nodigits
= 366 – 266
Analogically:
P7= P7-digits = P7-all – P7-nodigits
= 367 – 267
P8= P8-digits = P8-all – P8-nodigits
= 368 – 268
CS 441 Discrete mathematics for CS M. Hauskrecht
Inclusion-Exclusion principle
Used in counts where the decomposition yields two dependent
count tasks with overlapping elements
• If we used the sum rule some elements would be counted twice
Inclusion-exclusion principle: uses a sum rule and then corrects
for the overlapping elements.
We used the principle for the cardinality of the set union.
• |A B| = |A| + |B| - |A B|
U B
CS 441 Discrete mathematics for CS M. Hauskrecht
10
Inclusion-exclusion principle
Example: How many bitstrings of length 8 start either with a bit 1
or end with 00?
• It is easy to count strings that start with 1:
• How many are there? 27
• It is easy to count the strings that end with 00.
• How many are there? 26
• Is it OK to add the two numbers to get the answer? 27 + 26
• No. Overcount. There are some strings that can both start with 1
and end with 00. These strings are counted in twice.
• How to deal with it? How to correct for overlap?
• How many of strings were counted twice? 25 (1 xxxxx 00)
• Thus we can correct for the overlap simply by using:
• 27 + 26 – 25 = 128+ 64-32 = 160
CS 441 Discrete mathematics for CS M. Hauskrecht
Tree diagrams
Tree: is a structure that consists of a root, branches and leaves.
• Can be useful to represent a counting problem and record the
choices we made for alternatives. The count appears on the leaf
nodes.
Example:
What is the number of bit strings of length 4 that do not have two
consecutive ones.
CS 441 Discrete mathematics for CS M. Hauskrecht
11
Tree diagrams
Example:
What is the number of bit strings of length 4 that do not have two
consecutive ones?
Empty string
1 0
CS 441 Discrete mathematics for CS M. Hauskrecht
Tree diagrams
Example:
What is the number of bit strings of length 4 that do not have two
consecutive ones?
Empty string
1 0
CS 441 Discrete mathematics for CS M. Hauskrecht
12
Tree diagrams
Example:
What is the number of bit strings of length 4 that do not have two
consecutive ones?
Empty string
1 0
1 0
CS 441 Discrete mathematics for CS M. Hauskrecht
Tree diagrams
Example:
What is the number of bit strings of length 4 that do not have two
consecutive ones?
Empty string
1 0
1 0
0 1 0
CS 441 Discrete mathematics for CS M. Hauskrecht
13
Tree diagrams
Example:
What is the number of bit strings of length 4 that do not have two
consecutive ones?
Empty string
0
1
1 0
0 1 0
(1010)
CS 441 Discrete mathematics for CS M. Hauskrecht
Tree diagrams
Example:
What is the number of bit strings of length 4 that do not have two
consecutive ones?
Empty string
1 0
0 1 0
1 0 0
0 1
0 1 0 1 0 0 1 0
(1010)
CS 441 Discrete mathematics for CS M. Hauskrecht
14
Pigeonhole principle
• Assume you have a set of objects and a set of bins used to store
objects.
• The pigeonhole principle states that if there are more objects
than bins then there is at least one bin with more than one object.
• Example: 7 balls and 5 bins to store them
CS 441 Discrete mathematics for CS M. Hauskrecht
Pigeonhole principle
• Assume you have a set of objects and a set of bins used to store
objects.
• The pigeonhole principle states that if there are more objects
than bins then there is at least one bin with more than one object.
• Example: 7 balls and 5 bins to store them
• At least one bin with more than 1 ball exists.
CS 441 Discrete mathematics for CS M. Hauskrecht
15
Pigeonhole principle
• Assume you have a set of objects and a set of bins used to store
objects. The pigeonhole principle states that if there are more
objects than bins then there is at least one bin with more than
one object.
• Theorem. If there are k+1 objects and k bins. Then there is
at least one bin with two or more objects.
…
k bins
CS 441 Discrete mathematics for CS M. Hauskrecht
Pigeonhole principle
• Assume you have a set of objects and a set of bins used to store
objects. The pigeonhole principle states that if there are more
objects than bins then there is at least one bin with more than
one object.
• Theorem. If there are k+1 objects and k bins. Then there is
at least one bin with two or more objects.
• Proof. (by contradiction)
• Assume that we have k + 1 objects and every bin has at most
one element. Then the total number of elements is k which is a
contradiction.
• End of proof
CS 441 Discrete mathematics for CS M. Hauskrecht
16
Pigeonhole principle
Example:
• Assume 367 people. Are there any two people who has the same
birthday?
• How many days are in the year? 365.
• Then there must be at least two people with the same birthday.
CS 441 Discrete mathematics for CS M. Hauskrecht
Generalized pigeonhole principle
• We can often say more about the number of objects.
• Say we have 5 bins and 12 objects. What is it we can say about
the bins and number of elements they hold?
• There must be a bin with at least 3 elements.
• Why?
• Assume there is no bin with more than 3 elements. Then the max
number of elements we can have in 5 bins is 10. We need to
place 13 so at least one bin should have at least 3 elements.
CS 441 Discrete mathematics for CS M. Hauskrecht
17
Generalized pigeonhole principle
Theorem. If N objects are placed into k bins then there is at least
one bin containing at least N / k objects.
Example. Assume 100 people. Can you tell something about the
number of people born in the same month.
• Yes. There exists a month in which at least
100 / 12 = 8.3 = 9 people were born.
CS 441 Discrete mathematics for CS M. Hauskrecht
18