CS2_14_Recursion

CS2_14_Recursion - CS2 Module 14 Category: CS Concepts...

Info iconThis preview shows pages 1–26. Sign up to view the full content.

View Full Document Right Arrow Icon
CS2 Module 14 Category: CS Concepts Topic: Recursion Objectives Head and Tail Recursion
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS 2 Introduction to Object Oriented Programming Module 14 CS Concepts Recursion
Background image of page 2
Recursion Remember Recursion? A programming technique whereby a method calls itself. Still relies on three things: Check for termination Move closer to solution Make Recursive call
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Let’s say you place two rabbits in a hutch. What’s going to happen? Recursion
Background image of page 4
If it takes two months for rabbits to reach maturity, in two months you’ll have one productive pair and one (brand new) non-productive pair. (This assumes all rabbits live up to their reputation.) Two Months Later. .. original rabbits new rabbits
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
1 month old 0 months old The next month, you get another pair . . . The rabbits keep at it.
Background image of page 6
Suppose What if: The rabbits always had two offspring, always male and female; Rabbits always reached maturity in two months; No rabbit dies. How many pairs of rabbits do you have in a year? 1 1 2 2 3 3
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Hare Raising Story Start End Month 5 End Month 1 End Month 2 End Month 3 End Month 4 = one m/f pair KEY
Background image of page 8
Pairs Pairs of Rabbits of Rabbits 1 2 3 4 5 6 7 8 9 10 11 12 Month Productive Non-Productive Total 0 1 1 2 3 5 8 13 21 34 55 89 0 1 1 2 3 5 8 13 21 34 55 1 1 1 2 3 5 8 13 21 34 55 89 144 See a pattern yet?
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Let’s Take Another Example Instead of rabbits, let’s use geometry. Draw a square of size 1. Rotating 90 degrees, add to it a square of size 1. Rotating 90 degrees again, add a square of size 2. Again, rotate and add a square of size 3, and so on. Keep this up for the sequence we noted in the table: 1, 1, 2, 3, 5, 8, 13, 21, . . . , What do you see?
Background image of page 10
1
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
1
Background image of page 12
2
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
3
Background image of page 14
5
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8
Background image of page 16
13
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
21
Background image of page 18
1 1 2 3 5 8 13 21
Background image of page 19

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Does this look familiar?
Background image of page 20
It’s not just about rabbits.
Background image of page 21

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The Truth The Truth is Out There is Out There
Background image of page 22
See the pattern? 1 2 3 4 5 6 7 8 9 10 11 12 Month Productive Non-Productive Total 0 1 1 2 3 5 8 13 21 34 55 89 0 1 1 2 3 5 8 13 21 34 55 1 1 1 2 3 5 8 13 21 34 55 89 144 We used brute force to find the progression: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, . .. , It turns out this pattern is repeated in many places: sea shells, sun flowers, pine cones, the stock market, bee hives, etc. It’s the Fibonacci Sequence.
Background image of page 23

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Writing the Formula Given: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, . .. , Can we write this as a formula for any number, n ? This guy could: This guy could: Jacques Binet (1786-1856) But let’s be honest. We might not be as smart as him. But that’s OK. We can code. - - + = n n n F 2 5 1 2 5 1 5 1
Background image of page 24
What If You Can’t Find the Formula? Suppose you didn’t know: You could take Math 3012 or you could instead manage with: Fib(n) = Fib(n-1) + Fib(n-2), Fib(1) = 1; Fib(2) = 1; (The value at any given place is the sum of the two prior values.) Which one would your rather code and debug? For some problems, there might not exist a formula, and recursion is your only option.
Background image of page 25

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 26
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/23/2012 for the course CS 1371 taught by Professor Stallworth during the Fall '08 term at Georgia Tech.

Page1 / 98

CS2_14_Recursion - CS2 Module 14 Category: CS Concepts...

This preview shows document pages 1 - 26. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online