






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Intuitively, a recursive algorithm is one which solves a problem by reducing it to an instance of the same problem, but with smaller input. Like mathematical induction, a recursive algorithm has two components:
Example 1. Write a recursive algorithm for linear search.
Example 2. Write a recursive algorithm for binary search.
Example 3. Write a recursive algorithm for computing an, assuming that n is a nonnegative integer.
Example 5. Give a recursive definition for the operation sr^ which represents the reversal of binary string s.
Trees
A rooted tree T is a triple T = (V, E, r), where V is the set of vertices or nodes of the tree, E is a set of ordered pairs of vertices, called edges, which are of the form (u, v), where u is called the parent of v, and v the child of u; and r ∈ V is called the root of the tree. Moreover, the following recursive definition gives the set of legally defined trees.
T = (V 1 ∪ · · · ∪ Vk ∪ {r}, E 1 ∪ · · · ∪ Ek ∪ {(r, ri)| 1 ≤ i ≤ k}, r)
is a legal tree.
A tree formed via the inductive step with r = root, and k = 10. Each triangle Ti represents a distinct legal tree.
Example 6. Given T = (V, E, r) and v ∈ V , give recursive definitions for the sets ancestor(v) and descendant(v).
Example 7. Given the tree T shown below, answer the following.
Tree Theorems:
Proofs of parts 1) and 2):
Recurrence Relations
The equation fn = fn− 1 +fn− 2 from Example 8 is an example of what is called a recurrence relation. Any sequence which satisifies a recurrence relation is called a solution of the recurrence relation. Moreover, many recurrence relations can be solved, in the sense that a closed-form solution to the relation can be found.
A linear homogeneous recurrence relation of degree k with constant coefficients is a recurrence relation of the form
an = c 1 an− 1 + c 2 an− 2 + · · · + ckan−k,
where c 1 , c 2 ,... , ck are real numbers and ck 6 = 0. Such a relation has a corresponding characteristic equation
rk^ = c 1 rk−^1 + c 2 rk−^2 + · · · + ck.
Solving linear homogeneous recurrence relation of degree k with constant coef- ficients: if the characteristic equation
rk^ = c 1 rk−^1 + c 2 rk−^2 + · · · + ck
has t roots r 1 , r 2 ,... , rt with mulitplicities m 1 , m 2 ,... , mt, respectively, so that m 1 + m 2 + · · · + mt = k, then the sequence an is a solution of the recurrence relation
an = c 1 an− 1 + c 2 an− 2 + · · · + ckan−k
if and only if an = (α 1 , 0 + α 1 , 1 n + · · · + α 1 ,m 1 − 1 nm^1 −^1 )rn 1 +(α 2 , 0 + α 2 , 1 n + · · · + α 2 ,m 2 − 1 nm^2 −^1 )r 2 n
where αi,j are constants for 1 ≤ i ≤ t, and 0 ≤ j ≤ mi − 1.
Example 9. Find a closed-form solution for the Fibonacci sequence using the above result.
A linear nonhomogeneous recurrence relation of degree k with constant coeffi- cients is a recurrence relation of the form
an = c 1 an− 1 + c 2 an− 2 + · · · + ckan−k + F (n),
where c 1 , c 2 ,... , ck are real numbers, ck 6 = 0, and F (n) is some function.
Solving linear nonhomogeneous recurrence relation of degree k with constant coefficients: First solve the corresponding homogeneous relation and add the solution to a particular solution of the nonhomogeneous relation.