60
A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say “Recursion Rocks My World!”

A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Embed Size (px)

Citation preview

Page 1: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

A QuestionHow many people here don’t like recursion?

Why not?

A Promise: By the end of this lecture, you will say:

“Recursion Rocks My World!”

Page 2: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

RecursionRecursion

Let’s say you place two rabbits in a hutch.

What’s going to happen?

Page 3: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

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

Page 4: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

1 month old 0 months old

The next month, you get another pair . . .

The rabbits keep at it.

Page 5: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

SupposeWhat 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?

1111

2222

3333

Page 6: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Hare Raising Story

Start

End Month 5

End Month 1

End Month 2

End Month 3

End Month 4

= one m/f pair

KEY

Page 7: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

PairsPairs 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 apattern

yet?

Page 8: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Let’s Take Another ExampleInstead 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?

Page 9: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

1

Page 10: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

1

Page 11: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

2

Page 12: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

3

Page 13: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

5

Page 14: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

8

Page 15: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

13

Page 16: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

21

Page 17: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

11

2

3

58

13

21

Page 18: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Does this look familiar?

Page 19: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

It’s not just about rabbits.

Page 20: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

The TruthThe Truthis Out Thereis Out There

Page 21: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

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 FibonacciSequence.

Page 22: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Writing the FormulaGiven:

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)

1+ 1-5 5

2 2-

n n

Fib(n) =

But let’s be honest. We’re not as smart as him.

But that’s OK. We can code.

Page 23: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

What If You Can’t Find the Formula?

Suppose you didn’t know:

1+ 1-5 5

2 2-

n n

Fib(n) =

You could take a Math course or you could instead manage with:

Fib(n) = Fib(n-1) + Fib(n-2),Fib(0) = 0;Fib(1) = 1;

(The value at any given place is the sum of the two prior values.)

Which onewould yourrather codeand debug?

For someproblems,there mightnot exist aformula, andrecursion isyour onlyoption.

Page 24: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Recursive Fibonacci

Fib(n) = Fib(n-1) + Fib(n-2),Fib(0) = 0;Fib(1) = 1;

We have our general rule:

We can say a few things about it:

It’s defined recursively (duh).

It has a terminal condition (AHA!)

It can be determined and calculated (addition).

1

2

3

Page 25: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Coding Fibonacci

int fib (int num) {

What do we know to start with? We know thatwe need a function that

return the Fibonaccivalue for a number at a

given position.

This suggests a functionthat gives and gets an int

Page 26: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Coding Fibonacci

int fib (int num) {if (num == 0) return 0;

What’s the FIRST thing we dowith a recursive method?

We plan on how it will terminate!

We know one special casefor the Fibonacci sequence:

F(0) = 0

Page 27: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Coding Fibonacci

int fib (int num) {if (num == 0) return 0;else if (num == 1) return 1;

We also know asecond special casethat could terminate

our recursion:

F(1) = 1.

Page 28: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Coding Fibonacci

int fib (int num) {if (num == 0) return 0;else if (num == 1) return 1;else return fib(num-1) + fib(num-2);

}

The last part of ourformula is merely:

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

Page 29: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Coding Fibonacci

int fib (int num) {if (num == 0) return 0;else if (num == 1) return 1;else return fib(num-1) + fib(num-2);

}

Is this safe? What if someonepassed in 0 to our method?

What happens?

What if they passed in 1?

Page 30: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Coding Fibonacci

int fib (int num) {if (num == 0) return 0;else if (num == 1) return 1;else return fib(num-1) + fib(num-2);

}

void main(String[] args) {for (int i=0; i < 10; i++) printf(“fib(%d)=%d\n”,i,fib(i));

}It is our responsibilityto write a main to test

this method.

Page 31: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Coding Fibonacci

int fib (int num) {if (num == 0) return 0;else if (num == 1) return 1;else return fib(num-1) + fib(num-2);

}

void main(String[] args) {for (int i=0; i < 10; i++) printf(“fib(%d)=%d\n”,i,fib(i));

}

Are we done?What about negative

numbers? More work is needed

Page 32: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Recursion ReviewSo far, we’ve seen that for recursive behavior:

1) Recursion exists in all of nature.

2) It’s easiereasier than memorizing a formula. Not every problem has a formula, but every problem can be expressed as a series of small, repeated steps.

3) Each step in a recursive process should be small, calculable, etc.

4) You absolutely need a terminating condition.

Page 33: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Honesty in Computer Science1. To make life easy the typical examples given for

recursion are factorial and the Fibonacci numbers.

2. Truth is the Fibonacci is a horror when calculated using “normal” recursion and there’s not really any big advantage for factorial.

3. So why all the fuss about recursion?

4. Recursion is absolutely great when used to write algorithms for recursively defined data structures like binary trees. Much easier than iteration!

5. Recursion is excellent for any divide & conquer algorithm like...

Page 34: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

One More ExampleSuppose we wanted to create a method that solve:

Pow(x, y) = xy

In other words, the method returned the value of one number raised to the power of another:

double pow(double value, int exponent);

Page 35: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Planning the MethodUnlike the Fibonacci example, our mathematical formula is not the complete answer.

Pow(x, y) = xy

We’re missing some termination conditions.

But we know:

x1 = x;

x0 = 1;

So we could use these as our terminating condition.

Page 36: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Attempt #1double pow(double value, int exponent){ if (exponent == 0) return 1D;

Always, always start withsome sort of terminating

condition. We know any numberraised to the zero power is one.

Page 37: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Attempt #1double pow(double value, int exponent){ if (exponent == 0) return 1D; else if (exponent == 1) return value;

... and any number raised to thepower of one is itself.

Page 38: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Attempt #1double pow(double value, int exponent){ if (exponent == 0) return 1D; else if (exponent == 1) return value; else return value * pow (value, exponent--);}

For all other values, we canreturn the number times the

recursive call, using our exponentas a counter. Thus, we calculate:

26 = 2*2*2*2*2*2

Page 39: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Attempt #1double pow(double value, int exponent){ if (exponent == 0) return 1D; else if (exponent == 1) return value; else return value * pow (value, exponent--);}

When we run this, however, badthings happen. The program

crashes, having caused a stack overflow. How can we solve this?

Page 40: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Attempt #1double pow(double value, int exponent){if (exponent == 0) return 1D; else if (exponent == 1) return value; else { return value * pow (value, exponent--); }}

Our debug statement tells us that the exponent is never being decreased.Evidently, the “exponent--” line is

not being evaluated before the recursivecall takes place. As it turns out, the

post-decrement operator -- is the problem.

DOH!

DOH!

Page 41: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Attempt #1double pow(double value, int exponent){ if (exponent == 0) return 1D; else if (exponent == 1) return value; else { exponent = exponent - 1; return value * pow (value, exponent); }}

We decide that typing one extraline takes less time than debugging

such a subtle error. Things areworking now.

Page 42: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

“Do I Have to Use Recursion?”double pow(double value, int exponent){ if (exponent == 0) return 1D; else if (exponent == 1) return value; else { exponent = exponent - 1; return value * pow (value, exponent); }}

How many would have preferred to do this with a “for loop” structure or some other iterative solution?

How many think we can make our recursive method even faster than iteration?

Page 43: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Nota BeneOur power function works through brute force recursion.

28 = 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2

But we can rewrite this brute force solution into two equal halves:

28 = 24 * 24

and24 = 22 * 22

and22 = 21 * 21

andanything to the power 1 is itself!

Page 44: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

And here's the cool part...

28 = 24 * 24

Since these are the same we don't have to calculate them both!

Page 45: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

AHA!

So the trick is knowing that 28 can be solved by dividing the problem in half and using the result twice!

So only THREE multiplication operations have to take place:

28 = 24 * 24

24 = 22 * 22

22 = 21 * 21

Page 46: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

"But wait," I hear you say!

You picked an even power of 2. What about our friends the odd numbers?

Okay we can do odds like this:

2odd = 2 * 2 (odd-1)

Page 47: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

"But wait," I hear you say!

You picked a power of 2. That's a no brainer!

Okay how about 221

221 = 2 * 220 (The odd number trick)

220 = 210 * 210

210 = 25 * 25

25 = 2 * 24

24 = 22 * 22

22 = 21 * 21

Page 48: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

"But wait," I hear you say!

You picked a power of 2. That's a no brainer!

Okay how about 221

221 = 2 * 220 (The odd number trick)

220 = 210 * 210

210 = 25 * 25

25 = 2 * 24

24 = 22 * 22

22 = 21 * 21

That's 6 multiplications instead of 20 and it getsmore dramatic as the exponent increases

Page 49: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

The Recursive InsightIf the exponent is even, we can divide and conquer so it can be solved in halves.

If the exponent is odd, we can subtract one, remembering to multiply the end result one last time.

We begin to develop a formula:

Pow(x, e) = 1, where e == 0 Pow(x, e) = x, where e == 1 Pow(x, e) = Pow(x, e/2) * Pow(x,e/2), where e is even Pow(x, e) = x * Pow(x, e-1), where e > 1, and is odd

Page 50: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Solution #2double pow (double value, int exponent){ if (exponent == 0) return 1D; else if (exponent == 1) return value;

}

We have the same basetermination conditions

as before, right?

Page 51: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Solution #2double pow (double value, int exponent){ if (exponent == 0) return 1D; else if (exponent == 1) return value; else if (exponent % 2 == 0) {

}

This little gem determines if a number is odd or even.

Page 52: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Solution #2double pow (double value, int exponent){ if (exponent == 0) return 1D; else if (exponent == 1) return value; else if (exponent % 2 == 0) { exponent = exponent / 2;

}

We next divide the exponent in half.

Page 53: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Solution #2double pow (double value, int exponent){ if (exponent == 0) return 1D; else if (exponent == 1) return value; else if (exponent % 2 == 0) { exponent = exponent / 2; double half = pow (value, exponent);

}

We recurse to find that half of the brute force

multiplication.

Page 54: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Solution #2double pow (double value, int exponent){ if (exponent == 0) return 1D; else if (exponent == 1) return value; else if (exponent % 2 == 0) { exponent = exponent / 2; double half = pow (value, exponent); return half * half;

}

And return the twohalves of the equation

multiplied by themselves

Page 55: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Solution #2double pow (double value, int exponent){ if (exponent == 0) return 1D; else if (exponent == 1) return value; else if (exponent % 2 == 0) { exponent = exponent / 2; double half = pow (value, exponent); return half * half; } else { exponent = exponent - 1;

}

If the exponent is odd,we have to reduce it

by one . . .

Page 56: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Solution #2double pow (double value, int exponent){ if (exponent == 0) return 1D; else if (exponent == 1) return value; else if (exponent % 2 == 0) { exponent = exponent / 2; int half = pow (value, exponent); return half * half; } else { exponent = exponent - 1; double oneless = pow (value, exponent);

}

And now the exponent iseven, so we can just

recurse to solve that portionof the equation.

Page 57: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Solution #2double pow (double value, int exponent){ if (exponent == 0) return 1D; else if (exponent == 1) return value; else if (exponent % 2 == 0) { exponent = exponent / 2; int half = pow (value, exponent); return half * half; } else { exponent = exponent - 1; double oneless = pow (value, exponent); return oneless * value; }} We remember to multiply the value

returned by the original value, since we reduced the exponent by one.

Page 58: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Recursion vs. Iteration:Those of you who voted for an iterative solution are likely going to produce:

O(N)

In a Dickensian world, you would be fired for this.

While those of you who stuck it out with recursion are now looking at:

O(log2n)

For that, you deserve a raise.

Page 59: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”

Questions?

Page 60: A Question How many people here don’t like recursion? Why not? A Promise: By the end of this lecture, you will say: “Recursion Rocks My World!”