Académique Documents
Professionnel Documents
Culture Documents
HO# 49
Slides--Recursive Functions
11/10/08
3S
If x, y S, then x + y S
3S
If x, y S, then x + y S
Nothing else is in S.
CS103A
HO# 49
Slides--Recursive Functions
11/10/08
3S
If x, y S, then x + y S
3S
If x, y S, then x + y S
Prove: If w S, then 3 | w.
Prove: If w S, then 3 | w.
Suppose x, y S, 3 | x, 3 | y, and w = x + y.
Suppose x, y S, 3 | x, 3 | y, and w = x + y.
Then by Theorem 32.1, 3 | w, so P(w) is true.
Thus applying the recursive rule preserves the property,
and all members of S are divisible by 3.
0S
S
Recursive Functions
A recursive function is one that does some of the work in such a
way as to reduce the problem to a simpler one of exactly the same
form. Ultimately the problem becomes so simple that it can be solved
directly.
Since the subproblem is of the same form as the original, the function
can call itself to solve it.
CS103A
HO# 49
Slides--Recursive Functions
11/10/08
Recursive Functions
A recursive function is one that does some of the work in such a
way as to reduce the problem to a simpler one of exactly the same
form. Ultimately the problem becomes so simple that it can be solved
directly.
Pseudocode Example
To print the characters of a string:
function PrintString(string s)
{
Since the subproblem is of the same form as the original, the function
can call itself to solve it.
}
Pseudocode Example
To print the characters of a string:
function PrintString(string s)
{
if (Length(s) = 0) then return
printchar(First(s))
PrintString(Rest(s))
}
Pseudocode Example
What does this do?
base case
do some work
recursive call on simpler problem
function PrintString(string s)
{
if (Length(s) = 0) then return
PrintString(Rest(s))
printchar(First(s))
}
base case
recursive call on simpler problem
do some work
where
Length is a function that returns the length of a string
printchar is a system routine that prints a character
First is a function that returns the first character of a string
Rest is a function that returns a string with the first character
removed
Pseudocode Example
Pseudocode Example
function PrintString(string s)
{
if (Length(s) = 0) then return
printchar(First(s))
PrintString(Rest(s))
printchar(First(s))
}
function PrintString(string s)
{
if (Length(s) = 0) then return
printchar(First(s))
PrintString(Rest(s))
}
base case
do some work
recursive call on simpler problem
do some work
base case
do some work
recursive call on simpler problem
CS103A
HO# 49
Slides--Recursive Functions
11/10/08
Pseudocode Example
To sum the first n integers, add n to the sum of the first n-1 integers.
function Sum(integer n)
{
if (n = 0)
then
return 0
else
return n + Sum(n 1)
Base case
Do some work after
a recursive call
Pseudocode Example
an can be defined by: a0 = 1, an = a (an-1)
function power(real a, non-negative integer n)
{
if (n = 0)
then
else
return 1
Base case
return a power(a, n 1)
Pseudocode Example
Dont do this:
function Sum(integer n)
{
if (n = 0)
then
return 0
else if (n = 1)
then
return 1
else if (n = 2)
then
return 3
else if (n = 3)
then
return 6
else
return n + Sum(n 1)
}
Base case
Another base case
Yet another base case
Still another base case
Do some work after a recursive call
n!
function factorial(n)
{
if (n = 1)
then
return 1
else
return n factorial(n 1)
}
function nfact(n)
{
nf := n
while (n 1)
{
n := n 1
nf := n nf
}
return nf
}
Give iterative and recursive algorithms for finding the nth term of the
sequence defined by: a0 = 1, a1 = 3, a2 = 5, an = an-1 + (an-2 )2 + (an-3 )3.
Which is more efficient?
CS103A
HO# 49
Slides--Recursive Functions
11/10/08
R(7)
R(4)
R(5)
R(6)
low
R(5)
R(4)
R(3)
R(4)
R(3)
R(2)
R(3)
R(2)
R(1)
high
Is the number x in the array ?
R(3)
R(2)
R(3)
R(2)
R(2)
R(1)
R(1)
R(3)
R(2)
R(2)
R(1)
R(1)
R(3)
R(2)
R(1)
R(0)
R(2)
R(1)
R(0)
R(0)
1
2
3
4
5
6
R(a,b)
{
if (a = 0) return 0
...
F(a+b)
...
...
}
F(x)
{
1
...
2 }
R(a,b)
{
if (a = 0) return 0
1
...
2
F(a+b)
3
...
4
...
5
6 }
F(x)
{
...
1
2 }
R(a,b)
{
if (a = 0) return 0
1
...
2
F(a+b)
3
...
4
...
5
6 }
a
b
ret
R(3, 4)
3
4
OS
Stack
PC
R,1
Stack frame
F(x)
{
...
1
2 }
a
b
ret
R(3, 4)
3
4
OS
Stack frame
Stack
PC
R,3
CS103A
HO# 49
Slides--Recursive Functions
1
2
3
4
5
6
R(a,b)
{
if (a = 0) return 0
...
F(a+b)
...
...
}
F(x)
{
...
1
2 }
1
2
3
4
5
6
R(3, 4)
x
ret
7
R,4
Stack frame
a
b
ret
3
4
OS
Stack frame
R(a,b)
{
if (a = 0) return 0
...
F(a+b)
...
...
}
F(x)
{
...
1
2 }
Stack
PC
1
2
3
4
5
6
11/10/08
a
b
ret
R(3, 4)
R(a,b)
{
if (a = 0) return 0
...
F(a+b)
...
...
}
1
2
3
4
5
6
R(a,b)
{
if (a = 0) return 0
...
R(a-1,b)
...
...
}
1
2
3
4
5
6
R(a,b)
{
if (a = 0) return 0
...
R(a-1,b)
...
...
}
Stack frame
Stack
PC
F,1
3
4
OS
R,4
F(x)
{
1
...
2 }
Stack
PC
1
2
3
4
5
6
OS
R(a,b)
{
if (a = 0) return 0
...
R(a-1,b)
...
...
}
a
b
ret
R(3, 4)
3
4
OS
Stack
PC
R,1
a
b
ret
R(3, 4)
3
4
OS
Stack
PC
R,3
CS103A
HO# 49
Slides--Recursive Functions
1
2
3
4
5
6
R(a,b)
{
if (a = 0) return 0
...
R(a-1,b)
...
...
}
R(3, 4)
R(a,b)
{
if (a = 0) return 0
...
R(a-1,b)
...
...
}
R(3, 4)
2
4
R,4
a
b
ret
3
4
OS
R(a,b)
{
if (a = 0) return 0
...
R(a-1,b)
...
...
}
R(3, 4)
a
b
ret
1
4
R,4
a
b
ret
2
4
R,4
a
b
ret
3
4
OS
1
2
3
4
5
6
R(a,b)
{
if (a = 0) return 0
...
R(a-1,b)
...
...
}
R(3, 4)
a
b
ret
1
4
R,4
a
b
ret
2
4
R,4
a
b
ret
3
4
OS
R,4
1
2
3
4
5
6
2
4
R,4
a
b
ret
3
4
OS
R,3
a
b
ret
0
4
R,4
a
b
ret
1
4
R,4
a
b
ret
2
4
R,4
a
b
ret
3
4
OS
Stack
PC
R,1
a
b
ret
Stack
PC
R,1
Stack
PC
R(a,b)
{
if (a = 0) return 0
...
R(a-1,b)
...
...
}
R(3, 4)
Stack
PC
1
2
3
4
5
6
a
b
ret
1
2
3
4
5
6
Stack
PC
1
2
3
4
5
6
11/10/08
R(a,b)
{
if (a = 0) return 0
...
R(a-1,b)
...
...
}
R(3, 4)
R,1
a
b
ret
1
4
R,4
a
b
ret
2
4
R,4
a
b
ret
3
4
OS
Stack
PC
R,6
CS103A
HO# 49
Slides--Recursive Functions
1
2
3
4
5
6
R(a,b)
{
if (a = 0) return 0
...
R(a-1,b)
...
...
}
R(3, 4)
11/10/08
a
b
ret
2
4
R,4
a
b
ret
3
4
OS
1
2
3
4
5
6
a
b
ret
R(3, 4)
Stack
PC
R(a,b)
{
if (a = 0) return 0
...
R(a-1,b)
...
...
}
Stack
PC
R,4
3
4
OS
R,4
Tail Recursion
1
2
3
4
5
6
R(a,b)
{
if (a = 0) return 0
...
R(a-1,b)
...
...
}
1
2
3
4
5
6
R(3, 4)
R(3, 4)
Stack
PC
R(a,b)
{
if (a = 0) return 0
...
...
...
R(a-1,b)
}
Tail Recursion
1
2
3
4
5
6
R(a,b)
{
if (a = 0) return 0
...
...
...
R(a-1,b)
}
R(3, 4)
a
b
ret
1
4
R,6
a
b
ret
2
4
R,6
a
b
ret
3
4
OS
R,6
a
b
ret
1
4
R,6
a
b
ret
2
4
R,6
a
b
ret
3
4
OS
R,1
Tail Recursion
Stack
PC
0
4
R,6
Stack
PC
OS
a
b
ret
1
2
3
4
5
6
R(a,b)
{
if (a = 0) return 0
...
...
...
R(a-1,b)
}
R(3, 4)
a
b
ret
2
4
R,6
a
b
ret
3
4
OS
Stack
PC
R,6
CS103A
HO# 49
Slides--Recursive Functions
11/10/08
Tail Recursion
1
2
3
4
5
6
R(a,b)
{
if (a = 0) return 0
...
...
...
R(a-1,b)
}
1
2
3
4
5
6
a
b
ret
R(3, 4)
3
4
OS
Stack
PC
R(3, 4)
Stack
PC
R,6
Tail Recursion
1
2
3
4
5
6
R(a,b)
{
if (a = 0) return 0
...
...
...
R(a-1,b)
}
OS
Recursive Functions
A recursive function is one that does some of the work in such a
way as to reduce the problem to a simpler one of exactly the same
form. Ultimately the problem becomes so simple that it can be solved
directly.
R(a,b)
{
if (a = 0) return 0
...
...
...
R(a-1,b)
}
Since the subproblem is of the same form as the original, the function
can call itself to solve it.
The following need to be true:
a
b
ret
R(3, 4)
3
4
OS
Stack
PC
R,6