TECH
Computer Science
Recursion and Induction
4
For advanced algorithm development,
recursion is an essential design technique
•
Recursive Procedures
•
What is a Proof?
•
Induction Proofs
•
Proving Correctness of Procedures
•
Recurrence Equations
•
Recursion Trees
Recurrence Equations vs.
Recursive Procedures
•
Recurrence Equations:
4
defines a function over the natural numbers, say T(n),
in terms of its own value at one or more integers
smaller than n.
4
T(n) is defined inductively.
4
There are base cases to be defined separately.
4
Recurrence equation applies for n larger than
the base cases
•
Recursive Procedures:
4
a procedure calls a
unique
copy of itself
4
converging to a base case (stopping the recursion)
e.g. Fibonacci Function
•
Recurrence Equation: e.g. Fibonacci Function
4
fib(0) = 0; fib(1) = 1;
// base cases
4
fib(n) = fib(n1) + fib(n2)
// all n>2
•
Recursive Procedure:
•
int fib(int n)
4
int f, f1, f2;
4
1. if (n < 2)
4
2.
f = n;
// base cases
4
3. else
4
4.
f1 = fib( n1 );
4
5.
f2 = fib( n2 );
4
6.
f = f1 + f2;
4
7. return f;
The working of recursive procedure
•
a unique copy for each call to itself
4
individual procedure invocation at run time
4
i.e. activation frame
•
e.g. The working of fib(n)
4
main ()
4
int x = fib(3);
Activation Tree
4
Each node corresponds to a different procedure
invocation, just at the point when it is about to return.
4
A preorder traversal visits each activation frame in
order of its creation
Analysis for algorithm without loops
•
In a computation without loops, but possible with
recursive procedure calls:
4
The time that any particular activation frame is on the
top of the frame stack is O(L),
4
where L is the number of lines in the procedure that
contain either a simple statement or a procedure call.
4
The total computation time is
θ
(C),
4
where C is the total number of procedure calls that
occur during the computation.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentDesigning Recursive Procedures
•
// Think Inductively
•
converging to a base case (stopping the recursion)
4
identify some unit of measure (running variable)
4
identify base cases
•
assume p solves all sizes 0 through 100
4
assume p99 solve subproblem all sizes 0 through 99
4
if p detect a case that is not base case it calls p99
•
p99 satisfies:
4
1. The subproblem size is less than p’s problem size
4
2. The subproblem size is not below the base case
4
3. The subproblem satisfies all other preconditions of
p99 (which are the same as the preconditions of p)
Recursive Procedure design e.g.
•
Problem:
4
write a delete(L, x) procedure for a list L
4
which is supposed to delete the first occurrence of x.
4
Possibly x does not occur in L.
•
Strategy:
4
Use recursive Procedure
4
The size of the problem is
the number of elements in list L
4
Use IntList ADT
4
Base cases: ??
4
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 EGGEN,R
 Algorithms, Recursion, Control flow, recursive procedure

Click to edit the document details