Docsity

Recursion-Programming For Aeronautical Engineering And Sciences-Lecture Slides, Slides of Aeronautical Engineering

Toggle sidebar
bg1
Recursion
call themselves
base
case) is reached that can be solved
immediately
General algorithm
• if stopping condition then
solve simple problem
else
use recursion to solve smaller problem(s)
combine solutions from smaller problem(s)
• Writing procedures and functions which
•Involves
– Solving large problems
– By breaking them into smaller problems
–Of identical form
• Eventually, a “trivial” problem (the
•Iteration
–Cognitive simple
•Recursion
– Is not as intuitive
– Demanding on machine time and memory
– Sometimes simpler than iteration
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Recursion-Programming For Aeronautical Engineering And Sciences-Lecture Slides and more Slides Aeronautical Engineering in PDF only on Docsity!

Recursion

call themselves

base

case) is reached that can be solved

immediately

General algorithm

  • if stopping condition then solve simple problem else use recursion to solve smaller problem(s) combine solutions from smaller problem(s)

• Writing procedures and functions which

• Involves

  • Solving large problems
  • By breaking them into smaller problems
  • Of identical form

• Eventually, a “trivial” problem (the

• Iteration

  • Cognitive simple

• Recursion

  • Is not as intuitive
  • Demanding on machine time and memory
  • Sometimes simpler than iteration

Guess a number

  • Problem : think of a number in the range 1 to N
  • Reworded :

    • guess again
  • Recursion comes into the “ guess again ” stage - -

Factorial

n,

computes n!

n! = 1 ∗ 2 ∗ ... ∗ (n-1) ∗ n

Receive: n , an integer Precondition: n >= 0 (0! = 1 and 1! = 1) Return: n!

Given a set of N possible numbers to choose from Guess a number from the set

  • If wrong, Continue until the number is guessed successfully

A set of N-1 numbers remains from which to guess This is a smaller version of the same problem

• Write a function that, given

• Example:

• Specification:

Recursion

called self-referential , or recursive.

process:

base case problem whose solution is trivial

Example: The factorial function has two base cases : if n = 0 : n! = 1 if n = 1 : n! = 1

Induction Step

induction step : a means of solving the one or

Example: In the factorial problem, we solve the

problem:

n! = (n-1)! ∗ n

induction step.

A function that is defined in terms of itself is

Recursive functions are designed in a 3-step

  1. Identify a : an instance of the
  2. Identify an non-trivial instances of the problem using more “smaller” instances of the problem.

“big” problem using a “smaller” version of the

  1. Form an algorithm from the base case and

Algorithm

0. Receive N

1. if N > 1

return Factorial(N-1) * N

else

return 1

Ada Code

function return Positive is

begin

if N > 1 then return N * Factorial(N-1); else return 1; end if;

end Factorial;

-- Factorial(N)

Factorial (N : Natural)

-- factorial

Behavior

N (^4) return?

Factorial(4)

N 3 return 6

= 4 * 6

function Factorial (N : Natural) return Positive is begin – factorial if N > 1 then return N * Factorial(N-1); else return 1; end if; end Factorial;

Behavior

Factorial(4) terminates, returning 24 to its caller.

N 4 return 24

Factorial(4)

function Factorial (N : Natural) return Positive is begin – factorial if N > 1 then return N * Factorial(N-1); else return 1; end if; end Factorial;

Factorial(3) terminates, returning 6 to Factorial(4):

For example the exponentiation problem: Given two values x and n , compute xn. Example: 3^3 = 27

usually win, because the overhead of a function call is far more time-consuming than the time to execute a loop.

recursive solution is more efficient than a corresponding loop-based solution.

For example the exponentiation problem: Given two values x and n , compute xn. Example: 3^3 = 27

A Legend

Legend has it that there were three diamond needles

Stacked upon the leftmost needle were 64 golden disks, each a different size, stacked in concentric order:

If we time the for-loop version and the recursive version, the for-loop version will

However, there are problems where the

set into the floor of the temple of Brahma in Hanoi.

Example

stack of six disks from needle 1 to

needle 2.

single move.

the five disks on top of it out of the way.

then need to move the five disks back on top of it to complete the solution.

Example

problem, is to solve the five disk

problem (with a different destination

needle). Here is where recursion comes

in.

• Consider six disks instead of 64

• Suppose the problem is to move the

  • Part of the solution will be to move the bottom disk from needle 1 to needle 2, as a
  • Before we can do that, we need to move
  • After we have moved the large disk, we

• We have the following process:

  • Move the top five disks to needle 3
  • Move the disk on needle 1 to needle 2
  • Move the disks on needle 3 to needle 2

• Notice that part of solving the six disk

Algorithm

  • hanoi(from,to,other,number) number disks from to needle to if number=1 then move the top disk from needle from to needle to else hanoi(from,other,to, number-1) hanoi(from,to,other, 1) hanoi(other,to, from, number-1) end

Analysis

problem, as a function of n be moved. n 1 1 2 3 3 7 4 5 ... i 2 i - 64 2 64 -1 (a big number)

-- move the top -- from needle

Let’s see how many moves it takes to solve this , the number of disks to

Number of disk-moves required

15 31