15
Dynamic Programming

Algorithm chapter 8

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Algorithm chapter 8

Dynamic Programming

Page 2: Algorithm chapter 8

Dynamic Programming

Dynamic Programming is a general algorithm design technique. “Programming” here means “planning”.

Invented by American mathematician Richard Bellman in the 1950s to solve optimization problems

Main idea: solve several smaller (overlapping) subproblemsrecord solutions in a table so that each subproblem is only solved

oncefinal state of the table will be (or contain) solution

Dynamic programming vs. divide-and-conquerpartition a problem into overlapping subproblems and

independent onesstore and not store solutions to subproblems

Page 3: Algorithm chapter 8

Example: Fibonacci numbers

• Recall definition of Fibonacci numbers:f(0) = 0f(1) = 1f(n) = f(n-1) + f(n-2)

• Computing the nth Fibonacci number recursively (top-down):

f(n)

f(n-1) + f(n-2)

f(n-2) + f(n-3) f(n-3) + f(n-4)

...

Page 4: Algorithm chapter 8

Example: Fibonacci numbers

Computing the nth fibonacci number using bottom-up iteration:

• f(0) = 0• f(1) = 1• f(2) = 0+1 = 1• f(3) = 1+1 = 2• f(4) = 1+2 = 3• f(5) = 2+3 = 5•••• f(n-2) = • f(n-1) = • f(n) = f(n-1) + f(n-2)

ALGORITHM Fib(n)F[0] 0, F[1] 1for i 2 to n do

F[i] F[i-1] + F[i-2]return F[n]

extra space

Page 5: Algorithm chapter 8

Examples of Dynamic Programming Algorithms

Computing binomial coefficients

Warshall’s algorithm for transitive closure

Floyd’s algorithms for all-pairs shortest paths

Constructing an optimal binary search tree

Some instances of difficult discrete optimization problems:

knapsack

Page 6: Algorithm chapter 8

Computing Binomial Coefficients

A binomial coefficient, denoted C(n, k), is the number of combinations of k elements from an n-element set (0 ≤ k ≤ n).Recurrence relation (a problem 2 overlapping problems)

C(n, k) = C(n-1, k-1) + C(n-1, k), for n > k > 0, and C(n, 0) = C(n, n) = 1

Dynamic programming solution:Record the values of the binomial coefficients in a table of n+1rows and k+1 columns, numbered from 0 to n and 0 to krespectively.

11 11 2 11 3 3 11 4 6 4 11 5 10 10 5 1…

Page 7: Algorithm chapter 8

Transitive ClosureThe transitive closure of a directed graph with n vertices can be

defined as the nxn matrix T = {tij}, in which the element in the ith row (1 ≤ i ≤ n) and the jth column (1 ≤ j ≤ n) is 1 if there exists a nontrivial directed path (i.e., a directed path of a positive length) from the ith vertex to the jth vertex; otherwise, tij is 0.

Graph traversal-based algorithm and Warshall’s algorithm

3

42

13

42

1

0 0 1 01 1 11 1 10 0 0 011 1 1 1

0 0 1 01 0 0 10 0 0 00 1 0 0

Adjacency matrix 1 1Transitive closure

Page 8: Algorithm chapter 8

Warshall’s Algorithm•• Main idea: Use a bottom-up method to construct the transitive closure of a given digraph with n vertices through a series of nxn boolean matrices:

R(0),…, R(k-1), R(k) , …, R(n)

The question is: how to obtain R(k) from R(k-1) ?

3

42

13

42

1

R(0)

0 0 1 01 0 0 10 0 0 00 1 0 0

Does not allow an intermediate node

3

42

13

42

1

R(1)

0 0 1 01 0 11 10 0 0 00 1 0 0

R(2)

0 0 1 01 0 1 10 0 0 01 1 1 1 11 1

R(3)

0 0 1 01 0 1 10 0 0 01 1 1 1

Allow 1,2,3 to be an intermediate node

R(4)

0 0 1 01 11 1 10 0 0 01 1 1 1

Allow 1,2,3,4 to be an intermediate node

3

42

1

R(k) : rij(k) = 1 in R(k) , iff

there is an edge from i to j; orthere is a path from i to j going through vertex 1; orthere is a path from i to j going through vertex 1 and/or 2; or

...there is a path from i to j going through 1, 2, … and/or k

Allow 1 to be an intermediate node

Allow 1,2 to be an intermediate node

Page 9: Algorithm chapter 8

Warshall’s AlgorithmIn theIn the kkthth stage: to determine stage: to determine RR(k(k)) is to determine if a path exists is to determine if a path exists between two vertices between two vertices i, j i, j using just vertices among 1,…,using just vertices among 1,…,k k

rrijij(k(k--1)1) = 1 = 1 (path using just 1 ,…,(path using just 1 ,…,kk--11))

rrijij(k(k)) = 1: or = 1: or

((rrikik(k(k--1)1) = 1 and = 1 and rrkjkj

(k(k--1)1)) = 1 ) = 1 (path from (path from i i to to kkand from and from kk to to iiusing just 1 ,…,using just 1 ,…,kk--1)1)

j

kkth stage

{Rule to determine whether rij

(k) should be 1 in R(k) :

• If an element rij is 1 in R(k-1) , it remains 1 in R(k) .

• If an element rij is 0 in R(k-1) ,it has to be changed to 1 in R(k) iff the element in its row i and column k and the element in its column j and row k are both 1’s in R(k-1).

i

Page 10: Algorithm chapter 8

Floyd’s Algorithm: All pairs shortest paths

All pairs shortest paths problem: In a weighted graph, find shortest paths between every pair of vertices.

Applicable to: undirected and directed weighted graphs; no negative weight.

Same idea as the Warshall’s algorithm : construct solution through series of matrices D(0) , D(1), …, D(n)

Example:3

42

14

16

1

5

3

0 ∞ 4 ∞1 0 6 3 ∞ ∞ 0 ∞∞ 5 1 0

0 ∞ 4 ∞1 0 5 3 ∞ ∞ 0 ∞6 5 1 0

weight matrix distance matrix

dij(k) = length of the

shortest path from i to j with each vertex numbered no higher than k.

Page 11: Algorithm chapter 8

Floyd’s Algorithm

DD(k(k)) : allow 1, 2, …, k to be intermediate vertices.: allow 1, 2, …, k to be intermediate vertices.In theIn the kkthth stage, determine whether the introduction of stage, determine whether the introduction of kk as a new eligible as a new eligible intermediate vertex will bring about a shorter path from i to j.intermediate vertex will bring about a shorter path from i to j.

ddijij(k(k) ) = min{= min{ddijij

(k(k--1) 1) , , ddikik(k(k--1)1) + + ddkjkj

(k(k--11} for } for k k ≥≥ 1, 1, ddijij(0) = (0) = wwijij

i

jdij

(k-1)

dkj(k-1)

kdik(k-1)

kth stage

Page 12: Algorithm chapter 8

General Comments

The crucial step in designing a dynamic programming algorithm:

Deriving a recurrence relating a solution to the problem’s current instance with solutions of its smaller ( and overlapping) subinstances.

Page 13: Algorithm chapter 8

The Knapsack Problem and Memory Functions (1)

The problemFind the most valuable subset of the given n items that fit into a knapsack of capacity W.

Consider the following subproblem P(i, j)Find the most valuable subset of the first i items that fit into a knapsack of capacity j, where 1 ≤ i ≤n, and 1≤ j ≤ WLet V[i, j] be the value of an optimal solution to the above subproblem P(i, j). Goal: V[n, W] The question: What is the recurrence relation that expresses a solution to this instance in terms of solutions to smaller subinstances?

Page 14: Algorithm chapter 8

The Knapsack Problem and Memory Functions (2)

The RecurrenceTwo possibilities for the most valuable subset for the subproblem P(i, j)

It does not include the ith item: V[i, j] = V[i-1, j]It includes the ith item: V[i, j] = vi+ V[i-1, j – wi]

V[i, j] = max{V[i-1, j], vi+ V[i-1, j – wi] }, if j – wi ≥ 0V[i-1, j] if j – wi < 0

V[0, j] = 0 for j ≥ 0 and V[i, 0] = 0 for i ≥ 0

{

Page 15: Algorithm chapter 8

Memory FunctionsMemory functions: a combination of the top-down and bottom-up method. The idea is to solve the subproblems that are necessary and do it only once.

Top-down: solve common subproblems more than once.Bottom-up: Solve subproblems whose solution are not necessary for the solving the original problem.

ALGORITHM MFKnapsack(i, j)if V[i, j] < 0 //if subproblem P(i, j) hasn’t been solved yet.

if j < Weights[i]value MFKnapsack(i – 1, j)

elsevalue max(MFKnapsack(i – 1, j),

values[I] + MFKnapsck( i – 1, j – Weights[i]))V[i, j] value

return V[i, j]