Download a n-2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
7.1 Recurrence Relations
β€’ A recurrence relation is a recursive
definition for the terms of a sequence
β€’ A solution to a recurrence relation is a
non-recursive formula for the terms of the
sequence
Example: Determine whether the sequences π‘Žπ‘› = 2𝑛 and π‘Žπ‘› = 𝑛4𝑛 are
solutions of the recurrence relation π‘Žπ‘› = 8π‘Žπ‘›βˆ’1 βˆ’ 16π‘Žπ‘›βˆ’2 .
Another Example – The Towers of
Hanoi
How many moves are made in solving the
puzzle if there are n disks?
Solution by Iteration…
7.2 Solving Recurrence Relations
β€’ Definition: A linear homogeneous
recurrence relation of degree k with
constant coefficients
Solving Linear Homogeneous Recurrence Relations with Constant Coefficients
Theorem 1
Let c1 and c2 be real numbers. Suppose the
quadratic r2 – c1r – c2 has two real roots r1
and r2. Then {an} is a solution to the
recurrence relation an = c1an-1 + c2an-2 if and
only if an = Ξ±1r1n + Ξ±2r2n for n = 0, 1, 2, …,
where Ξ±1 and Ξ±2 are constants.
Example
β€’ Solve the recurrence relation for the Fibonacci sequence
0, 1, 1, 2, 3, 5, …
Another Example
β€’ Solve the recurrence relation for the sequence defined by
a0 = 1,
a1 = 2, and for n > 1 an = 2an-1 + 3an-2
Theorem 2
β€’ Let c1 and c2 be real numbers with c2
nonzero. Suppose that r2 – c1r – c2 = 0 has
only one real root r0. A sequence {an} is a
solution of the recurrence relation an =
c1an-1 + c2an-2 if and only if an = Ξ±1r0n + Ξ±2nr0n
for n = 0, 1, 2, …, where Ξ±1 and Ξ±2 are
constants
Example
β€’ Solve the recurrence relation for the sequence defined by
a0 = 1,
a1 = 2, and for n>1
an = 2an-1 – an-2
Example
β€’ Solve the recurrence relation for the sequence defined by
a0 = 6,
a1 = 8, and for n>1
an = 4an-1 – 4an-2
Theorem 3
Let c1, c2 , c3 , …, ck be real numbers with ck
nonzero. Suppose the equation
rk – c1rk-1 – c2rk-2 – … – ck-1r – ck = 0
has k distinct real roots r1 , r2 , … , rk. A sequence
{an} is a solution of the recurrence relation an =
c1an-1 + c2an-2 + … + ckan-k if and only if
an = Ξ±1r1n + Ξ±2r2n + … + Ξ±krkn
for n = 0, 1, 2, …, where Ξ±1, Ξ±2, …, Ξ±k are constants
Example
β€’ Solve the recurrence relation for the sequence defined by
a0 = 1,
a1 = 1,
a2 = 2, and for n > 1 an = 2an-1 + an-2 – 2an-3
7.3 Divide and Conquer Algorithms
and Recurrence Relations
Suppose an algorithm requires f(n)
operations for a problem of size n, and
suppose that for significant values of n the
algorithm reduces to a recursive calls each
of size n/b. Suppose also that the overhead
of performing the calls and consolidating the
results takes g(n) operations. This gives rise
to a recurrence relation, namely
f(n) = af(n/b) + g(n)
Examples
β€’ Binary Search: f(n) = f(n/2)+2
β€’ Maximum value in a sequence: f(n) = 2f(n/2)+2
β€’ Merge Sort: 2f(n/2) + n
Binary Search Solution by Iteration
β€’ Binary Search: f(n) = f(n/2)+2
Theorem 1
Let 𝑓 be an increasing function that satisfies the recurrence relation
𝑓 𝑛 = π‘Žπ‘“
𝑛
𝑏
+𝑐
whenever 𝑛 is divisible by 𝑏, where π‘Ž β‰₯ 1, 𝑏 is an integer greater than 1, and 𝑐
is a positive real number. Then
𝑂 𝑛log𝑏 π‘Ž 𝑖𝑓 π‘Ž > 1
𝑓(𝑛) is
.
𝑂(log 𝑛) 𝑖𝑓 π‘Ž = 1
Furthermore, when 𝑛 = π‘π‘˜ , where π‘˜ is a positive integer,
𝑓 𝑛 = 𝐢1 𝑛log𝑏 π‘Ž + 𝐢2 ,
𝑐
𝑐
where 𝐢1 = 𝑓 1 + π‘Žβˆ’1 and 𝐢2 = βˆ’ π‘Žβˆ’1.
Example: Find 𝑓 𝑛 when 𝑛 = 3π‘˜ , where 𝑓 satisfies the recurrence relation
𝑛
𝑓 𝑛 = 2𝑓 3 + 4 with 𝑓 1 = 1.
The Master Theorem
If a recursive algorithm for a problem of size n
makes a recursive calls on itself, each for a
problem of size n/b, and if the overhead of
performing the calls and consolidating the results
is cnd, then the number f(n) of operations
performed by the call is f(n) = af(n/b) + cnd, and
f(n) is
(a) O(nd) if a < bd,
(b) O(nd log n) if a = bd, and
(c) O(nlogb a ) if a > bd.
Binary Search Solution using the
Master Theorem
β€’ Binary Search: f(n) = f(n/2)+2
Fast Matrix Multiplication
f(n) = 7f(n/2) + 15n2/4
7.5 Inclusion/Exclusion
β€’ Two-Set Version
β€’ Three-Set Version
Example: Find the number of elements in 𝐴1 βˆͺ 𝐴2 βˆͺ 𝐴3 if
there are 100 elements in each set and if
a) The sets are pairwise disjoint
b) There are 50 common elements in each pair of sets
and no elements in all three sets
c) There are 50 common elements in each pair of sets
and 25 elements in all three sets
d) The sets are equal
Example: Suppose that 14 students receive an A on the
first exam in a class, and 18 receive an A on the second
exam. If 22 students received an A on either the first or the
second exam, how many students received an A on both
exams?
The Generalized Principle of
Inclusion/Exclusion
7.6 Applications of Inclusion/Exclusion
β€’ Finding a set with none of the properties P1, P2, …, Pn
Example: Find the number of primes less than 40
Example: Find the number of onto functions from
a set of 6 elements to a set of 4 elements.
Find a formula for the number of onto functions from a set
of m elements to a set of n elements where m β‰₯ n.
Related documents