Proofs, Recursion and Analysis of Algorithms

Download Report

Transcript Proofs, Recursion and Analysis of Algorithms

Proofs, Recursion and Analysis of
Algorithms
Mathematical Structures
for Computer Science
Chapter 2.5
Copyright © 2006 W.H. Freeman & Co.
MSCS Slides
Proofs, Recursion and Analysis of Algorithms
Solving Recurrence Relations

Example 29 defined a recurrence relation





S(1) = 2
Eq(1)
S(n) =2S(n – 1) for n >= 2
Eq(2)
We can recognize S as the sequence 2, 4, 8, 16, . . . which can
also be written as S(n) = 2n.
Eq(3)
If we want to compute S(10) we can do so directly using the
second definition, instead of having to compute S(2), S(3), …
Equation 3 is an example of what is called a closedform solution to the recurrence relation.
Finding a closed form-solution is called solving the
recurrence relation.
Section 2.5
Recurrence Relations
1
Expand, guess and verify

One way to solve: so-called “expand, guess, and
verify” approach, where we




“expand” the sequence by generating several terms using
the recurrence relation
“guess” at a closed form solution by looking at the terms
in the sequence and detecting a pattern
“verify” the solution by actually proving that it is correct.
Expand by working backwards: S(n), S(n - 1), S(n - 2),
…, S(1).

Section 2.5
In general there will be k expansions, where k = n – 1.
Recurrence Relations
2
Expand, guess and verify: Example

Show that S(n) = 2n for the following recurrence relation:
S(1) = 1
S(n) = 2S(n-1) for n  2

Expansion: Using the recurrence relation over again everytime

S(n) = 2S(n-1)
 S(n) = 2(2S(n-2)) = 22S(n-2)
 S(n) = 22(2S(n-3)) = 23S(n-3)

Looking at the developing pattern, we guess that after k such
expansions, the equation has the form

S(n) = 2kS(n-k)

This should stop when n-k =1, hence k = n-1,
S(n) = 2n-1S(1)  S(n) = 2.2n-1 = 2n

At this point we have a conjecture, it still must be proven.

Section 2.5
Recurrence Relations
3
Verification Step for Expand, Guess & Verify




Confirm derived closed-form solution by induction on the value
of n. In other words, prove that S(n) = 2.2n-1 = 2n
Basis step, S(l) = 2 = 21. True, by definition.
Now, assume that S(k) = 2k.
Then S(k+1) = 2S(k) (by using the recurrence relation definition)
S(k+1) = 2(2k) (by using the above inductive hypothesis)
 S(k+1) = 2k+1


Section 2.5
This proves that our closed-form solution is correct.
Recurrence Relations
4
Properties of recurrence relations



Section 2.5
A recurrence relation is linear if it can be written as
S(n) = f1(n)S(n-1) + f2(n)S(n-2) + …..+ fk(n)S(n-k) +
g(n)
 The term linear means that each term of the
sequence is defined as a linear function of the
preceding terms; i.e., all the S(i) as shown above
have power 1.
 Example: F(n) = F(n-1) + F(n-2)
In general, the fi’s can be expressions involving n.
The relation is homogeneous if g(n) = 0 for all n.
Recurrence Relations
5
Order of Recurrence Relation

A recurrence relation is said to have constant coefficients if
the f’s are all constants.

Fibonaci relation is homogenous and linear:
• F(n) = F(n-1) + F(n-2)


Non-constant coefficients: T(n) = 2nT(n-1) + 3n2T(n-2)
Order of a relation is defined by the number of previous
terms in a relation for the nth term.

First order: S(n) = 2S(n-1)
• nth term depends only on term n-1

Second order: F(n) = F(n-1) + F(n-2)
• nth term depends only on term n-1 and n-2

Third Order: T(n) = 3nT(n-2) + 2T(n-1) + T(n-3)
• nth term depends only on term n-1 and n-2 and n-3
Section 2.5
Recurrence Relations
6
Solving Recurrence Relations


Section 2.5
Equation 8, page 150 is a formula that can be used to
solve closed-form solutions for linear, first-order
recurrence relations with constant coefficients.
Table 2.6 on page 151 describes the EGV method of
developing a conjectured solution and then explains
how to use the solution formula to verify the
conjecture. (Assuming, of course, that it’s correct.)
Recurrence Relations
7
Properties of recurrence relations

A nonlinear relation is the one that has earlier values in the
definition as powers other than 1.
Example: F(n+1) = 2nF(n-1)(1-F(n-1))

Solutions are quite complex.
Homogenous relation is a relation that has g(n) = 0 for all n
Example: a(n) – a(n-1) = 2n
Inhomogenous relation:
Example: S(n) = 2S(n-1)






Section 2.5
Recurrence Relations
8