Documents
User Generated
Resources
Learning Center

# Recursion _Recursion _Recursion _stop it____

VIEWS: 0 PAGES: 63

• pg 1
```									Lecture 6

Chapter 3. Recursion

1
In functions and data structures
What is recursion?

It is something defined in terms of itself

… but it must have a stopping condition!

… it must “bottom out”

Recursive functions
A recursive function is one that calls itself.

E.g. to calculate N! = N  (N-1)  (N-2)  . . . 1, do the following:

if N = 0 then 1
else N  (N-1)!

We have defined the factorial function ! In terms of itself.
–   Note the factorial function is applied to a smaller number every
time until it is applied to 0.

The factorial function in Java
The factorial function in Java

fact(4) =
The factorial function in Java

fact(4) = 4 * fact(3)
The factorial function in Java

fact(4) = 4 * fact(3)
= 4 * 3 * fact(2)
The factorial function in Java

fact(4) = 4 * fact(3)
= 4 * 3 * fact(2)
= 4 * 3 * 2 * fact(1)
The factorial function in Java

fact(4) = 4 * fact(3)
= 4 * 3 * fact(2)
= 4 * 3 * 2 * fact(1)
= 4 * 3 * 2 * 1 * fact(0)
The factorial function in Java

fact(4) = 4 * fact(3)
= 4 * 3 * fact(2)
= 4 * 3 * 2 * fact(1)
= 4 * 3 * 2 * 1 * fact(0)
=4*3*2*1*1
The factorial function in Java

fact(4) = 4 * fact(3)
= 4 * 3 * fact(2)
= 4 * 3 * 2 * fact(1)
= 4 * 3 * 2 * 1 * fact(0)
=4*3*2*1*1
=4*3*2*1
The factorial function in Java

fact(4) = 4 * fact(3)
= 4 * 3 * fact(2)
= 4 * 3 * 2 * fact(1)
= 4 * 3 * 2 * 1 * fact(0)
=4*3*2*1*1
=4*3*2*1
=4*3*2
The factorial function in Java

fact(4) = 4 * fact(3)
= 4 * 3 * fact(2)
= 4 * 3 * 2 * fact(1)
= 4 * 3 * 2 * 1 * fact(0)
=4*3*2*1*1
=4*3*2*1
=4*3*2
=4*6
The factorial function in Java

fact(4) = 4 * fact(3)
= 4 * 3 * fact(2)
= 4 * 3 * 2 * fact(1)
= 4 * 3 * 2 * 1 * fact(0)
=4*3*2*1*1
=4*3*2*1
=4*3*2
=4*6
= 24
In general…

A recursive function when calling itself makes a clone and calls the
clone with appropriate parameters.

Important: a workable recursive algorithm (function/procedure) must
always:
• Rule 1: reduce size of data set, or the number its working on, each time
it is recursively called and
• Rule 2: provide a stopping case (terminating condition)

• Factorial function is only simple example, but recursion is valuable tool in
more complex algorithms.

• Can always replace recursive algorithm with an iterative one, but often
recursive solution much sleeker

Linear recursion
With linear recursion a method is defined so that it makes at most one
recursive call each time it is invoked.
Useful when we view an algorithmic problem in terms of a first and/or last
element plus a remaining set with same structure as original set.

Example 1 : factorial example
Example 2: summing the elements of an array -

Algorithm LinearSum(A,n):
Input: An integer array A and integer n1, such that A
has at least n elements
Output:The sum of the first n integers in A
if n=1 then
return A[0]
else
return LinearSum(A,n-1)+A[n-1]

This is pseudocode. Used
in GoTa a lot.
Visualizing Recursion
Example recursion trace:

• Recursion trace                                        call
return 4*6 = 24           final answer

• A box for each recursive call             RecursiveFactorial(4)

• An arrow from each caller to                              call                return 3*2 = 6
callee                                       RecursiveFactorial(3)

• An arrow from each callee to                                  call               return 2*1 = 2

caller showing return value                    RecursiveFactorial(2)
call              return 1*1 = 1
RecursiveFactorial(1)

Actually we will remove the blue arrows                                call            return 1
on the right, to make it simpler                        RecursiveFactorial(0)

More examples
Raise m to the power n

n 1             n2
m  m.m
n
 m.m.m          ...
Raise m to the power n

n 1
m  m.m
n

m 1 0

We now have a recursive definition
Example 3: RaisePower

Algorithm RaisePower(m,n):
Input: Integers m,n
Output: value of mn
if n=0 then
return 1
else
return (m*RaisePower(m,n-1))

n 1
m  m.m
n

m 1
0
Example 3: RaisePower

Algorithm RaisePower(m,n):
Input: Integers m,n
Output: value of mn
if n=0 then
return 1
else
return (m*RaisePower(m,n-1))

Does RaisePower satisfy rules 1 and 2? Yes!

n 1
m  m.m
n

m 1
0
Example 3: RaisePower

Algorithm RaisePower(m,n):
Input: Integers m,n
Output: value of mn
if n=0 then
return 1
else
return (m*RaisePower(m,n-1))

Does RaisePower satisfy rules 1 and 2? Yes!

Rule 1 (number function working on is decreased)

n 1
m  m.m
n

m 1
0
Example 3: RaisePower

Algorithm RaisePower(m,n):
Input: Integers m,n
Output: value of mn
if n=0 then
return 1
else
return (m*RaisePower(m,n-1))

Does RaisePower satisfy rules 1 and 2? Yes!

Rule 1 (number function working on is decreased)

Rule 2   ( stopping case)
n 1
m  m.m
n

m 1
0
Recursion trace, m=2, n=4

call

RaisePower(2,4)
return 2*4=8
call
RaisePower(2,3)
return 2*2=4
call

RaisePower(2,2)
return 2*1=2
call

RaisePower(2,1)
return 1
call

RaisePower(2,0)

And in Java…

public static int raisePower(int m, int n){
if (n==0) return 1;
else return(m*raisePower(m,n-1));
}

Another example … has9
has9

Given an integer (base 10) does it contain the digit 9?
has9

Algorithm Has9(n):
Input: Integer n
Output: whether 9 appears in decimal expansion of n
if (n mod 10)=9 then
return true
else
if (n<10) then
return false
else
return Has9(n/10)

has9

Algorithm Has9(n):
Input: Integer n
Output: whether 9 appears in decimal expansion of n
if (n mod 10)=9 then
return true
else
if (n<10) then
return false
else
return Has9(n/10)

Does Has9 satisfy rules 1 and 2? Yes!

has9

Algorithm Has9(n):
Input: Integer n
Output: whether 9 appears in decimal expansion of n
if (n mod 10)=9 then
return true
else
if (n<10) then
return false
else
return Has9(n/10)

Does Has9 satisfy rules 1 and 2? Yes!

Rule 1 (number function working on is decreased)

has9

Algorithm Has9(n):
Input: Integer n
Output: whether 9 appears in decimal expansion of n
if (n mod 10)=9 then
return true
else
if (n<10) then
return false
else
return Has9(n/10)

Does Has9 satisfy rules 1 and 2? Yes!

Rule 1 (number function working on is decreased)
Rule 2   ( stopping case)

has9

Algorithm Has9(n):
Input: Integer n
Output: whether 9 appears in decimal expansion of n
if (n mod 10)=9 then
return true
else
if (n<10) then
return false
else
return Has9(n/10)

Does Has9 satisfy rules 1 and 2? Yes!

Rule 1 (number function working on is decreased)
Rule 2   ( stopping case)

example

reverse
Reverse an array
Reverse an array
Reverse an array
Reverse an array
Example … for you

palindrome
example

Binary search
Binary search
Binary search
Binary search

example

All permutations of a string
perm
perm

Ouch!
example

Arithmetic! (on natural numbers)

Similar to … Church Numerals
Recursive Arithmetic
Recursive Arithmetic
Recursive Arithmetic
Recursive Arithmetic
example

list
list

We have been defining lists in terms of nodes, where a node has

- a head (we call it “element” where we store data)
- a tail (we call it “next” and it points to the next node or null)

This IS inherently recursive
list
list
list
list
list

NOTE: this is NOT destructive! It produces a new list!
Tail recursion

• Recursion is useful tool for designing algorithms with short,
elegant definitions
• But comes at a cost – need to use memory to keep track of the
state of each recursive call
• When memory is of primary concern, useful to be able to derive
non-recursive algorithms from recursive ones.
• Can use a stack data structure to do this (see ADT chapter), but
some cases we can do it more easily and efficiently
i.e. when algorithms use tail recursion
• An algorithm uses tail recursion when recursion is linear and
recursive call is its very last operation