Session 10 - Advanced Counting
Session 10 - Advanced Counting
International University
School of Computer Science and Engineering
Session 7
Advanced Counting
Dr. Nguyen Van Sinh
[email protected]
Contains
Why advanced counting?
Many counting problem cannot be solved easily by using
the methods discussed in sessions 5 and 6 such as:
Ø How many bit strings of length n do not contain two
consecutive zeros?
Ø How many ways are there to assign 7 jobs to 3
employees so that each employee is assigned at least one
job?
In order to solve this problem, we can base on the
“Recurrence Relation”.
In this section, we are working on two issues:
Ø Recurrence relations.
Ø Solving linear recurrence relations
A. Recurrence Relation
Define: A recurrence relation for the sequence {an} is an equation
that expresses an in terms of one or more of the previous terms
of the sequence, namely, a0, ai, …, an-1 for all integers n with n ³
n0, where n0 is a nonnegative integer. A sequence is called a
solution of a recurrence relation if its terms satisfy the recurrence
relation.
Example:
Let {an} be a sequence that satisfies the recurrence relation an =
an-1 - an-2 for n = 2, 3, 4, ... , and suppose that a0 = 3 and a1 = 5.
What are a2 and a3?
Solution:
We see from the recurrence relation that a2 = a1 - a0 = 5 - 3 = 2,
and a3 = a2 - a1 = 2 - 5 = -3. Similarly: a4 = a3 - a2 = -3 - 2 = -5.
A. Recurrence Relation
Recursively defined sequences are also known as
recurrence relations. The actual sequence is a
solution of the recurrence relations.
Solution:
Let Pn is population n years after 2017.
We have recurrence relation: Pn = Pn-1 + 0.002 Pn-1
Or Pn = 1.002Pn-1
…
P6 = (1.002)395 = ?millions
Example:
Fibonacci sequence:
an = an-1 + an-2 (n > 2) [Given a1 = 1, a2 = 1]
a3 = a2 + a1 = 1 + 1 = 2
a4 = a3 + a2 = 2 + 1 = 3
a5 = a4 + a3 = 3 + 2 = 5
a6 = a5 + a4 = 5 + 3 = 8
…
an = an-1 + an-2
Example:
Find the number of bit strings of length n that do not
have two consecutive 0s.
Solution:
Let b = b1 b2 .. bn be a bit string. Let an is a number of
bit strings b. Consider two following cases:
Example:
if(n>0)
{
move((n-1),Col1,Col2,Col3);
cout<<“Move a disk from Col1: “ <<fromTower <<
“to Col3: “ <<toTower<< “\n”;
move((n-1),Col2,Col3,Col1);
}
}
see: HanoiT.cpp
B. Solving Linear Recurrence Relations
A linear recurrence relation is of the form
Ø Representing Relations
Ø Properties of Relations
Ø Operations on relations
What is a relation?
What is a relation?
Representing Relations
Relations vs. Functions
When to use which?
A function yields a single result for any element in
its domain.
Example: age (of a person), square (of an integer)
etc.
Example.
= is reflexive, since a = a
≤ is reflexive, since a ≤ a
< is not reflexive is a < a is false.
Symmetry
Anti-symmetry
More on symmetric relations
Transitivity
Examples of transitive relations
Summary of properties
= < > ≤ ≥
Reflexive X X X
Irreflexive X X
Symmetric X
Asymmetric X X
Antisymmetric X X X
Transitive X X X X X
Operations on relations
Let A = {1, 2, 3} and B = (1, 2, 3, 4}. Define two
relations
R1 = {(1,1), (1,2), (1,3)}
R2 = {(1,1), (1,3), (1,4)}
Then,
R1 ⋃ R2 = {(1,1), (1,2), (1,3), (1,4)}
R1 ⋂ R2 = {(1,1), (1,3)}
R1 - R2 = {(1,2)}
More operations on relations:
Composition
Let S be a relation from the set A to the set B, and R
be a relation from the set B to the set C. Then, the
composition of S and R, denoted by S ◦ R is
{(a, c) | a ∈ A, b ∈ B, c ∈ C such that (a, b) ∈ S and (b, c) ∈
R}
SQL queries
SQL queries carry out the operations described earlier:
SELECT GPA
FROM Student Records
WHERE Department = Computer Science
Representing Relations Using Matrices
A relation between finite sets can be represented using a 0-1 matrix.
Let A = {a1, a2, a3} and B = {b1, b2, b3}. A relation R from A to B can
be represented by a matrix MR, where mij = 1 if (ai, bj) ∈ R,
otherwise mij = 0
b1 b2 b3
a1 0 0 0
a2 1 0 0
a3 1 1 0
The above denotes a relation R from A = {1,2,3} to B = {1,2,4}, where for each
element (a, b) of R, a > b
Representing Relations Using Matrices
1
1 0 0
0
1 1 0
1
1 1 1 0
1 2 3 2
1 1 0 0
1
2 1 1 0
3
3 1 1 1
The relation ⊆ on the power set of a set S forms a partially ordered set
Source: http://en.wikipedia.org/wiki/Partially_ordered_set
Homework
1. Revise chapter 8 in the textbook
2. 6, 8, 16, pages 457
3. 6, 8, 10, pages 471
4. Implement: HanoiTower.cpp:
a) Input n from keyboard
b) Print the number of moving disk
c) Compute processing time for each entered
number of n
Submit before 9:00PM, 21/4/2018 to the Blackboard