Dynamic Programming

From text, "Dynamic programming is an algorithm design method that can be used when the solution to a problem can be viewed as the result of a sequence of decisions. In earlier chapters we saw many problems that can be viewed this way."

Both the Knapsack problem and Shortest Path problem, as noted in the previous section, can be solved with a greedy strategy. For many problems, it is not possible to make stepwise decisions (based on only local information) in such a manner that the sequence of decisions made is optimal. (Consider Example 5.4 -- shortest path and the optimality of the first "greedy" choice in the whole solution.)

See Examples 5.1 - 5.13 in text for an illustration of taking the knapsack and shortest path problems and adapting them to more difficult problems with combinatorial solutions hence requiring a sequence of decisions.

I. Perspective

II. Principle of Optimality

An optimal sequence of decisions may be found by making decisions one at a time and never making an erroneous decision

III. Steps of Dynamic Programming

Basically, the idea is to "figure" the options up to a point. From this point calculate the next step to maintain optimality. (sort of like proofs by induction - assume n and show n+1 BUT - each next step is a new input which could change the previous best so far).

IV. Examples of Dynamic Programming

A dynamic programming algorithm always considers a sequence of decisions

V. Example: 0/1 Knapsack

Example: knapsack problem was to find {x1, x2, . . ., xn} such than 0 <xi< 1

The Knapsack greedy algorithm assigns x values in one pass.

Adapt the Knapsack Problem to consider {x1, x2, . . ., xn} such than xi = 0 OR xi = 1

In general we may have to try all possible decision sequences.

Find {x1, x2, . . ., xn} subject to feasibility criteria and optimize objective function.

If each xi can take on only a finite set of values, then we can try all combinations.

If b possible values for each xi, then bn combinations: O(bn)

From the text, page 257:

Note the progression in the text exercises below and the steps in III.

  1. Example 5.6 (2- Optimality): Principle of Optimality holds,

  2. Example 5.8 (1 - a solution): Let gj(y) be the value of an optimal solution to KNAP(j+1,n,y) then
    g0(m) is the value of an optimal solution to KNAP(1,n,m) and g0(m) = max {g1(m), g1(m- w1) + p1}

  3. Example 5.10 (3 - recurrence relation): gi(y) = max {gi+1(y), gi+1(y- wi+1) + pi+1}
Example 5.13 - recurrence equation could work forward OR backward
Example 5.11

VI. The All-Pairs Shortest Path Problem

Given a directed graph with edges (E), find shortest path for all (i,j) pairs.

Note that "all (i,j) pairs" would indicate a matrix. That is what we generate - a matrix A such that A(i,j) is the length of the shortest path between i and j.

We allow negative lengths but no negative cycles.

A (i,j) = shortest path starting at i stopping at j passing only through nodes in S. (note i and j)

Each calculation would be single-source shortest path. O(n2) ==> O(n3)

Starting with S = {} keep adding nodes to S.
Node k being added to S
Ak-1(i,j) - before
Ak(i,j) - after

If i to j is the shortest path and k is on this path, then i to k is smallest and k to j is smallest. Principle of optimality works


Procedure APSP(input: W[1:n,1:n];A[1:n,1:n]) begin for i=1 to n do for j=1 to n do A(0)(i,j) := W[i,j]; endfor endfor for k=1 to n do for i=1 to n do for j=1 to n do A(k)(i,j)=min(A(k-1)(i,j),A(k-1)(i,k) + A(k-1)(k,j)) endfor endfor endfor end


Procedure APSP(input: W[1:n,1:n];A[1:n,1:n]) begin for i=1 to n do for j=1 to n do A(i,j) := W[i,j]; endfor endfor for k=1 to n do for i=1 to n do for j=1 to n do A(i,j)=min(A(i,j),A(i,k) + A(k,j)); endfor endfor endfor end

VII. Shortest path in multistage graphs

See text.
Good example is resource allocation (n units of resource are allocated to r projects.)
Allocate resources to projects in such a way to maximize total net profit.
Rather like the BIPART assignment in the Divide and Conquer homework set.

Partition vertices into k> 2 disjoint sets Vi where 1 < i < k, |V0|= 1 (source), |Vk|= 1 (sink)

no cycles

V = U i=0 to k Vi     Edges connect adjacent layers. If <u,v> E, then u Vi and v Vi+1

Shortest path from V0: s (source) to Vk: t (sink)

book example shows a five-stage graph. A minimum cost path is indicated by the broken edges.

c(i,j) = cost of edge <i,j>

dist(j) = shortest path (cost of distance) from node j (in Vi) to node t (sink).     (Book uses p(i,j) for the minimum-cost path from vertex j in Vi to vertex t and cost(i,j) for its cost)

The cost of a path from s to t is the sum of the costs of the edges on the path. The multistage graph problem is to find a minimum-cost path from s to t.
Each set Vi defines a stage in the graph.

Dynamic programming formulation

consider some j Vi

Note that for a k stage graph, every s to t path is a result of a sequence of k - 2 decisions. The ith decision involves determining which vertex in Vi+1 is to be on the path. It is easy to see that the principle of optimality holds.

Recurrance formula:     dist (j) = min   l Vi+1 and (j,l) E   {c(j,l) + dist (l)}

start with {dist (t) = 0}

     book example solved

Calculate dist for nodes according to decreasing layer number.

Time: at each node, work done proportional to degree
allocate work done to edges O(n + e)

Example of use:

(page 258) Do this very slowly considering Figure 5.3

Consider a resource allocation problem in which n units of resource are to be allocated to r projects.
If j, 0 < j < n, units of the resource are allocated to project i, then the resulting net profit is N(i,j). The problem is to allocate the resource to the r projects in such a way as to maximize total net profit.
This problem can be formulated as an r + 1 stage graph problem as follows:

The resulting graph for a three-project problem with n=4 is shown in Figure 5.3 .

Try problem 1 on page 264

VIII. Traveling Salesperson

A tour of G is a directed simple cycle that includes every vertex in V. The cost of a tour is the sum of the cost of the edges on the tour. The traveling salesperson problem is to find a tour of minimum cost.

Note that for the knapsack problem, we were looking at a "subset" problem, i.e., we needed the correct choices for objects in a set of size n. Hence the possible number of alternatives (or possible subsets for a set n with two options for each) was bn (Or 2n when we had two choices). This (traveling salesperson problem) is a permutation problem. We have n cities to visit; note that the number of permutations of a set of n items is n!.
Also note than n! >> 2n (as early as n=4 ... and then grows quite fast).

Dynamic programming formulation

Without loss of generality, consider a tour to be a simple path that starts and ends at vertex 1. Every tour consists of The path from vertex k to vertex 1 goes through each vertex in V -{1,k} exactly once. It is easy to see that if the tour is optimal, then the path from k to 1 must be the shortest k to 1 path through all vertices in V - {1,k}. Hence the principle of optimality holds.

Let g(i,S) be the length of the shortest path starting at vertex i, going through all vertices in S, and terminating at vertex 1. The function g(1,V-{1}) is the length of the optimal salesperson tour. From the principle of optimality it follows that

g(1, V-{1}) = min 1<k<n {c1k + g(k, V - {1,k})}

Generalizing, for i not an element of S

g(i, S) = min j S {cij + g(j, S - {j})}

Consider principle of optimality, and what the function given means/does.

cij is cost from i to j, so we are looking at all the j's that link to i . In your mind say, "I have the problem solved for everywhere except this vertex. Look at all the places that this vertex connects to and how good are the solutions up to that point. I want the miminum new sum of (1)what has been done so far with (2)this new edge I need to complete."

Look how much this looks like the multistage problem. Discuss differences. Read Section 5.9 for additional interesting applications of this problem.

Trace a traveling sales example (page 300) and look at another example of single source with negative paths and dynamic programming (Figure 5.10, page 272)

Here is a lecture from MIT on Longest Common Subsequence

IX. Summary

Dynamic Programming hints: