recursion - purdue universitycs180/spring2010web/recitation_slides/week12.pdffibonacci numbers can...

46

Upload: others

Post on 06-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this
Page 2: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

RecursionJohn Ross Wallrabenstein

Page 3: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this
Page 4: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Recursion: The Black Magic of Programming

Page 5: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this
Page 6: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Learning the Dark ArtTheory

Definition

Constructing a Recursive Function Definition

Practice

Implementation and Data Structures

Page 7: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Theory

Page 8: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

DefinitionRecursion: See recursion

But I spelled ‘recursion’ correctly...

Page 9: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Formal Definition

Recursive Function:

a function that is defined in terms of itself

Page 10: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Black Magic

You have seen such dark mathemagic before!

Page 11: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

The Fibonacci Numbers

Page 12: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Fibonacci NumbersHow are the Fibonacci Numbers defined?

Fib(0)=0

Fib(1)=1

Fib(2)=1

Fib(3)=2

The Dark Sequence: 0,1,1,2,3,5,8,13,21...

Page 13: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Fibonacci Numbers

Can we generalize this progression?

Fib(n)=?

Page 14: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Fibonacci Numbers

Can we generalize this progression?

Fib(n)=Fib(n-1)+Fib(n-2)

However, I claim that this alone is a poorly cast spell!

Page 15: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Casting Recursive Spells

The novice witch or wizard is overly concerned with the recursive power of their spell and they forget:

A strong base for their spell!

Page 16: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Casting Recursive Spells

A proper recursive spell is composed of:

A series of one or more base cases

A series of one or more recursive spells

Page 17: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Fibonacci NumbersThe Correct Fibonacci Spell

A strong spell base

Fib(0)=0

Fib(1)=1

The recursive spell

Fib(n)=Fib(n-1)+Fib(n-2)

Page 18: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Conjuring Practice

Page 19: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Conjuring PracticeA crafty wizard has proposed the following challenge: “What is the sum of the first n integers greater than zero?”

Unfortunately, your wand does not have the ability to perform loop iterations or multiplication/division, only addition

Assumption: You are not the powerful mathemagician Carl Frederich Gauss

Page 20: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Conjuring Practice

We must divide this challenge up into smaller challenges that we can solve easily:

GaussSum(n)=n+GaussSum(n-1)

Lower your wands! What have we forgotten?

Page 21: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Conjuring Practice

Remember: A powerful recursive spell must have one or more base cases

What is the GaussSum(n) base case?

GaussSum(0)=0

Page 22: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this
Page 23: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Gauss’ SolutionRewrite the problem as follows:

(100+1)+(99+2)+(98+3)+...

Note that each quantity sums to 101, which is n+1

Note that there are 50, or n/2, quantities

In general, GaussSum(n)=(n+1)(n/2)

Page 24: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Practice

Page 25: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

StacksA stack is a “last-in first-out” data structure similar to a stack of papers

i.e. The bottom sheet can only be removed after all sheets above it have been removed

Method calls use stacks with entries called activation records, rather than sheets

Page 26: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Stacks

Consider what happens when a call to MethodB is made from MethodA

The information relevant to MethodA must be saved so that when we return from MethodB, we can resume execution

Page 27: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Stacks

In order to gain insight into how recursion correctly arrives at the solution, we will trace through a sample execution of the GaussSum spell

Page 28: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

GaussSpell Trace

!public static int GaussSum(int n){! if(n == 0)! ! return 0;! return (n + GaussSum(n-1));}

return (n + GaussSum(n-1));

In: n=Q

Page 29: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

return (3 + GaussSum(3-1))

In: n=3return (3 + GaussSum(3-1))

In: n=3return (3 + GaussSum(3-1))

In: n=3return (3 + GaussSum(3-1))

In: n=3

return (2 + GaussSum(2-1))

In: n=2return (2 + GaussSum(2-1))

In: n=2return (2+ GaussSum(2-1))

In: n=2

return (1 + GaussSum(1-1))

In: n=1return (1 + GaussSum(1-1))

In: n=1

return 0

In: n=0

return (3 + GaussSum(3-1))

In: n=3

return (2 + GaussSum(2-1))

In: n=2

return (1 + 0GaussSum(1-1))

In: n=1

return (3 + GaussSum(3-1))

In: n=3

return (2 + 1GaussSum(2-1))

In: n=2

return (3 + 3GaussSum(3-1))

In: n=3

Time

Page 30: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

When Spells Go Wrong

Page 31: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Infinite Recursion

When a recursive spell is cast without properly defined base cases, the recursion continues ad infinitum

Page 32: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Infinite Recursion!

public static int Fib(int n){! fib = Fib(n-1)+Fib(n-2) ! if(n == 0)! ! return 0;! if(n == 1)! ! return 1;! return fib;} What is

wrong?

Stack Overflow

Note: Space Turtles!

Page 33: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Efficiency

Page 34: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Efficiency

A skilled wizard or witch knows that their spell must manifest quickly to defeat their opponent

As fate would have it, both the Fibonacci Sequence and the Gaussian Sum should NEVER be computed recursively!

Page 35: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

EfficiencyThe Fibonacci Sequence, in particular, generates too many recursive calls to be useful

Solution?

Iterative Approach

Always consider switching to an iterative solution when recursion is too costly

Page 36: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

When To Use Recursion

When is recursion useful?

Recursive solutions are often more concise

Some problems have natural recursive solutions

Iterative solution would be convoluted

Page 37: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

A Proper Recursion Application

Page 38: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

The Challenge

A crafty wizard has proposed the following challenge: “Given an array of sorted numbers, find the location of element X”

To check if element I = X, you tap I with your wand and yell “Prae Quam”!

Note: “Prae Quam” is Latin for “Comparison”

Page 39: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

The Challenge

However, to defeat the wizard, you must GUARANTEE to use less than N uses of your wand to perform a comparison (lest your magic be exhausted)

How do we win the challenge?

Page 40: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

The Challenge

Key Observation: If X is greater than the middle element of the list, it is in the right half. Otherwise, it is in the left half.

Can anyone think of a recursive solution?

Page 41: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Binary Search

Solution:

Compare X with the middle element of the list

Recursively search the proper half of the list

Page 42: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

Binary Search15

20

35

41

57

63

75

80

85

90

X=63

15

20

35

41

57

63

75

80

85

90

PraeQuam!

PraeQuam!

15

20

35

41

57

63

75

80

85

90

PraeQuam!DONE!

Page 43: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

!

public int BinS(int[] a, int f, int l, int k){ if( f > l) return -1; else{ int mid = ( f + l) / 2; if( key == a[mid] ) return mid; if( key < a[mid] ) return BinS(a, f, mid-1, k); if( key > a[mid] ) return BinS(a, mid+1, l, k);} In O(log n)

Time!

Page 44: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this
Page 45: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this

You have Mastered Recursive Spells

No Infinite Recursion!

Who has actually seen Fantasia?

Page 46: Recursion - Purdue Universitycs180/Spring2010Web/recitation_slides/Week12.pdfFibonacci Numbers Can we generalize this progression? Fib(n)=Fib(n-1)+Fib(n-2) However, I claim that this