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.
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.
From the text, page 257:
Optimal solutions to subproblems are retained so as to avoid recomputing their values
Note the progression in the text exercises below and the steps in III.
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.
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
A(0)(i,j) = W[i,j]. (goes through no vertex - just (i,j) edge)
Note that
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> 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.
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
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
Consider a resource allocation problem in which n units of resource are to be allocated to r projects.
Try problem 1 on page 264
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!.
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
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
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
for all combination of xi values
If b possible values for each xi, then bn combinations:
O(bn)
check for constraints
end
calculate optimizing function
keep track of max.
one does not have to look at all possible decision sequences to obtain an optimal decision sequence because subsequences that are suboptimal are not considered. Although the total number of different decision sequences is exponential, dynamic programming algorithms often have polynomial complexity
Example 5.13 - recurrence equation could work forward OR backward
g0(m) is the value of an optimal solution to KNAP(1,n,m) and g0(m) = max {g1(m), g1(m- w1) + p1}
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.

Node k being added to S
Ak-1(i,j) - before
Ak(i,j) - after

A(k)(i,k) = A(k-1)(i,k)
i.e., kth row and column remain the same.
A(k)(k,j) = A(k-1) (k,j)
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.
Each set Vi defines a stage in the graph.
Dynamic programming formulation
consider some j
allocate work done to edges O(n + e)
Example of use:
(page 258) Do this very slowly considering Figure 5.3
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 .
Represents that at each project, there are 0 or 1 or 2 or ... n resources that could have been used (0 to n at each stage indicates n+1 possibilities)
Note X and Y. Since X is from V(3,3), 3 resources have already been allocated by stage 3.
Hence the best net profit (N) is max{N(3,0),N(3,1)} since I can use only 4 resources total to get from stage 3 to V(4,4).
Similarly, since Y is from V(3,2), 2 resources have already been allocated by stage 3.
Hence the best net profit (N) is max{N(3,0),N(3,1),N(3,2)} since I can use only 4 resources total to get from stage 3 to V(4,4).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.
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
(a) an edge <1,k> for some k
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.
(b) a path from vertex k to vertex 1.
Together these
make a cycle from 1 to 1 (or anywhere to anywhere)IX. Summary
Dynamic Programming hints: