Basic Counting Principles
Counting
Determining the number of elements of a finite
set
Example: Password is a word of length 5, 6 or
7 symbols that can be letters or digits. We
want to find the number of all possible
Counting Rules
Product Rule: Suppose that a procedure can be broken down into a sequence of two tasks. If
there are n1ways to do the first task and for each of these ways of doing the first task, there are n2 ways to do the second task, then there are n1n2 ways to do the procedure.
An extended version of the product rule is often useful. Suppose that a procedure is carried
out by performing the tasks T1, T2, . . . , Tm in sequence. If each task Ti , i = 1, 2, . . . , n, can be done in ni ways, regardless of how the previous tasks were done, then there are n1 · n2 · · · nm
Counting examples
The chairs in the room can be labeled as L DD (one letter and
two digits, what is the maximal number of chairs labeled in different way? We suppose alphabet with 26 letters
Answer: 26*10*10
There are 38 students in a class, and 38 chairs, how
many different seating arrangements are there if everyone shows up?
Answer: 38*37*36 … *3*2*1
How many different bit strings of length seven are there? Answer: 2*2*2*2*2*2*2= 27
Important cases of the Product Rule
Cartesian product
|A1 A2 … An| = |A1||A2|. . . |An|
Subsets of a set S
|P(S)|= 2|S|
Strings of length n over
Counting Rules
Sum Rule: If there are n1 choices of an element from S1 and n2 choices of an element from S2 and S1 S2 is empty, then there are n1 + n2 choices of an element from S1 S2
We can extend the sum rule to more than two tasks. Suppose that a task can be done in one of n1 ways, in one of n2 ways, . . . , or in one of
nm ways, where 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.
Then the number of ways to do the task is n1 + n2
Combination of counting principles
BASIC variable names of length at most 2
Variables can be one or two characters long
The first character must be a letter
The second character can be a letter or a digit
The keywords “TO”, “IF”, and “DO” are excluded
Two sets of variables:
a) one letter – 26 variables
b) two symbols - 26.(26+10) - 3 variables Together 26+ 26.(26+10) - 3 =959
Counting Functions
Suppose |S| = n, |T| = m
How many functions from S to T?
Answer : m
nHow many one-to-one functions from S to T?
Answer :
m.(m-1).(m-2). … .(m-n+1)
when
m
n
otherwise
0
.
Counting Passwords
Passwords must be 4 to 6 characters long, and must contain at least one letter and at least one digit. (Case insensitive, no special characters)
3 types of passwords :
a) 4 symbols – passwords of all digits or letters should be excluded – 4(26+10) – 426 - 410
a) 4 symbols – passwords of all digits or letters should be excluded – 5(26+10) – 526 - 510
a) 4 symbols – passwords of all digits or letters should be excluded – 6(26+10) – 626 - 610
The Division Rule
There are n/d ways to do a task if it can be done using a procedure that can be carried out in n ways, and for every way w, exactly d of the n ways correspond to way w.
We can restate the division rule in terms of sets: “If the finite set A is the union of n pairwise disjoint subsets each with d elements, then n = |A|/d.”
The Division Rule
How many 2 element subsets can be chosen from the set A={a,b,c,d,e}?
Answer:(5.4)/2=10
How many 3 element subsets can be chosen from the set A={a,b,c,d,e}?
Inclusion-Exclusion Principle
How many binary strings of length 9 start with
0 or end with 11?
|A
1|=2
8|A
2
|=2
7|A
1
A
2|=2
6Answer: 2
8+ 2
7- 2
6=256+128-64=320
Inclusion-Exclusion
How many numbers from 1 to 100 are
divisible by 5?
Answer: :
100/5
= 20
How many numbers from 1 to 100 are
divisible by 3?
Answer:
100/3
= 33
How many numbers from 1 to 100 are
divisible by 3 or 5?
Inclusion-Exclusion
A class of 40 students has 20 CS majors, 15
Math majors. 5 of these students are dual
majors. How many students in the class are
neither math, nor CS majors?
Generalized Inclusion Exclusion
Principle
|ABC|=|A|+|B|+|C|-|AB|-|AC|-|BC|+|ABC|
Example: How many numbers from 1 to 100 are not divisible by 2, 3 or 5.
.
Generalized Inclusion Exclusion
Principle
26 7 14 27 7 13 3 326 = 100 – (50 +33 +20) + (16 + 10 + 6) - 3 50
33
20
16 10
Pigeon Hole Principle
If k is a positive integer and k+1 or more
objects are placed into k boxes, then at least one box has two or more objects
If N objects are placed into k boxes, then there is at least one box containing at least
PHP Applications
How many students should be in the class to
guarantee that at least two students receive the same score of midterm exam, if the midterm exam is
graded from 0 to 20? Answer: 22
Prove that if you have 800 people, at least three share a common birthday.
Exercises
A multiple-choice test contains 10 questions. There are four possible answers for each question.
a) In how many ways can a student answer the questions on the test if the student answers every question?
b) In how many ways can a student answer the
questions on the test if the student can leave answers blank?
Exercises
How many bit strings of length ten both begin and end with a 1?
How many bit strings are there of length six or less, not counting the empty string?
Exercises
How many positive integers between 5 and 31 a) are divisible by 3? Which integers are these? b) are divisible by 4? Which integers are these?
Exercises
How many positive integers less than 1000 a) are divisible by 7?
b) are divisible by 7 but not by 11? c) are divisible by both 7 and 11? d) are divisible by either 7 or 11?
e) are divisible by exactly one of 7 and 11? f ) are divisible by neither 7 nor 11?
Exercises
A bowl contains 10 red balls and 10 blue balls. A woman selects balls at random without looking at them.
a) How many balls must she select to be sure of having at least three balls of the same color?
b) How many balls must she select to be sure of having at least three blue balls?