MIS 4395 A Structured Programming Language Ahmed Imran

  • Slides: 19
Download presentation
MIS 4395. A Structured Programming Language Ahmed Imran Kabir Week 10, Recursion Copyright ©

MIS 4395. A Structured Programming Language Ahmed Imran Kabir Week 10, Recursion Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Recursive Function Definition • The adjective "recursive" originates from the Latin verb "recurrere", which

Recursive Function Definition • The adjective "recursive" originates from the Latin verb "recurrere", which means "to run back". And this is what a recursive definition or a recursive function does: It is "running back" or returning to itself. Most people who have done some mathematics, computer science or read a book about programming will have encountered the factorial, which is defined in mathematical terms as • n! = n * (n-1)!, if n > 1 and 0! = 1 • It's used so often as an example for recursion because of its simplicity and clarity. We will come back to it in the following.

Recursive Function Definition (Cont. ) • The possible combinations quickly multiply out to unimaginably

Recursive Function Definition (Cont. ) • The possible combinations quickly multiply out to unimaginably large numbers. Indeed, the repertoire (whole body of item) of sentences is theoretically infinite, because the rules of language use a trick called recursion. A recursive rule allows a phrase to contain an example of itself, as in She thinks that they think that he knows and so on, ad infinitum. And if the number of sentences is infinite, the number of possible thoughts and intentions is infinite too, because virtually every sentence expresses a different thought or intention. "

Recursion Definition • Recursion is a method of programming or coding a problem, in

Recursion Definition • Recursion is a method of programming or coding a problem, in which a function calls itself one or more times in its body. Usually, it is returning the return value of this function call. If a function definition satisfies the condition of recursion, we call this function a recursive function.

 • A recursive function has to fulfil an important condition to be used

• A recursive function has to fulfil an important condition to be used in a program: it has to terminate. A recursive function terminates, if with every recursive call the solution of the problem is downsized and moves towards a base case. A base case is a case, where the problem can be solved without further recursion. A recursion can end up in an infinite loop, if the base case is not met in the calls. • Example: 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1 Replacing the calculated values gives us the following expression 4! = 4 * 3 * 2 * 1 Generally we can say: Recursion in computer science is a method where the solution to a problem is based on solving smaller instances of the same problem.

Recursive functions in Python • Now we come to implement the factorial in Python.

Recursive functions in Python • Now we come to implement the factorial in Python. It's as easy and elegant as the mathematical definition.

Recursive functions in Python (Cont. ) • We can track how the function works

Recursive functions in Python (Cont. ) • We can track how the function works by adding two print() functions to the previous function definition:

 • This Python script outputs the following results:

• This Python script outputs the following results:

Iterative version of the factorial function • Let's have a look at an iterative

Iterative version of the factorial function • Let's have a look at an iterative version of the factorial function.

Iterative version of the factorial function (Cont. ) • It is common practice to

Iterative version of the factorial function (Cont. ) • It is common practice to extend the factorial function for 0 as an argument. It makes sense to define 0! to be 1, because there is exactly one permutation of zero objects, i. e. if nothing is to permute, "everything" is left in place. Another reason is that the number of ways to choose n elements among a set of n is calculated as n! divided by the product of n! and 0!. All we have to do to implement this is to change the condition of the if statement:

Types of Recursion Linear Recursive A linear recursive function is a function that only

Types of Recursion Linear Recursive A linear recursive function is a function that only makes a single call to itself each time the function runs (as opposed to one that would call itself multiple times during its execution). The factorial function is a good example of linear recursion. Tail recursive Tail recursion is a form of linear recursion. In tail recursion, the recursive call is the last thing the function does. Often, the value of the recursive call is returned. As such, tail recursive functions can often be easily implemented in an iterative manner; Binary Recursive Some recursive functions don't just have one call to themself, they have two (or more). Functions with two recursive calls are referred to as binary recursive functions. Exponential recursion An exponential recursive function is one that, if you were to draw out a representation of all the function calls, would have an exponential number of calls in relation to the size of the data set (exponential meaning if there were n elements, there would be (an) function calls where a is a positive number).

Types of Recursion (Cont. ) • Nested Recursion In nested recursion, one of the

Types of Recursion (Cont. ) • Nested Recursion In nested recursion, one of the arguments to the recursive function is the recursive function itself! These functions tend to grow extremely fast. A good example is the classic mathematical function, "Ackerman's function. It grows very quickly (even for small values of x and y) • Mutual Recursion A recursive function doesn't necessarily need to call itself. Some recursive functions work in pairs or even larger groups. For example, function A calls function B which calls function C which in turn calls function A. A simple example of mutual recursion is a set of function to determine whether an integer is even or odd.

The Fibonacci Numbers The Fibonacci numbers are the numbers of the following sequence of

The Fibonacci Numbers The Fibonacci numbers are the numbers of the following sequence of integer values: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, . . . The Fibonacci numbers are defined by: Fn = Fn-1 + Fn-2 with F 0 = 0 and F 1 = 1 The Fibonacci sequence is named after the mathematician Leonardo of Pisa, who is better known as Fibonacci. In his book "Liber Abaci" (publishes 1202) he introduced the sequence as an exercise dealing with bunnies. His sequence of the Fibonacci numbers begins with F 1 = 1, while in modern mathematics the sequence starts with F 0 = 0. But this has no effect on the other members of the sequence.

The Fibonacci Number (Cont. ) • • • The Fibonacci numbers are the result

The Fibonacci Number (Cont. ) • • • The Fibonacci numbers are the result of an artificial rabbit population, satisfying the following conditions: a newly born pair of rabbits, one male, one female, build the initial population these rabbits are able to mate at the age of one month so that at the end of its second month a female can bring forth another pair of rabbits these rabbits are immortal a mating pair always produces one new pair (one male, one female) every month from the second month onwards The Fibonacci numbers are the numbers of rabbit pairs after n months, i. e. after 10 months we will have F 10 rabits

The Fibonacci Number (Cont. ) • The Fibonacci numbers are easy to write as

The Fibonacci Number (Cont. ) • The Fibonacci numbers are easy to write as a Python function. It's more or less a one to one mapping from the mathematical definition:

Iterative solution

Iterative solution

Recursion tree

Recursion tree

Implementing Memory for Recursive Version

Implementing Memory for Recursive Version

Recursive Algorithm in Fibonacci • We can also define a recursive algorithm for our

Recursive Algorithm in Fibonacci • We can also define a recursive algorithm for our Fibonacci function by using a class with callabe instances, i. e. by using the special method __call__. This way, we will be able to hide the dictionary in an elegant way. We used a general approach which allows as to define also functions similar to Fibonacci, like the Lucas function: