Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and...

14
Algorithmic Algorithmic Recursion Recursion

Transcript of Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and...

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

Algorithmic Algorithmic RecursionRecursion

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

RecursionRecursionAlongside the algorithm, recursion is one of the Alongside the algorithm, recursion is one of the

most important and fundamental concepts in most important and fundamental concepts in computer science as well as many areas of computer science as well as many areas of mathematics.mathematics.

Recursive solutions to problems tend beRecursive solutions to problems tend be simplesimple eloquenteloquent conciseconcise

Definition:Definition:

A recursive algorithm is one that calls or A recursive algorithm is one that calls or refers to itself within its algorithmic body.refers to itself within its algorithmic body.

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

Like other iterative constructs such as Like other iterative constructs such as the the FORFOR loop and the loop and the WHILEWHILE loop a loop a particular condition needs to be met particular condition needs to be met that prevents the algorithm from that prevents the algorithm from executing indefinitely.executing indefinitely.

In recursive algorithms this condition is In recursive algorithms this condition is called the called the basebase or or end caseend case..

The base case usually represents a The base case usually represents a lower bound on the number of times lower bound on the number of times the recursive algorithm will call itself.the recursive algorithm will call itself.

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

A recursive algorithm normally expects A recursive algorithm normally expects INPUT parameters and returns a INPUT parameters and returns a single RETURN value.single RETURN value.

Each subsequent recursive call of the Each subsequent recursive call of the algorithm should pass INPUT algorithm should pass INPUT parameters that are closer to the base parameters that are closer to the base casecase

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

Recursion ExampleRecursion ExampleThe The Fibonacci SequenceFibonacci Sequence is powerful integer is powerful integer

number sequence which has many number sequence which has many applications in mathematics, computer applications in mathematics, computer science and even nature ( the science and even nature ( the arrangements of flower petals, spirals in arrangements of flower petals, spirals in pine cones all follow the Fibonacci pine cones all follow the Fibonacci sequence ).sequence ).

The Fibonacci sequence is defined as follows:The Fibonacci sequence is defined as follows:

Fib(n) = Fib(n-1) + Fib(n-2) where N > 1Fib(n) = Fib(n-1) + Fib(n-2) where N > 1 Fib(0) = Fib(0) = Fib(1) = 1Fib(1) = 1

The first ten terms in the sequence are The first ten terms in the sequence are 1,1,2,3,5,8,13,21,33,541,1,2,3,5,8,13,21,33,54

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

{Module to compute the n{Module to compute the nthth term in the term in the Fibonacci sequence}Fibonacci sequence}

Module Name: FibModule Name: FibInput: nInput: nOutput: nOutput: nthth term in sequence term in sequenceProcess:Process: IF (n<=1) {base condition}IF (n<=1) {base condition} THENTHEN RETURN 1RETURN 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

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

ComputeComputefib_5 Fib(5)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)Fib(1) + Fib(0)

= 1+1+1+1+1+1+1+1 = 8= 1+1+1+1+1+1+1+1 = 8

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

When looking at recursive solution When looking at recursive solution to a problem we try to identify to a problem we try to identify two main characteristics:two main characteristics:

1.1. Can we identify a solution where Can we identify a solution where the solution is based upon the solution is based upon solving a simpler problem of solving a simpler problem of itself each time.itself each time.

2.2. Can we identify a base case Can we identify a base case where the simpler solutions where the simpler solutions stop.stop.

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

Most recursive modules (functions) Most recursive modules (functions) take the following form:take the following form:

IF ( base case ) IF ( base case ) THENTHEN

RETURN base_case RETURN base_case solutionsolution

ELSEELSE RETURN recursive RETURN recursive

solutionsolution ENDIFENDIF

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

Example:Example: Evaluate x Evaluate xyy recursively recursively

Firstly we need to answer the two Firstly we need to answer the two questions.questions.

(1)(1) Answer yesAnswer yes

xxyy = x * x = x * xy-1y-1

(2)(2) Answer yesAnswer yes

xx00 = 1 = 1

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

{ Module to compute x{ Module to compute xyy recursively } recursively }Module Name: powerModule Name: powerInput: x,yInput: x,yOutput: x to the power of yOutput: x to the power of yProcess:Process: IF (y=0) {base condition}IF (y=0) {base condition} THENTHEN RETURN 1RETURN 1 ELSE {recursive call}ELSE {recursive call} RETURN x*power(x,y-1)RETURN x*power(x,y-1) ENDIFENDIF

xy Recursive Module

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

Example:Example: Triangular valuesTriangular values

Design an iterative algorithm to read a number from a Design an iterative algorithm to read a number from a user and print it’s triangular value.user and print it’s triangular value.

( ( if the number 5 is input its triangular value is if the number 5 is input its triangular value is 5+4+3+2+1 = 155+4+3+2+1 = 15 ) )

{Print triangular value of input number}{Print triangular value of input number}PRINT “Enter a number “PRINT “Enter a number “READ numberREAD numbertriangular_value triangular_value 0 0FOR ( value FROM 1 TO number )FOR ( value FROM 1 TO number ) triangular_value triangular_value triangular_value + triangular_value +

valuevalueENDFORENDFOR

PRINT triangular_valuePRINT triangular_value

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

Now design a recursive module to solve the same Now design a recursive module to solve the same problem.problem.

Step 1 – Answer questionsStep 1 – Answer questions

(1) Yes - tri(n) = n + tri(n-1)(1) Yes - tri(n) = n + tri(n-1)(2) Yes - n = 1(2) Yes - n = 1

Algorithm:Algorithm:

Module Name: triModule Name: triInputs:Inputs: n nOutputs: triangular value of nOutputs: triangular value of nProcess:Process:

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

IF ( n = 1 )IF ( n = 1 )

THENTHEN /* Base Case *//* Base Case */

RETURN 1 RETURN 1

ELSEELSE /* /* Recursive solution */Recursive solution */

RETURN n + tri(n-1)RETURN n + tri(n-1)

ENDIFENDIF