chapter 8

22
CHAPTER 8 RECURSION Chapter 9: Recursion 1

Upload: mort

Post on 23-Feb-2016

43 views

Category:

Documents


0 download

DESCRIPTION

CHAPTER 8. RECURSION. Recursion. Concept of recursion A recursive: Benefit and Cost Comparison : Iterative and recursive functions Example : Simple and complex Converting recursive to non-recursive equivalent functions. Simple Recursion. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CHAPTER  8

CHAPTER 8RECURSION

Chapter 9: Recursion 1

Page 2: CHAPTER  8

Concept of recursion A recursive: Benefit and Cost Comparison : Iterative and recursive

functions Example : Simple and complex Converting recursive to non-recursive

equivalent functions

Chapter 9: Recursion 2

Recursion

Page 3: CHAPTER  8

Concept: defining a solution in terms of a simpler version of the solution◦Recursion is often used instead of

iteration C permits a function to call itself. We say that a function is calling itself when

we can find a function call inside the body of the function with the same name as it has.

This function call is invoking itself.

Chapter 9: Recursion 3

Simple Recursion

Page 4: CHAPTER  8

A solution of a problem is a recursive solution if it is expressible in a smaller version of itself and if ultimately a simple non recursive solution can be found.

Hence, a function that call itself directly or indirectly to solve a smaller version of its task until a final call does not require a self call is a recursive function

A recursive technique is an alternative to iteration technique (using loop), a commonly used technique by student due to its simplicity.

Chapter 9: Recursion 4

What is a recursive solution?

Page 5: CHAPTER  8

In order to implement a recursion to solve a problem, you need to consider some of these questions that are significantly useful such as◦ How can your problem being defined in term of smaller

version of itself ?◦ How does each recursive call diminish the size of the

problem?◦ What instance of the problem can serve as the case

base?◦ As the problem size diminishes, will you reach the case

base?

Chapter 9: Recursion 5

A Recursive Solution

Page 6: CHAPTER  8

The Benefits: ◦ often your code will be shorter ◦ it is usually easier to define the solution recursively

and writing the code is just a matter of implementing the definition

◦ more elegant solution◦ you may not need local variables to implement your solution◦ some solutions are only expressible recursively (or at least are only

easily expressible recursively) this is true of some tree and graph operations, and search

problems that require backtracking The Cost/disadvantages:

◦ recursive solutions are often harder to debug◦ recursion requires numerous function calling

this can lead to much poorer run-time performance◦ takes up more memory space (on the run-time stack)◦ in a few cases, solution may be much less efficient than an iterative

solution

A Recursive: Benefit & Cost

Page 7: CHAPTER  8

Factorial: ◦ fact(n): if(n < 2) 1 else n * fact(n – 1)

Fibonacci: ◦ fib(n): if (n < 3) 1 else fib(n – 1) + fib(n – 2)

Finding largest item in an array: ◦ largest(a, n): if (n = = 0) return a[0] else return max(a[n],

largest(a, n-1)) We can also find the largest using a binary search like strategy:

◦ largest(a, low, high): if(low = = high) return a[low] else return max(largest(a, low, (low+high)/2)), largest(a,

(low+high)/2+1, high)) Writing elements backwards

◦ output_backward(S, n): if(n>=0) output_backwards(S, n-1) output character in S at position (n)

A Recursive: Examples of usage

Page 8: CHAPTER  8

As mentioned earlier, we can use a recursion technique as an alternative to the iteration technique.

Assume we want to calculate the summation value of 1 + 2 + 3 + 4 + 5.

We can use iteration technique to accomplish this taskint cal (int n)

{int j=1;

while ( j < n) {sum = sum + j;j++;

} }

Chapter 9: Recursion 8

Comparison: Iterative Functions

Page 9: CHAPTER  8

Alternatively, we can use recursion technique to solve the problem.

int cal( int n){

if (n == 1 )return 1;

elsereturn ( n + cal(n-1));

}

Chapter 9: Recursion 9

Comparison: Recursive Functions

Page 10: CHAPTER  8

Chapter 9: Recursion 10

Comparison: Recursive Functions (con’t)N=4 => 4 + 3 + 2 + 1 = 10

“return ( n + cal(n-1))”

4

4 + cal (3)

3 + cal (2)

2 + cal (1)

1

4

4 + cal (3)

3 + cal (2)

2 + cal (1)

1a) Sequence of recursive calls

1 returned

2 + 1 = 3 is returned

3 + 3 = 6 is returned

4 + 6 = 10 is returned

Final value = 10 is returned

b) Value returned from each recursive call (backtracking)

Page 11: CHAPTER  8

#include <stdio.h>void print_integers(int);

int main( ) {int number;printf(“Enter an integer: “);scanf(“%d”, number);print_integers(number);

}

void print_integers(int n) {

if (n>=1) {printf(“%d\n”, n);print_integers(n-1);

}}

Chapter 9: Recursion 11

A Recursive: Simple example1

Page 12: CHAPTER  8

The function print_integers also has a function call in its body to itself.

The parameter in the statement print_integers(n-1); is 1 less than n, the value in the previous call.

In other words, the problem that the function is expected to solve is smaller or simpler version of the previous problem.

Thus we can say that the problem size is gradually diminishing.

Chapter 9: Recursion 12

A Recursive: Simple example1 (con’t)

Page 13: CHAPTER  8

It is important to understand how a recursive function works. Assuming you have a factorial function as below

int Fact ( int n) {if ( n == 1)

return 1; ielse

return (n * Fact ( n-1)); ii }

Chapter 9: Recursion 13

A Recursive: Simple example2

Page 14: CHAPTER  8

You can find the value of factorial 4 by calling Fact(4), but how does this function works and produce the output of Fact(4).

Fact(4) = ( 4 * Fact(3))= ( 4 * (3 * Fact(2)))= (4 * ( 3 * (2 * Fact(1))))= (4 * (3 * ( 2 * 1)))= (4 * (3 * (2)))= (4 * (6))= 24

Chapter 9: Recursion 14

A Recursive: Simple example2 (con’t)

Page 15: CHAPTER  8

Using graphical representation, this process can be illustrated as below

Chapter 9: Recursion 15

Tracing a Recursion Function

Fact (4)

Fact (4)iii 4 * Fact (3) Fact 3

iii. 3 * Fact (2)

Fact 2iii. 2 * Fact (1)

Fact 2iii. 2 * 1

Fact 3iii. 3 * 2

Fact 1i return 1ii

Fact 4iii 4 * 6

Page 16: CHAPTER  8

Assuming we want to develop a recursive solution to calculate the value of Xn . Firstly, we may need to study the formal notation solution of this problem. We can write the solution of Xn as below

X0 = 1…………………(i) base case statement

Xn = X + Xn-1…………(ii) recursive statement

Statement (i) will serve as our base case statement and the statement (ii) will serve as the recursive statement.

Chapter 9: Recursion 16

A Recursive: Simple example3

Page 17: CHAPTER  8

So by having the definition, we can develop the recursive function as below.

int pow ( int X, int n) {if ( n == 0)

return 1;else

return (X * pow ( X,n-1)); }

Chapter 9: Recursion 17

A Recursive: Simple example3 (con’t)

Page 18: CHAPTER  8

Exercise:◦ Trace example3 using a graphical representation

as discussed in slide no. 10.◦ The initial value of variable X=2 and n=3◦ What is final output returned.

Chapter 9: Recursion 18

A Recursive: Simple example3 (con’t)

Page 19: CHAPTER  8

int fibonacci(int seq_num) {int first = 0, second = 1, count = 3, fibo;if(seq_num == 0)

fibo = first;else if(seq_num == 1)

fibo = second;else

while(count <= seq_num) { fibo = first + second; first = second; second = fibo; count++;}

return fibo;} Chapter 9: Recursion 19

Example 4: Fibonacci numbers – Iteration techniqueFibonacci - a series of numbers created by adding the last two numbers in the series to produce the next number in the series, i.e., 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc.

Page 20: CHAPTER  8

int fibonacci(int seq_num) {if(seq_num == 0)

return 0;else if(seq_num == 1)

return 1;else

return fibonacci(seq_num -1) + fibonacci(seq_num -2);

}

This has simplified the iteration technique in the previous slide

Chapter 9: Recursion 20

Example 4: Fibonacci numbers - Recursive technique

Page 21: CHAPTER  8

(fib 5) (fib 3) (fib 4)

(fib 1) (fib 2) (fib 2) (fib 3)

(fib 0) (fib 1) (fib0)(fib 1) (fib 1) (fib 2)

(fib 0)(fib 1)

Chapter 9: Recursion 21

Tree of sub problems: Fibonacci

Page 22: CHAPTER  8

This chapter has introduced you;◦How to construct simple recursion◦How recursion can simplify the iteration

technique in certain cases◦A few examples on recursion

Fibonacci Factorial Power of

Chapter 9: Recursion 22

SUMMARY