Вы находитесь на странице: 1из 24

CHAPTER 5 : RECURSION

5.1 Introduction to Recursion 5.1.1 What is recursion? 5.1.2 Characteristics of the problems that can be solved recursively Application with Recursive & Iterative technique 5.2.1 Factorial l l ti 5 2 1 F t i l calculation 5.2.2 Multiplication series 5.2.3 Fibonacci Series 5.2.4 Quicksort Recursion with Stack
Intersession May 2009 UiTMT (MP, EZ, NIK) 1

5.2

5.3 53

Introduction to Recursion
What is Recursion?
Recursion is a programming approach or technique in to solve complex programming problems. Recursion is defined as a function/method that calls itself directly or indirectly thru another function/method that give the results in recursive calls. Usually, U ll recursive technique i used t solve a complex i h i is d to l l programmable problem such as series in mathematics and sorting problem gp Problem to be solved must have a basic method that will stop the recursive calls so that it will not get into infinite calls of functions/methods functions/methods.
Intersession May 2009 UiTMT (MP, EZ, NIK) 2

Introduction to Recursion (cont.) (cont.)


Characteristics of the problems that can be solve recursively:
Recursive method is a divide and conquer approach Any complex problem is divided into two conceptual pieces: piece that know how to do piece that does not know how to do (divide the piece again into smaller problem) Piece that know how to do is the base case or simplest case that cannot be divided anymore and returns a result Piece that does not know how to do is the smaller than and resemble the original problem and can be divided into smaller piece known as recursive call The calls to the function must be kept in memory until the calls to the base cases are done and return a value so a lot of memory must be allocated for this process at an one time any
Intersession May 2009 UiTMT (MP, EZ, NIK) 3

Recursion Advantages and Disadvantages


Advantages: Simple to write Easy to implement for the problems that is naturally recursive Reduce the use of variables Disadvantages: Not all problems can be solved using recursive technique Not efficient in storage usage Not provided in all programming languages (example: FORTRAN)

Intersession May 2009 UiTMT (MP, EZ, NIK)

Recursion application
Examples of the mathematical problems that can be solved using recursive technique: g q 1. Factorial Calculation 2. Summation 3. 3 Multiplication 4. Fibonacci series F t i l C l l ti Factorial Calculation Popular mathematical calculation using recursive solution g g Factorial for non-negative integer n is written as n! that are composed of the product of the series as : n! = n * (n-1) * (n-2) * (n-3) * . * 1
Intersession May 2009 UiTMT (MP, EZ, NIK) 5

Recursion application (cont.) (cont.)


Example : 0 ! = 1 or 1 ! = 1 4!=4*3*2*1 = 24 Factorial condition: fact (n) = 1 fact (n) = n * fact (n-1) ! To T evaluate the problem : l h bl n=4 fact (4 ) = 4 * fact (3) = 4 * (3 * fact (2)) = 4 * 3 * (2 * fact (1)) = 4 * 3 * 2 * (1) = 24
Intersession May 2009 UiTMT (MP, EZ, NIK) 6

Base case

if n = 0 or n = 1 if n > 1

Recursion application (cont.) (cont.)


To solve iteratively :
public static long factorial (int n) { int fac = 1; for (int j = n; j > = 1; j - -) { fac *= j ; } return fac; } // end factorial

To solve recursively:
public static long factorial (int n) { if (n ==1 || n == 0 ) ( return 1; long result = n * factorial (n - 1) ; return result; } // end factorial
Intersession May 2009 UiTMT (MP, EZ, NIK)

recursive call i ll
7

Recursion application (cont.)


Multiplication Series Multiplication for non-negative integer a and b is written as a * b means accumulate a until b times example: a + a + a + until b times Multiplication Condition: mult (a, b) = 0 (a mult (a, b) = a mult (a, b) = a + mult (a, (b-1)) To evaluate the problem: = mult (6 * 3) = = = = = if b = 0 if b = 1 if b > 1

base case

6 + mult (6 * (3-1)) 6 + mult (6 * 2) 6 + 6 + mult (6 * (2-1)) 6 + 6 + mult (6 * 1) 6+6+6 18


8

Intersession May 2009 UiTMT (MP, EZ, NIK)

Recursion application (cont.)


To l T solve iteratively: l
public static long multiply ( int a, int b ) { int sum =0, n =0; while (n < b ) { sum += a; n++ } return sum; } // end multiply

To solve recursively:
public static long multiply ( int a, int b ) { if (a ==0 || b ==0) return 0; else if (b==1) return a; else return a + multply(a b-1); multply(a,b-1); } // end multiply
Intersession May 2009 UiTMT (MP, EZ, NIK) 9

Recursion application (cont.)


Summation Series Summation for non-negative integer a and b is written as 0 + 1 + 2 + 3 + 4 + .. + n Summation Condition: sum (0) = 0 sum (1) = 1 sum (n) = n + sum (n - 1) To evaluate the problem: sum (5 ) = = = = = =

if b = 0 if b = 1 if b > 1

base case

5+ 5+ 5+ 5+ 5+ 5+

sum (5 - 1) = 5 + mult (4) (4 + sum (4 - 1)) = 5 + 4 + sum (3) 4 + (3 + sum (3 -1)) = 5 + 4 + 3 + sum(2) 4 + 3 + (2 + sum (2 1)) 4 + 3 + 2 + sum(1) 4 + 3 + 2 + (1) = 15
10

Intersession May 2009 UiTMT (MP, EZ, NIK)

Recursion application (cont.)


To l T solve iteratively: l
public static long sum ( int n) { int sum = 0; while (n > 0) { sum += n; n-} return sum; } // end multiply

To l T solve recursively: l
public static long sum ( int n ) { if (n == 0 || n == 1) return n; else if ( n > 1 ) return n + sum ( n 1 ); } // end multiply
Intersession May 2009 UiTMT (MP, EZ, NIK) 11

Recursion application (cont.) (cont.)


Fibonacci Series Fibonnaci series is : ib i i i
Location 0 1 2 3 4 5 6 7 8 n

Series

0, 1,

1,

2,

3, 5 , 8,

13,

21, ., n

Fibonacci series begin with 0 and 1 and has the property that q f p each subsequent Fibonacci number is the sum of the previous two fibonacci number. Fibonacci series at location 6 is 8

Intersession May 2009 UiTMT (MP, EZ, NIK)

12

Recursion application (cont.)


Fibonacci condition:
fibo (0) = 0 fibo (1) = 1 fibo (n) = fibo (n - 1) + fibo (n - 2) if n = 0 if n = 1 if n > 1 base case

To evaluate the problem:


fibo (4) = fibo (4 1) + fib (4 2) = fibo (3) + fib(2) = 2 + 1 = 3 fibo (3) = fib (3 - 1) + fibo (3 2) = fibo (2) + fibo (1) = 1 + 1 = 2

fibo (2) = fibo (2 1) + fibo (2 2) = fibo (1) + fibo (0) = 1 + 0 = 1 fibo (1) = 1 fibo (0) = 0
Intersession May 2009 UiTMT (MP, EZ, NIK) 13

Recursion Function (cont.)


To solve iteratively:
public static l bli t ti long fib(i t n) fib(int ) { int a=0,b=1, sum=0,count=1; if(num==0 || num==1) sum = num; else { while (count!=num) { sum a b; sum=a+b; a=b; b=sum; count++; } } return sum; } // end fibonacci
Intersession May 2009 UiTMT (MP, EZ, NIK) 14

Recursion Function (cont.)


To solve recursively: fibo (0) = 0 fibo (1) = 1 fibo (n) = fibo (n 1) + fibo ( n - 2) public static long fibo (int n) { if (n ==0 || n ==1) return n; else return fib ( - 1) + fib ( - 2) t fibo (n fibo (n 2); } // end fibonacci

Intersession May 2009 UiTMT (MP, EZ, NIK)

15

Recursion application (cont.)


QuickSort One of the fastest sorting algorithm and implemented using recursive f g g p g technique Steps : 1. Choose a pivot : First, Middle or Last element (consistent throughout the process) 2. Create two markers : START (refers to first element) and END (refers th l t element) ( f the last l t) 3. Move START to right as long as data in START <= the pivot 4. Move END to the left as long as data in END is > pivot 5. 5 If START and END stopped before crossing each other swap other, data in START and END. Repeat (1-5) until START and END crosses each other. Swap END element with pivot element

Intersession May 2009 UiTMT (MP, EZ, NIK)

16

Recursion application (cont.)


6. 7. All data < the pivot (except pivot ) will be in left partition and all data > the pivot (except pivot) will be in right partition. Repeat steps 1-7 for both left and right partition until data is sorted.

Data Sequences : a. Best case : data almost sorted b. Medium case : data is random c. Worst case : data is in reversed sequence

Intersession May 2009 UiTMT (MP, EZ, NIK)

17

Recursion application (cont.)


Example :
<= 10 S 10 2 20 S Swap 10 2 0 S 15 1 20 E 30 15 1 0 E 2 20 15 1 0

10, 2, 20, 15, 1, 0, 30 Pivot: first element (10) ( )


> 30 E 30

Intersession May 2009 UiTMT (MP, EZ, NIK)

18

Recursion application (cont.)


<= 10 2 0 15 S Swap 10 2 0 1 S 10 2 0 1 E Swap (pivot value and END value ) 1 2 0 E 10 15 S
19

> 1 E 20 30

15 E 15 S

20

30

20

30

20

30

Intersession May 2009 UiTMT (MP, EZ, NIK)

Recursion application (cont.)


Pivot = 1 Pivot = 15

<= 1 S <= 1 2 S Swap <= 1 0 S 2

> 0 E > 0 E 10 10

<= 15 S <= 15 E 20 S 20

> 30 E > 30

> 2 E 10

<= 15 20

> 30

Swap S ap
Intersession May 2009 UiTMT (MP, EZ, NIK) 20

Recursion application (cont.)


Pivot = 1

<= 1 0 E

> 2 S 10 15 20 30

Swap (pivot value and END value ) Sorted List 0 1 2 10 15 20 30

Intersession May 2009 UiTMT (MP, EZ, NIK)

21

Recursion application (cont.)


Recursive Tree
10 1 0 2 [] [] 15 20 30

Recursive tree can be designed from the pivot value and the partition list. The level of recursive tree can show the level of difficulty in solving the recursive problem. It depends on the choice of pivot element and the sequence of data (best case, average case, worst case) f d t (b t t )
Intersession May 2009 UiTMT (MP, EZ, NIK) 22

Recursion application (cont.)


Quicksort Algorithm using recursion:
public static void QuickSort (List, Start, End) { if (Start < End) { split (List, Start, End, Mid) QuickSort(List, Start, Mid-1); QuickSort(list, Mid+1, End); } } //end QuickSort

Intersession May 2009 UiTMT (MP, EZ, NIK)

23

Relation Between Recursion and Stack


Example : Factorial calculation using stack implementation 5! = 120

n 5 4 3 2 1 0

y 5 * Fac(4) 4 * Fac(3) 3 * Fac(2) F (2) 2 * Fac(1) 1 * Fac(0) Fac(0) y = n * fac(x)

Stack 120 24 6 2 1 1 Fac(n)


24

Intersession May 2009 UiTMT (MP, EZ, NIK)

Вам также может понравиться