32
CS 321. Algorithm Analysis & Design Lecture 11 Recursion - Continued. Fibonacci Numbers

11 - 03 Feb - From Recursion to Dynamic Programming

Embed Size (px)

Citation preview

Page 1: 11 - 03 Feb - From Recursion to Dynamic Programming

CS 321. Algorithm Analysis & Design Lecture 11

Recursion - Continued.Fibonacci Numbers

Page 2: 11 - 03 Feb - From Recursion to Dynamic Programming
Page 3: 11 - 03 Feb - From Recursion to Dynamic Programming

(In class we started with a recap of Independent Set, see the previous set of slides.)

Page 4: 11 - 03 Feb - From Recursion to Dynamic Programming
Page 5: 11 - 03 Feb - From Recursion to Dynamic Programming
Page 6: 11 - 03 Feb - From Recursion to Dynamic Programming
Page 7: 11 - 03 Feb - From Recursion to Dynamic Programming

How many ways to tile a corridor of length n?

Page 8: 11 - 03 Feb - From Recursion to Dynamic Programming

How many ways to tile a corridor of length n?

Page 9: 11 - 03 Feb - From Recursion to Dynamic Programming

How many ways to tile a corridor of length n?

Page 10: 11 - 03 Feb - From Recursion to Dynamic Programming

How many ways to tile a corridor of length n?

Page 11: 11 - 03 Feb - From Recursion to Dynamic Programming

How many ways to tile a corridor of length n?

Page 12: 11 - 03 Feb - From Recursion to Dynamic Programming

How many ways to tile a corridor of length n?

Page 13: 11 - 03 Feb - From Recursion to Dynamic Programming

How many ways to tile a corridor of length n?

Page 14: 11 - 03 Feb - From Recursion to Dynamic Programming

What’s the last tile?

Black Red

Page 15: 11 - 03 Feb - From Recursion to Dynamic Programming

Black

Page 16: 11 - 03 Feb - From Recursion to Dynamic Programming
Page 17: 11 - 03 Feb - From Recursion to Dynamic Programming
Page 18: 11 - 03 Feb - From Recursion to Dynamic Programming
Page 19: 11 - 03 Feb - From Recursion to Dynamic Programming
Page 20: 11 - 03 Feb - From Recursion to Dynamic Programming
Page 21: 11 - 03 Feb - From Recursion to Dynamic Programming
Page 22: 11 - 03 Feb - From Recursion to Dynamic Programming
Page 23: 11 - 03 Feb - From Recursion to Dynamic Programming
Page 24: 11 - 03 Feb - From Recursion to Dynamic Programming
Page 25: 11 - 03 Feb - From Recursion to Dynamic Programming
Page 26: 11 - 03 Feb - From Recursion to Dynamic Programming

C(4) = C(3) + C(2)

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

C(1) = 1, C(2) = 2, C(3) = 3

The general recurrence.

Page 27: 11 - 03 Feb - From Recursion to Dynamic Programming

Notice that there is redundancy in the recursion tree for computing C(n).

C(n)

C(n-1) C(n-2)

C(n-3)C(n-2) C(n-4)C(n-3)

[If implemented directly]

Page 28: 11 - 03 Feb - From Recursion to Dynamic Programming

Memoization - The Basic Idea

Page 29: 11 - 03 Feb - From Recursion to Dynamic Programming

Simply store the values when you compute them.

Memoization - The Basic Idea

Page 30: 11 - 03 Feb - From Recursion to Dynamic Programming

Simply store the values when you compute them.

Before delving into a recursive rabbit hole, check if the value is already stored somewhere.

Memoization - The Basic Idea

Page 31: 11 - 03 Feb - From Recursion to Dynamic Programming

Simply store the values when you compute them.

Before delving into a recursive rabbit hole, check if the value is already stored somewhere.

Get into recursion on a need-to basis.

Memoization - The Basic Idea

Page 32: 11 - 03 Feb - From Recursion to Dynamic Programming

Simply store the values when you compute them.

Before delving into a recursive rabbit hole, check if the value is already stored somewhere.

Get into recursion on a need-to basis.

We’re doing “actual work” for only n distinct subproblems. The rest is only table lookups.

Memoization - The Basic Idea