Top Banner
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis Chapter 13: Recursion
22

Starting Out with Programming Logic & Design Second Edition by Tony Gaddis

Mar 19, 2016

Download

Documents

Dea

Chapter 13: Recursion. Starting Out with Programming Logic & Design Second Edition by Tony Gaddis. Chapter Topics. 13.1 Introduction to Recursion 13.2 Problem Solving with Recursion 13.3 Examples of Recursive Algorithms. 13.1 Introduction to Recursion. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Starting Out with Programming Logic & Design

Second Edition

by Tony Gaddis

Chapter 13:Recursion

Page 2: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-2

Chapter Topics13.1 Introduction to Recursion13.2 Problem Solving with Recursion13.3 Examples of Recursive Algorithms

Page 3: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-3

13.1 Introduction to RecursionA recursive module is a module that calls itself

– When this happens, it becomes like an infinite loop because there may be no way to break out

– Depth of Recursion is the number of times that a module calls itself

– Recursion should be written so that it can eventually break away

• This can be done with an If statement

Page 4: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-4

13.1 Introduction to Recursion

Page 5: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-5

13.2 Problem Solving with RecursionA problem can be solved with recursion if it can

be broken down into successive smaller problems that are identical to the overall problems

– This process is never required, as a loop can do the same thing

– It is generally less efficient to use than loops because it causes more overhead (use of system resources such as memory)

Page 6: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-6

13.2 Problem Solving with RecursionHow it works

– If the problem can be solved now, then the module solves it and ends

– If not, then the module reduces it to a smaller but similar problem and calls itself to solve the smaller problem

– A Base Case is where a problem can be solved without recursion

– A Recursive Case is where recursion is used to solve the problem

Page 7: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-7

13.2 Problem Solving with RecursionUsing recursion to calculate the factorial of a

number– A factorial is defined as n! whereas n is the

number you want to solve– 4! or “four factorial” mean 1*2*3*4 = 24– 5! or “five factorial” means 1*2*3*4*5 = 120– 0! is always 1

• Factorials are often solved using recursion

Page 8: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-8

13.2 Problem Solving with Recursion

Continued…

User enters a number which determines

how many the function calls itself

Page 9: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-9

13.2 Problem Solving with Recursion

n is equal number

Page 10: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-10

13.2 Problem Solving with RecursionInside Program 13-3

– Inside the function, if n is 0, then the function returns a 1, as the problem is solved

– Else, Return n * factorial(n-1) is processed and the function is called again

– While the Else does return a value, it does not do that until the value of factorial(n-1) is solved

Page 11: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-11

13.2 Problem Solving with RecursionFigure 13-4 The value of n and the return value during each call of the function

Page 12: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-12

13.3 Examples of Recursive AlgorithmsSumming a Range of Array Elements with Recursion

Continued…

Page 13: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

13.3 Examples of Recursive Algorithms

13-13

Summing a Range of Array Elements with Recursion

Page 14: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-14

13.3 Examples of Recursive AlgorithmsInside Program 13-4

– start and end represent the array range– Return array[start] + rangeSum(array, start+1), end)

• This continuously returns the value of the first element in the range plus the sum of the rest of the elements in the range

• It only breaks out when start is greater than end• start must be incremented

Page 15: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-15

13.3 Examples of Recursive AlgorithmsThe Fibonacci Series

Continued…

• The Fibonacci sequence (also known as Leonardo Pisano or Fibonacci ) is named for Leonardo Pisano , an

Italian mathematician who lived from 1170 - 1250.

• The Fibonacci sequence was used to illustrate a problem based on a pair of breeding rabbits:

"How many pairs of rabbits will be produced in a year, beginning with a single pair, if in every month each pair

bears a new pair which becomes productive from the second month on?" The result can be expressed

numerically as: 1, 1, 2, 3, 5, 8, 13, 21, 34 ...

Month Fn = Fn-1 + Fn -2 # of Rabbit pairsMonth 1   1Month 2 0 + 1 1Month 3 1 + 1 2Month 4 1 + 2 3Month 5 2 + 3 5Month 6 3 + 5 8Month 7 5 + 8 13Month 8 8 + 13 21Month 9 13 + 21 34

Page 16: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-16

13.3 Examples of Recursive AlgorithmsThe Fibonacci Series

Continued…

Page 17: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-17

13.3 Examples of Recursive AlgorithmsThe Fibonacci Series

Page 18: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-18

13.3 Examples of Recursive AlgorithmsInside Program 13-5

– The Fibonacci numbers are 0,1,1,2,3,5,8,13,21…– After the second number, each number in the

series is the sum of the two previous numbers– The recursive function continuously processes

the calculation until the limit is reached as defined in the for loop in the main module

Page 19: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-19

13.3 Examples of Recursive AlgorithmsAdditional examples that can be solved with

recursion– The Greatest Common Divisor– A Recursive Binary Search– The Towers of Hanoi

Page 20: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-20

13.3 Examples of Recursive AlgorithmsRecursion vs. Looping

– Reasons not to use recursion• They are certainly less efficient than iterative

algorithms because of the overhead• Harder to discern what is going on with recursion

– Why use recursion• The speed and amount of memory available to modern

computers diminishes the overhead factor– The decision is primarily a design choice

Page 21: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

#Program 13‐1 (recursion_demo)

#This program has a recursive function.

def main():

#By passing the argument 5 to the message

#function we are telling it to display the

#message five times.

message(5)

def message(times):

print times

if (times > 0):

print 'This is a recursive function'

message(times - 1)#calls itself minus one time

#Call the main function.

main()

Python Code for Program 13-1

Page 22: Starting Out with Programming Logic & Design   Second Edition by Tony Gaddis

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

#Program 13‐2 (factorial)

# This program uses recursion to calculate

# the factorial of a number.

def main():

# Get a number from the user.

#This is the Python version of Program 13‐3 in the textbook page 108

number = input('Enter a nonnegative integer: ')

# Get the factorial of the number.

fact = factorial(number)

# Display the factorial.

print 'The factorial of', number, 'is', fact

# The factorial function uses recursion to

# calculate the factorial of its argument,

# which is assumed to be nonnegative.

def factorial(num):

if num == 0:

return 1

else:

print num

return num * factorial(num - 1)

# Call the main function.

main()

Python Code for Program 13-2