Chapitre 2: Dynamic Programming

Fibonacci numbers
18:21

Definition (Fibonacci numbers)

Exercise

Write a Python code that calculates Fibonacci numbers

Loading

Question

What happens if you calculate the th Fibonacci numbers? What is the running time of the above code?

Memoization
18:21

Definition (Memoization)

Optimization technique used to speed up programs by caching the results of expensive function calls and returning them when the same inputs are encountered again.

Exercise

Write a memoized version of fibonacci(n)

Loading

Question

What's the time complexity?

Memoization in real-life Python
18:21

In practice, there is a decorator that does all the work for you.

Loading

Bottom-up
18:21

Question

With the memoized function, the space complexity in . Can you write an algorithm with space complexity?

Loading

Dynamic programming
18:21

Question

What is dynamic programming?

  • Problem is broken down into overlapping subproblems
  • Results of the subproblems are saved
  • Solve the original problem

Two ways:

  • Top down (recurse + memoize)
  • Bottom up

Rod cutting
18:21

Question (Rod cutting problem)

What is the highest revenue we can get by cutting an feet rod and selling the pieces?

Length123456789
Price15891017172024

Question

What would the runtime be if we brute-force this problem and try all rod-cutting combinations?

Top down solution
18:21

We can decompose our problem as consisting of a first piece of length and a remainder of length . Write a top down implementation without worrying about memoization.

Loading

Question

What's the time complexity? What if we memoize?

Bottom-up version of rod cutting
18:21

Exercise

Write a bottom-up version of cut_rod

Loading

Question

What are the time and space complexities?

Rod cutting: exercises
18:21

Exercises

  1. What if we want to know the actual cuts and not just the value?
  2. What if the process of cutting itself had a cost?

Exercises
18:21

Dynamic programming
18:21

FibonacciRod cutting
Subproblems
Relate
Original
Order
Base cases
Running time

Remark

Longest Common subsequence
18:21

Problem (Longest Common Subsequence)

Given two sequences A and B, find the longest sequence L that's a subsequence of both A and B.

Example

A LCS of "hieroglyphology" and "michaelangelo" is "hello".

Exercise

Determine an LCS of

Exercise

What is the time complexity of the naive brute-force algorithm?

Problem analysis
18:21

Subproblems
C[i,j] denotes the length of LCS of X[:i] and X[:j]
Base cases
C[0,j] = C[i, 0] = 0
Recurrence

Top-down implementation
18:21

Exercise

Write a top-down implementation of LCS

Loading

Towards the bottom-up implementation
18:21

THEIR
0
H
A
B
I
T
BDCABA
0
A
B
C
B
D
A
B

LCS: Python implementation
18:21

Exercise

Below is a bottom-up implementation of LCS. What's its time complexity?

Loading

Parent pointers
18:21

We'll use parent pointers to relate a subproblem to its parent.

THEIR
0
H
A
B
I
T

Exercise

Find the LCS using parent pointers.

BDCABA
0
A
B
C
B
D
A
B

LCS: Python implementation with parent pointers
18:21

Exercise

Change the implementation so it actually computes the LCS and not only its length.

Loading

Longest increasing subsequence
18:21

Problem (Longest increasing Subsequence)

Given a sequence A, find the longest subsequence L of A consisting of increasing numbers.

Example

An LIS of "carbohydrate" would be "abort"

Analysis of the problem
18:21

Longest increasing subsequence
Subproblems
Relate
Original
Order
Base cases
Running time

Exercise

Implement the LIS algorithm in Python.

Knapsack
18:21

Problem

  • objects and a knapsack
  • Item weighs and has value
  • Knapsack has capacity of
  • Goal is to fill knapsack and maximize total value

Example

#valueweight
111
262
3185
4226
5287

If , the optimal choice is to take items 3 and 4.

Analysis of the knapsack problem
18:21

Knapsack
Guess
Subproblems
Relate
Original
Order
Base cases
Running time

Knapsack: top-down implementation
18:21

Exercise

Write a top-down implementation of the Knapsack problem

Loading

Knapsack: towards the bottom-up implementation
18:21

Use the idea of parent pointers to reconstruct the solution.

01234567891011
{}
{1}
{1, 2}
{1, 2, 3}
{1, 2, 3, 4}
{1, 2, 3, 4, 5}
#valueweight
111
262
3185
4226
5287

Bottom-Up implementation of Knapsack
18:21

Exercise

Write a bottom-up implementation of Knapsack. Use parent pointers to construct the original solution.

Loading

Running-time: pseudo-polynomial
18:21

Proposition

Remark

Storing in memory requires bit. Knapsack is therefore exponential in the input size, but polynomial in the input. We say it's a pseudo-polynomial algorithm.

Exercises: Question 4
18:21