Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well.

Similar presentations


Presentation on theme: "Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well."— Presentation transcript:

1 Algorithmic Recursion

2 Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well as many areas of mathematics. Recursive solutions to problems tend be simple simple eloquent eloquent concise conciseDefinition: A recursive algorithm is one that calls or refers to itself within its algorithmic body.

3 Like other iterative constructs such as the FOR loop and the WHILE loop a particular condition needs to be met that prevents the algorithm from executing indefinitely. In recursive algorithms this condition is called the base or end case. The base case usually represents a lower bound on the number of times the recursive algorithm will call itself.

4 A recursive algorithm normally expects INPUT parameters and returns a single RETURN value. Each subsequent recursive call of the algorithm should pass INPUT parameters that are closer to the base case

5 Recursion Example The Fibonacci Sequence is powerful integer number sequence which has many applications in mathematics, computer science and even nature ( the arrangements of flower petals, spirals in pine cones all follow the Fibonacci sequence ). The Fibonacci sequence is defined as follows: Fib(n) = Fib(n-1) + Fib(n-2) where N > 1 Fib(0) = Fib(1) = 1 Fib(n) = Fib(n-1) + Fib(n-2) where N > 1 Fib(0) = Fib(1) = 1 The first ten terms in the sequence are 1,1,2,3,5,8,13,21,33,54 1,1,2,3,5,8,13,21,33,54

6 {Module to compute the n th term in the Fibonacci sequence} Module Name: Fib Input: n Output: n th term in sequence Process: IF (n<=1) {base condition} IF (n<=1) {base condition} THEN THEN RETURN 1 RETURN 1 ELSE {recursive call} ELSE {recursive call} RETURN Fib(n-1) + Fib(n-2) RETURN Fib(n-1) + Fib(n-2) ENDIF ENDIF Fibonacci Sequence Module

7 Compute fib_5 Fib(5) Fib(5) Fib(5) Fib(4) + Fib(3) Fib(4) + Fib(3) Fib(3) + Fib(2) Fib(2) + Fib(1) Fib(3) + Fib(2) Fib(2) + Fib(1) Fib(2) + Fib(1) Fib(1) + Fib(0) Fib(1) + Fib(0) Fib(2) + Fib(1) Fib(1) + Fib(0) Fib(1) + Fib(0) Fib(1) + Fib(0) = 1+1+1+1+1+1+1+1 = 8

8 When looking at recursive solution to a problem we try to identify two main characteristics: 1. Can we identify a solution where the solution is based upon solving a simpler problem of itself each time. 2. Can we identify a base case where the simpler solutions stop.

9 Most recursive modules (functions) take the following form: IF ( base case ) IF ( base case ) THEN THEN RETURN base_case solution RETURN base_case solution ELSE ELSE RETURN recursive solution RETURN recursive solution ENDIF ENDIF

10 Example: Evaluate x y recursively Firstly we need to answer the two questions. (1) Answer yes x y = x * x y-1 (2) Answer yes x 0 = 1

11 { Module to compute x y recursively } Module Name: power Input: x,y Output: x to the power of y Process: IF (y=0) {base condition} IF (y=0) {base condition} THEN THEN RETURN 1 RETURN 1 ELSE {recursive call} ELSE {recursive call} RETURN x*power(x,y-1) RETURN x*power(x,y-1) ENDIF ENDIF x y Recursive Module

12 Example:Triangular values Design an iterative algorithm to read a number from a user and print it’s triangular value. ( if the number 5 is input its triangular value is 5+4+3+2+1 = 15 ) ( if the number 5 is input its triangular value is 5+4+3+2+1 = 15 ) {Print triangular value of input number} PRINT “Enter a number “ READ number triangular_value  0 FOR ( value FROM 1 TO number ) triangular_value  triangular_value + value triangular_value  triangular_value + valueENDFOR PRINT triangular_value PRINT triangular_value

13 Now design a recursive module to solve the same problem. Step 1 – Answer questions (1) Yes - tri(n) = n + tri(n-1) (2) Yes - n = 1 Algorithm: Module Name: tri Inputs: n Outputs: triangular value of n Process:

14 IF ( n = 1 ) THEN/* Base Case */ RETURN 1 ELSE /* Recursive solution */ RETURN n + tri(n-1) ENDIF


Download ppt "Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well."

Similar presentations


Ads by Google