Recurrence and Recursion

advertisement
Recurrence and Recursion
Introduced through Computer
Science
Recursively Defined Sequences
• Three ways to define a sequence:
– Informal: write the first few terms, expecting the
pattern to be obvious
– Traditional: write a formula to describe the sequence
– Computer Science: use a recurrence relationship
• Write a formula to relate each future term to a number of
past terms
• Specify initial conditions, the first few values needed to
satisfy the formula
• EX:
is the recurrence relationship and
are the initial conditions required to calculate the sequence
Why Use Recursion?
• Sometimes, you can’t define a sequence with a
formula, but you usually can define it with
recursion
• Defining a sequence with recursion is equivalent
to proving the sequence with induction
• Recursion is a fundamental programming
technique because it breaks large tasks into their
smallest possible chunks
– This translates to highly efficient code, if done
correctly
Computing Recursive Terms
• Recurrence relation: for all integers k ≥ 2
–
, initial conditions:
– Find C[2], C[3], C[4]
• C[2]=C[1]+2*C[0]+ 1
– C[2] = 2 + 2*1 + 1 = 5
• C[3] = C[2] + 3*C[1] + 1
– C[3] = 5 + 3*2 + 1 = 12
• C[4] = C[3] + 4*C[2] + 1
– C[4] = 12 + 4*5 + 1 = 33
Re-Writing a Recurrence Relationship
• Just like sums and products, variables in a recurrence
formula can be expressed in multiple ways
–
–
–
–
For all integers k ≥ 1,
For all integers k ≥ 0,
Both of these relationships say the same thing
Note that the recurrence relationship does not define any
specific sequence. Rather, it describes the patterns to
expect in a sequence with given initial conditions
– Also, a recursive sequence doesn’t have to start at element
0. Initial conditions may be placed at any index value
Showing an Explicit Formula Satisfies A
Recursive Formula
• Show that the sequence
for n ≥ 0 satisfies the
relationship
for all k ≥ 1
– Based on the formula, S[0] = 1
– S[k] and S[k-1] are defined in the recursion, so substitute
both into the explicit formula
• S[k] = (-1)^k * k!
• S[k-1] = (-1)^(k-1) * (k-1)!
– (-k)*S[k-1] = (-k)*{(-1)^(k-1)*(k-1)!} – By substitution from
recurrence formula
• (-1)(-1)^(k-1) * k(k-1)! – By simplification
• (-1)^(k) * K! – By basic algebra (EX: law of exponents)
• Final: (-k)*S[k-1] = S[k] – By substitution from explicit formula
Fibonacci Numbers
• A single pair of rabbits is born at the beginning of
the year. Suppose the following:
– Rabbit pairs are infertile for one month after birth, but
then give birth to one new male/female pair each
month
– No rabbits die
• How many rabbits will there be at the end of the
year?
• Try to solve this yourself first. Then check out the
program
Solution
• For every integer n ≥ 1, let
– F[n] = [number of rabbit pairs alive at end of month n]
– F[0] = the initial number of pairs = 1
• Note that the number of pairs born in month n equals the number of pairs
alive in month n-2.
– F[k] = F[k-1] + F[k-2]
• F[1] is also 1 because the first pair is not fertile until the second month
– Calculation:
•
•
•
•
•
•
•
F[2] = F[1] + F[0] = 1 + 1 = 2
F[3] = F[2] + F[1] = 2 + 1 = 3
F[4] = F[3] + F[2] = 3 + 2 = 5
F[5] = F[4] + F[3] = 5 + 3 = 8
….
F[12] = F[11] + F[10] = 144 + 89 = 233
This is the number of pairs. Double it to get 466 rabbits in all
Solving Recurrence Relationships by
Iteration
• Given a sequence that satisfies a recurrence
relationship with initial conditions, there may
exist a formula that can represent that
sequence
– This is sometimes very useful
• EX: if you have terms 1-10 of the sequence, but you
need terms 10,000-100,000, you would have to iterate
the recurrence relation many times
• With a formula, known as a solution, you could simply
plug in a value without having to iterate the relation to
obtain a result
Method of Iteration
• This is the most simple method
– Start with the relation and the initial conditions
and iterate it until you see a pattern in the
sequence produced. Based on the pattern, guess
the formula
• EX: for the relation “a[k] = a[k-1] + 2” and the condition
a[0] = 1, find the formula
• Start with a few terms and substitute numerical
expressions into each term instead of numbers
– a[1] = a[0] + 2 = 1 + 2
– a[2] = a[1] + 2 = (1 + 2) + 2 = 1 + 2 + 2 – eliminate parenthesis
– a[3] = a[2] + 2 = (1 + 2 + 2) +2 = 1+2+2+2
Iteration Continued
– By rewriting 1 + 2 + 2 + 2 as 1 + 3 * 2, and by
noting that 3 is the index of a[3], a pattern can be
observed
• It appears that the recurrence relation with a[0] = 1 is
equivalent to the formula a[n] = 1 + 2n
• Note: this is only a guess. To be absolutely certain, you
must prove the equivalence of the two formulae
Arithmetic Sequences
• A sequence “a[0], a[1], a[2]…” is an arithmetic
sequence iff there is a constant, d, such that:
– a[k] = a[k-1] + d for all integers K ≥ 1
– Or equivalently: a[n] = a[0] + d*n for all integers
n≥0
– In an arithmetic sequence, each term equals the
previous term plus a fixed constant
Geometric Sequences
• A sequence is geometric iff there is a constant,
r, such that:
– a[k] = r * a[k-1] for all integers k ≥ 1
– Or a[n] = a[0] * r^n for all integers n ≥ 0
– In a geometric sequence, each term equals the
previous term times a fixed constant
• Unlike an arithmetic sequence, which is linear, a
geometric sequence is exponential (the formula
contains a variable as an exponent), which implies that
it will change very rapidly
Using Formulas to Simplify Solutions
• The Tower of Hanoi relation is M[k] = 2*m[k-1] +1 for
all integers k ≥ 2. m[1] = 1
– By iteration, we find that:
• m[2] = 2m[1] + 1 = 2*1 + 1 = 2^1 + 1
• m[3] = 2m[2] + 1 = 2(2 + 1) + 1 = 2^2 + 2 + 1
• m[4] = 2m[3] + 1 = 2(2^2 + 2 + 1) = 2^3 + 2^2 + 2 + 1
– It seems that each term, m[k] equals the sum of
successive powers of two, (2^(k-1)+…+2^2+2^1+2^0)
• By the formula for the sum of a geometric sequence,
we
find that the sum equals
• The same process can be used to sum an arithmetic sequence
using it’s respective formula
Checking Correctness with Induction
• It is possible to make mistaken assumptions
when solving for a recurrence relation, so its
correctness must be verified
• First, show the formula holds to the relation for the first
term a[1]
• Second, show that for all integers k ≥ 1, if the formula
holds for n = k in a[n], then it holds for n = k+1
– The format of a proof by induction and the
procedures by which the terms to be proven
equivalent are compared with one another are
detailed in an accompanying module
Discovering that an Explicit Formula is
Incorrect
• Given c[k] = 2c[k-1] + k for all integers k ≥ 1
with c[0] = 1
– Is the formula “c[n] = 2^n + n for all integers n ≥ 0”
correct?
• Basis step: c[0] = 1 and 2^0 + 1 = 1
• Inductive step: c[k] = 2^k + k for some integer k ≥ 0
• We must show: c[k+1] = 2^(k+1) + (k+1)
– c[k+1] = 2c[k] + (K + 1) – By the recurrence relationship
– c[k+1] =2(2^k + k) + (k+1) – We assume c[k] = 2^k + k
– c[k+1] = 2^(k+1) + 3k + 1 – Basic algebra
…Continued
• Now we must show
holds true
– However, this simplifies algebraically to 2*k = 0 meaning that
k=0
• The reason this makes the relation false is deceptively
simple. k is meant to be any non-negative integer. The
fact that it only equals zero means that it cannot satisfy
the sequence defined by the recurrence relationship
An Interesting Case
• If you would like to see some more
complicated techniques, one of which will let
you solve the Fibonacci sequence, check this
out
Other Aspects of Recursion
• Recursive thinking can be applied to sets of
any kind
– There are three formal requirements to apply
recursion
• Base: A statement that certain objects belong to a set
• Recursion: A collection of rules indicating how to form
new set objects from those already in the set
• Restriction: A statement that no other objects are in
the set except for those already defined
A Simple Example
• There are certain combinations of parenthesis
that are illegal, like ()))
– To build a set of parenthesis that contains only legal
combinations, use the following recursive definition
• Base: () is in set P
• Recursion:
– If E is in P, so is (E)
– If E and F are in P, so is EF
• Restriction: Only combinations derived from the given rules
are in P
– By this definition, the combinations (), ()(), (())(), (()()), and others
can be formed, so the set is infinite
– This method is recursive because you start with the initial
condition () and use a well-defined procedure to obtain the set
Proving Properties of Recursive Sets
• A process called structural induction is used to
prove recursively defined sets
– Let S be a recursively defined set with some property
that is to be proven for every element of the set. The
following must be done:
• 1. Show that each object in the base satisfies the property
• 2. Show that for each rule in recursion, if the rule is applied to
an object that satisfies the property, then the object defined by
the rule also satisfies the property
• Because only the objects defined by the base and the rules are
in S, all of the objects in S must satisfy the property
Proving the Set of Legal Parenthesis
• Let the property of the set be “The parenthesis
configuration has an equal number of left and right
parenthesis”
– The Base of P is () which has 1 left and 1 right parenthesis
– Proof of Recursion
• If E is in the set, it satisfies the property, so (E) also must satisfy the
property because 1 left and 1 right parenthesis are added
• If E and F are in the set, then they have n and m pairs of
parenthesis, respectively. Therefore, EF, which is simply E and F
placed next to each other, must have n + m pairs
• Therefore, all of the elements in P satisfy the property
Recursive Sums and Products
• The recursive forms of sums and products are
designed to specify the order in which they are to
be computed
– EX:
• If you began with a value of 4 for n, and you continued to
recursively expand this product form, you would end with
((a[1]*a[2])*a[3])*a[4] which is a precise definition of the
order by which the product is calculated
• This form applies to both sums and products and is the
method of choice when using algorithms to compute them.
It is also used with induction to establish properties of finite
sets
– The following code is used to recursively sum a set
Sum:=0
For k:= 1 to n
sum:=sum + a[k]
Next k
Recursive Unions and Intersections
• Without diving into the definition of unions and
intersections, they can be expanded recursively
in the same way sums and products can be
– EX:
• This definition can also be used to prove properties with
induction and applies to both unions and intersections
• As with recursive sums and products, this form specifies
the order by which unions and intersections are to be
performed, which is necessary to compute them using an
algorithm
Recursive Functions
• A function is recursive if it refers to itself in its
definition
– Consider McCarthy’s 91 function:
for all positive integers n
• Remarkably, all values less than 101 yield 91 after
successive iteration of the function
• For values above 101, the function is still well defined
because n-10 will be calculated
– Another well-defined function is the Ackermann
function, which is famous for its incredible growth
rate
Poorly Defined Recursive Functions
• It is often difficult to define a function recursively.
Consider the following for n ≥ 1
–
– Why is this undefined?
•
•
•
•
G(1) = 1
G(2) = 1 + G(1) = 2
G(3) = G(8) = 1 + G(4) =…=1+(1+2) = 4
G(5) = G(14) = 1+G(7) =…= 3+G(5)
– As you can see, at value 5, you get G(5) = 3 + G(5), so
0 = 3. This means that the expression can’t be logically
defined as a function
• This is clearly undefined, but some recursive functions are
not easily discerned as functions or not
Why Use Recursion?
• Computers can do a lot of nifty things, but they
are not mathematicians (yet!). Computers cannot
look at the sum of a set or casually scan the
definition of a function and simply “jump to” an
answer. Computers operate on a step-by-step
basis. Recursion is one of the most efficient ways
to implement a step-by-step process. One could
easily write thousands of lines of code to sum a
set or calculate a sequence. By using recursion
correctly, you can replace those lines with a
compact, re-useable module
Download