Transcript Lecture 13

Lecture 12
Oct 13, 2008
Some simple recursive programs
• recursive problem solving, connection to induction
• Some examples involving recursion
Announcements
• module final exam Oct 15 from 10 to 12 noon.
• project # 2 due Oct 20 (next Monday).
Problem 1 – iterative solution
Write a program that takes as input as odd number k
and print the product 1 x 3 x 5 x … k. If the input is NOT
odd, the program should print –1.
Iteration:
int chainProduct(int k) {
if (k % 2 == 0) return –1;
int Result:= 1;
for (j = 3; j <= k; j = j+2) {
Result:= Result * j;
Return Result;
}
Induction and recursion
• These two are related concepts. Induction is a proof
technique, recursion is a related programming concept.
• Induction proof: assume it is true for n – 1, and show it for
n using the assumption.
• recursion: assume you know how to solve the problem
when the input size n – 1, and design a solution for the case
n using the solution for n - 1.
Induction Proof
Example: Prove the formula
1 + 2 + ... + n = n(n+1)/2
induction hypothesis:
1 + 2 + ... + n – 1 = (n – 1)n/2
induction step:
1 + 2 + ... + n = (1 + 2 + ... + n – 1) + n
= (n – 1)n /2 + n
= n ( n +1)/2
and this completes the proof.
Key facts to remember about recursion



If the problem involves only one input parameter n,
then you should determine how to compute f(n)
given the value of f(n – 1). When writing the code,
you can call the function recursively to get the value
of f(n – 1).
Sometimes, f(n) may depend on f(n – 1) as well as f(n
– 2) etc. This is fine, so long as f(n) does not depend
on f(n+1) …
Always provide exit from recursion. (The simplest base
cases such as k = 1 does not need recursion and
should be handled without recursive calls.)
Recursive Solution to the same problem
int chainProduct(int k) {
if (k % 2 == 0) return -1;
else if (k == 1) return 1;
else return k * chainProduct(k-2);
}
Here is the main program:
int main() {
// to compute f(15)
cout << “f(15) is “ << f(15) << endl;
}
Another simple example
The following problem is discussed in Chapter 7 of Weiss.
Compute the sum 1 + 2 + … + n.
Code is as shown:
long s( int n )
{ if( n == 1 ) return 1;
else return s(n-1) + n; }
Recursive evaluation of xn
Write a recursive procedure to compute g(x, n) = xn.
A simple solution:
g(x, n) = 1 if n = 0
x if n = 1
g(x, n – 1) * x else.
This recursive solution is not as efficient as it can be.
We will next see a faster recursive solution.
This is the first example of the power of recursion.
Iteratively implementing this idea harder and trickier.
g(x, n) = 1
x
g(x, n – 1) * x
g(x, n/2)2
if n = 0
if n = 1
if n > 1 and n is odd
if n > 1 and n is even
int power(int x, int n) {
if (n == 0) return 1; else
if (n == 1) return x; else
if (n % 2 == 1) return x * power(x, n-1); else
{
int temp = power(x, n/2);
return temp*temp;
}
}
Prime number testing
Write a program to print all the prime numbers below a
given integer N.
Recall that m is a prime if and only if there is no integer
x, 2 <= x < m such that (m % x == 0).
Write a recursive procedure isDivisible(m, k) that returns
true if there is an integer x, k <= x < m such that
(m % x == 0).
Prime number testing – recursive solution
isDivisible(m, k) returns true if there is an integer x, k <= x < m such
that (m % x == 0).
Question: Given the procedure isDivisible(x, k), how to
write a procedure isPrime(m) that returns true if and
only if m is a prime?
static boolean isPrime(int n) {
return !isDivisible(n, 2);
}
Recursive version of selection sort
Selection Sorting Algorithm:
• During the j-th pass (j = 0, 1, …, n – 2), examine the elements of the
array a[j] , a[j+1], …, a[n-1] and determine the smallest of them.
• Suppose the smallest of a[j] , a[j+1], …, a[n-1] is in position min.
• Swap a[min] and a[j].
Recursive structure of the algorithm:
selection_sort(int[] a, int j) // j-th pass
{
if (j = a.length) return;
// only one element in array
min = smallest of a[j] , a[j+1], …, a[n-1];
swap a[min] and a[j];
selection_sort(a, j+1);
}