# Lecture 5 - Dynamic Programming

Dynamic programming is a method of constructing a solution step by step. Breaking the computation in steps, but we now have several solutions to sub problems. It’s using saved solutions to solve other sub-problems to eventually come to a solution.

## Applications

• Computational Biology
• Information Theory
• Operations Research

### Well-known algorithms

• Bellman-Ford
• Knapsack solution
• CYK-Algorithm
• A solution for travelling salesperson of $O*(2^n)$

## Examples

### Shortest Path in Layered Digraphs

Because of the structure of the input graph, we can choose to compute layer-by-layer. The numbers in the nodes are the distance from the source. Note that the shortest paths for some vertices aren’t considered because the distance is longer than the current value.

Instead of considering all possible paths, we only extend optimal paths.

### Matrix Chain Product

We have a chain of matrices $A = A_1 \times A_2 \times … A_n$ and we want to compute the product

As an example, let $n = 3, d_0 = 5, d_1 = 50, d_2 = 20, d_3 = 10$

1. $(A_1 \times A_2) \times A_3$ First compute $5 \times 20$, which requires $5 \times 50 \times 20$ or 5000 multiplications
2. Next, try a different method, $50 \times 20 \times 10$ or 10000 scalar multiplications, which is worse

How can we produce this computation?

$M[i,i] = 0 \text{ for all } i \text { with } i \le i \le n$ $M[i,k] = min \{M[i,j] + d_{i-1} d_j d_k + M[j + 1, k]\} \text{ for all } i,k \text { with } 1 \le i \lt k \le n$

Pseudocode:

input n > 0 // Number of matrices
output M[1,n] // Computed minimum number of scalar operations
begin
for i <- 1 to n do M[i,i] <- 0; // Initialise the matrix diagonals to 0
for b <- 1 to n - 1 do // b is the difference between k and i
for i <- 1 to n - b do
k <- i + b;
m <- d[i - 1] * d[k];
M[i,k] <- inf;
for j <-i to k - 1 do
N <- M[i,j] + m * d[j] + M [j + 1, k];
M[i,k] <- min(M[i,k], N) // If this distance is shorter, replace
// (min just picks the smallest between)


## Shortest Paths Algorithms

Many different types, important ones are

• Floyd-Warshall Algorithm
• Johnson’s Algorithm

These are both dynamic programming-based