0% found this document useful (0 votes)
625 views52 pages

Session 10 - Advanced Counting

The document discusses advanced counting techniques using recurrence relations. It provides examples of recurrence relations for problems such as the number of bit strings without consecutive zeros, compound interest, Fibonacci sequence, and the Tower of Hanoi problem. The document outlines methods for solving linear recurrence relations, including substitution and using the characteristic equation of the relation. Properties of relations such as reflexivity, symmetry, transitivity, and composition of relations are also covered.

Uploaded by

Phúc Nguyễn
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)
625 views52 pages

Session 10 - Advanced Counting

The document discusses advanced counting techniques using recurrence relations. It provides examples of recurrence relations for problems such as the number of bit strings without consecutive zeros, compound interest, Fibonacci sequence, and the Tower of Hanoi problem. The document outlines methods for solving linear recurrence relations, including substitution and using the characteristic equation of the relation. Properties of relations such as reflexivity, symmetry, transitivity, and composition of relations are also covered.

Uploaded by

Phúc Nguyễn
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/ 52

Vietnam National University of HCMC

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.

Ex: Consider the recurrence relation: an+1 = 2an (n > 0)


[Given a1=1]

The solution is: 1, 2, 4, 8, 16 …., i.e. an = 2n-1

So, a30 = 229


Example:
Compound Interest:
A person deposits $10,000 in a savings account
that yields 11% interest annually. How much will
be there in the account After 30 years?
Solution: the definition is recursive.
Let Pn = account balance after n years.
Then Pn = Pn-1 + 0.11 Pn-1 = (1.11)Pn-1
Initial condition is P0 = 10,000$
P1 = (1.11)P0 ; P2 = (1.11)P1 = (1.11)2P0
P3 = (1.11)P2 = (1.11)3P0

Pn = (1.11)Pn-1 = (1.11)nP0
P30 = (1.11)30 10,000 = 228922.96
Example:
Compute population:
Suppose the population of VN now (2017) are 95
millions. The rate of increasing annually is 0.2%.
Compute the population of VN in year 2020

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:

a) bn = 1: number of bit strings b equal number of bit


strings b1b2..bn-1 that have no 00 and equal an-1.
b) bn = 0, then bn-1 = 1: number of bit strings b equal
number of bit strings b1b2..bn-2 that have no 00 and
equal an-2.
Example:
Following the sum rule, we have the recurrence
relation: an = an-1 + an-2 with two first values:
a1 = 2 and a2 = 3

Example:

For n = 1, the strings are 0 and 1 (a1)


For n = 2, the strings are 01, 10, 11 (a2)
For n = 3, the strings are 011, 111, 101, 010, 110 (a3)

Could you compute for n = 4?, n = 5?, …?


Tower of Hanoi

Transfer these disks from one peg to another. However, at no time, a


disk should be placed on another disk of smaller size. Start with 64
disks. When you have finished transferring them one peg to another,
the world will end.
Tower of Hanoi

Let, Hn = number of moves to transfer n disks. Then


Hn = 2Hn-1 +1
This is a recurrence relation with degree 1, so we
need the first value is H1 = 1. However, it is not a final
solution.
Tower of Hanoi
We can use an iterative approach to solve this
recurrence relation. Note that:

Therefore, with 64 disks, the number of transferring


is: H64 = 264 - 1 = 18,446,744,073,709,551,615
see: HanoiT.cpp
Tower of Hanoi
void move(int n, int Col1, int Col3, int Col2)
{

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

an = c1.an-1 + c2.an-2 + c3.an-3 + …+ ck.an-k

(where c1, c2, …, cn are constants)

Its solution is of the form an = rn (where r is a


constant) if and only if r is a solution of

rn = c1.rn-1 + c2. rn-2 + c3. rn-3 + …+ ck. rn-k

This equation is known as the characteristic equation.


B. Solving Linear Recurrence Relations

Solving recurrence relation is finding an


equation for general an so that no need to
compute by k previous elements.
There are to methods:
ØSubstitution method
ØUsing the characteristic equation
Substitution method

Example: Let Cn is the number of


transferring of n disk in Hanoi Tower. We
have recurrence relation:
Cn= 2Cn-1 + 1 and C1=1
= 22Cn-2+2+1
= 2n-1C1+2n-2+…+22+2+1
= 1+2+…+2n-1= 2n-1
Substitution method
In the Compound Interest: suppose Interest annually
is “I”; initial money is “M”, after n years we have
recurrence relation:
Mn=Mn-1 + IMn-1= (1+ I)Mn-1 . This is a recurrence
relation, degree 1 with M0 = M.
Mn = (1+ I)Mn-1
= (1+ I)2Mn-2

=(1+ I)nM0
So Mn = (1+ I)nM.
Characteristic Equation
In order to solve linear homogenous recurrence relation,
degree 2 with constant coefficient:
an = c1an-1 + c2an-2 ,c2¹ 0 (1) with first value a0 = I0, a1 = I1
we have characteristic equation: x2=c1x + c2 (2)
Theorem 1:
If a1 and a2 are two distinguish results of (2), then exist
only constants b and d so that an = ba1n + da2n
Theorem 2:
If a is a double result of (2), then exist only constants b
and so that an = ban + dnan
Example 1
Solve: an = an-1 + 2 an-2 (Given that a0 = 2 and a1 = 7)
Its solution is of the form an = rn
The characteristic equation is: r2 - r - 2 = 0. It has
two results r = 2, and r = -1
The sequence {an} is a solution to this recurrence
relation iff
an = α1 2n + α2 (-1)n
a0 = 2 = α1 + α2
a1 = 7 = α1. 2 + α2.(-1) This leads to α1= 3; α2 = -1

So, the solution is an = 3. 2n - (-1)n


Example 2: Fibonacci sequence
Solve: fn = fn-1 + fn-2 (Given that f0 = 0 and f1 = 1)
Its solution is of the form fn = rn
The characteristic equation is: r2 - r - 1 = 0. It has two
roots
r = ½(1 + √5) and ½(1 - √5)
The sequence {an} is a solution to this recurrence
relation iff
fn = α1 (½(1 + √5))n + α2 (½(1 - √5))n
(Now, compute α1 and α2 from the initial conditions): α1 = 1/√5 and α2 = -1/√5

The final solution is fn = 1/√5. (½(1 + √5))n - 1/√5.(½(1 - √5))n


Example
Given recurrence relation:
an = 4an-1 - 4an-2, a0 = 1, a1 = 3.
Solution:
Characteristic equation: x2 = 4x - 4
has double results x = 2.
an = b2n + dn2n.
a0= 1 and a1= 3 we have b = 1 and d = 1/2.
So
an = 2n + n2n-1.
Relations (chapter 8)
Ø Definition

Ø 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.

A relation allows multiple mappings between the


domain and the co-domain.
Example: students enrolled in multiple courses.
Relation within a set
Properties of Relations
We study six properties of relations:

What are these?


Reflexivity

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}

EXAMPLE. Let A = {1, 2, 3}, B = { 1, 2, 3, 4}, C = {0,


1, 2}
S = {(1,1), (1,4), (2,3), (3, 1), (3, 4)}
R = {(1,0), (2,0), (3,1), (3, 2), (4,1)

Then S ◦ R = {(1,0), (1,1), (2,1), (2,2), (3,0), (3,1)


More operations on relations:
Composition
Rn = Rn-1 ◦ R = R ◦ R ◦ R ◦ R … (n times)

EXAMPLE. Let R = {(1,1), (2,1), (3,2), (4,3)},. Then


R2 = R ◦ R = {(1,1), (2,1), (3, 1), (4,2)}
R3 = R2 ◦ R = {(1,1), (2,1), (3, 1), (4,1)}
R4 = R3 ◦ R = {(1,1), (2,1), (3, 1), (4,1)}

Notice that in this case for all n > 3, Rn = R3


n-ary relations
Has important applications in computer databases.
DEFINITION. Let A1, A2, A3, …, An be n sets. An n-ary
relation is a subset of A1 x A2 x A3 x… x An

EXAMPLE. R is a relation on N x N x N consisting of


triples (a, b, c) where a < b < c. Thus (1, 2, 3) ∈ R but
(3, 6, 2) ∉ R
Relational Data Model
Student Record
Name ID Major GPA
Alice 211 324 Physics 3.67
Bob 123 456 ECE 3.67
Carol 351 624 ECE 3.75
David 000 888 Computer 3.25
Science
The above table can be viewed as a 4-ary relation consisting of
the 4-tuples
(Alice, 211324, Physics, 3.67)
(Bob, 123456, ECE, 3.67)
(Carol, 351624, ECE, 3.75)
(David, 000888, Computer Science, 3.25)
Relational Data Model
Name ID Major GPA
Alice 211 324 Physics 3.67
Bob 123 456 ECE 3.67
Carol 351 624 ECE 3.75
David 000 888 Computer 3.25
Science

A domain is called a primary key when no two n-tuples


in the relation have the same value from this domain.
(These are marked red).
Operations on n-ary relations
SELECTION
Let R be an n-ary relation, and C be a condition that the
elements in R must satisfy. Then the selection operator
SC maps the n-ary relation R to the n-ary relations from R
that satisfy the condition C. Essentially it helps filter out tuples
that satisfy the desired properties. For example, you may filter
out the tuples for all students in ECE, or all students whose GPA
exceeds 3.5.
Operations on n-ary relations
PROJECTION
The projection Pi,j,k,…,m maps each n-tuple (a1, a2, a3, …, an) to
the tuple (ai, aj, ak, …, am).
Essentially it helps you delete some of the components of each
n-tuple. Thus, in the table shown earlier, the projection P1,4 will
retain only that part of the table that contains the student names
and their GPAs.
Use of the operations on n-ary relations

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

A reflexive relation on a given set A is recognized by a 1 along


the diagonal

1
1 0 0
0
1 1 0
1
1 1 1 0

A reflexive relation A symmetric relation


Representing Relations Using Digraph
A relation on a given set A can also be represented by a
directed graph

1 2 3 2
1 1 0 0
1

2 1 1 0

3
3 1 1 1

A directed graph representation


Let A = {1, 2, 3} of the relation shown on the left
Equivalence Relations
An equivalence relation on a set S is a relation that is
reflexive, symmetric and transitive.
Examples are:
(1) Congruence relation R = {(a,b) | a = b (mod m)}
(2) R = {(a, b) | L(a) = L(b)} in a set of strings of
English characters}, L(a) denotes the length of
English character string “a”
Partial Orders
A relation R on a set S is a partial order if it is reflexive,
anti-symmetric and transitive. The set is called a
partially ordered set, or a poset.
Examples are
(1) the ≥ relation,
(2) “x divides y” on the set of positive integers
(3) The relation ⊆ on the power set of a set S
Partial Orders

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

You might also like