Sei sulla pagina 1di 10

CPE222 – Discrete Structures

Lecture Notes

Lecture 9
Iteration and Recursion
Recurrence Relations or Recursively Defined Functions
In mathematics, we can create recursive functions, which
depend on its previous values to create new ones. We often
call these recurrence relations.
We use two steps to define a function with the set of non
negative integers as its domain:
BASIS STEP: Specify the value of the function at zero
RECURSIVE STEP: Give a rule for finding its value at an
integer from its values at smaller integer
For example, we can have function f(x)=2f(x-1), with f(1)=1
If we calculate some of f values, we get 1, 2, 4, 8, 16, ...
- Iteration is act of repeating a process, either to generate an
unbounded sequence of outcomes, or with the aim of approaching a
desired goal, target or result.
- Each repetition of process is also called an "iteration", and the
results of one iteration are used as starting point for next iteration
- Iteration in mathematics may refer to process of iterating a
function i.e. applying a function repeatedly, using the output from
one iteration as the input to the next.
- In iteration, we start with value of function at one or more integers
(base cases), and successively apply recursive function to find values
of the function at successive larger integers. Such a procedure is
called iterative
- Recursion is the process a procedure goes through when one of
steps of the procedure involves invoking the procedure itself.
- A procedure that goes through recursion is said to be 'recursive'.
Example: Fibonacci numbers, f0, f1, f2, . . . , are defined by equations
f0 = 0, base case1
f1 = 1, base case2
fn = fn−1 + fn−2 for n = 2, 3, 4, . . .. Find f5 by iteration method
f0 = 0
f1 = 1
f2=f1+f0=1+0=1
f3=f2+f1=1+1=2
f4=f3+f2=2+1=3
f5=f4+f3=3+2=5

Example Suppose that f is defined recursively by f (0) = 3,


f (n + 1) = 2f (n) + 3 Find f (1), f (2), f (3), and f (4) by iteration method
From the recursive definition it follows that f(0)=3
f (1) = 2f (0) + 3 = 2 · 3 + 3 = 9
f (2) = 2f (1) + 3 = 2 · 9 + 3 = 21
f (3) = 2f (2) + 3 = 2 · 21 + 3 = 45
f (4) = 2f (3) + 3 = 2 · 45 + 3 = 93
Example: Fibonacci numbers, f0, f1, f2, . . . , are defined by equations
f0 = 0, base case1
f1 = 1, base case2
fn = fn−1 + fn−2 for n = 2, 3, 4, . . .. Find f5 by recursion method
f5=f4+f3
= [(f3+f2)] + [(f2+f1)]
= [(f2+f1)+(f1+f0)] + [(f1+f0)+f1]
= [((f1+f0)+f1)+(f1+f0)] + [(f1+f0)+f1]
= [((1+0)+1)+(1+0)] + [(1+0)+1]
= [(1+1)+(1)] + [(1)+1]
= [(2)+(1)] + [2]
= [3] + [2] = 5
Example: factorial function definition
0! = 1
n! = n (n-1)! Find 4! by recursion method
4! = 4 · 3!
We then use recursive step repeatedly to write
3! = 3 · 2!
2! = 2 · 1!
1! = 1 · 0!
Inserting value of 0! = 1, and working back through steps,
1! = 1 · 1 = 1
2! = 2 · 1! = 2 . 1 = 2
3! = 3 · 2! = 3 · 2 = 6
4! = 4 · 3! = 4 · 6 = 24
- Iteration in computing is the technique marking out of a
block of statements within a computer program for a defined
number of repetitions in a loop.
- The block of statements is said to be iterated; a computer
scientist might also refer to that block of statements
as an "iteration".
-Recursion -function calls itself on a smaller version of input
- while using recursion, programmer need to be careful to
define an exit condition from the function, otherwise it will go
into an infinite loop
- In the context of mathematics or computer science, iteration
(along with related technique of recursion) is a standard
building block of algorithms.
Example: From the pseudocode below
int fact (num)
f=1
for i from 1 to num
{
f=f*i
}
return f
What is the value of fact(4), write the iterative steps
num=4
initial f=1
iteration-1 i=1, f=1*1=1
iteration-2 i=2, f=1*2=2
iteration-3 i=3, f=2*3=6
iteration-4 i=4, f=6*4=24
The value 24 is returned for fact(4)
Example: From the pseudocode below
int fact (num)
if num<=1
return 1
else
return num*fact(num-1)
What is the value of fact(4), write the recursive steps

The value 24 is returned for fact(4)


Iteration vs Recursion
- In algorithmic situations, recursion and iteration can be employed to
the same effect.
- The primary difference is that recursion can be employed as a
solution without prior knowledge as to how many times the action
will have to repeat, while a successful iteration requires that for
knowledge.
- Often an iterative approach for the evaluation of a recursively
defined sequence requires much less computation than a procedure
using recursion
- The recursive algorithm require more computation than an iterative
one when a recursively defined function is evaluated.
- It is sometimes preferable to use a recursive procedure even if it is
less efficient than the iterative procedure.
- In particular, this is true when the recursive approach is easily
implemented and the iterative approach is not

Potrebbero piacerti anche