Docsity

Lecture Notes on Recursive Algorithms | CECS 328, Study notes of Discrete Structures and Graph Theory

Toggle sidebar
bg1
Lecture 3: Recursive Algorithms
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:
•base case: the case when a problem cannot be reduced any further. In this case the
solution to the problem is hard coded into the program.
•inductive case: the case when a problem can be reduced to the same problem, but
of smaller input. In this case the program calls itself using the smaller input value as
input.
Example 1. Write a recursive algorithm for linear search.
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Lecture Notes on Recursive Algorithms | CECS 328 and more Study notes Discrete Structures and Graph Theory in PDF only on Docsity!

Lecture 3: Recursive Algorithms

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:

  • base case: the case when a problem cannot be reduced any further. In this case the solution to the problem is hard coded into the program.
  • inductive case: the case when a problem can be reduced to the same problem, but of smaller input. In this case the program calls itself using the smaller input value as input.

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.

  • basis step: T = ({r}, ∅, r) is a legal tree. It consists of one root node and zero edges.
  • inductive step: Suppose that T 1 = (V 1 , E 1 , r 1 ),... , Tk = (Vk, Ek, rk) are legal trees, then, if r is a node not occurring in any of the k 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.

  1. What is the size of T?
  2. Find ancestor(o), and descendant(d).
  3. Find depth(p) and the height of T.
  4. Find children(b), and siblings(h).
  5. Give the path from m to n.
  6. Is T full, complete, or perfect?

Tree Theorems:

  1. A tree of size n has n − 1 edges.
  2. Every tree is acyclic and connected.
  3. A perfect m-ary tree of depth k has mk+1^ − 1 nodes.

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

  • · · · + (αt, 0 + αt, 1 n + · · · + αt,mt− 1 nmt−^1 )rtn ,

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.