Dynamic Programming: of Optimality
Dynamic Programming: of Optimality
General method
Definition 1 The principle of optimality states that an optimal sequence of decisions has the property that whatever the
initial state and decision are, the remaining states must constitute an optimal decision sequence with regard to the state
resulting from the first decision.
Dynamic Programming 2
The above solution is known as bottom-up dynamic programming – we compute the smallest values first and build the
solution using the solution to smaller problems; most of the real dynamic programming situations refer to top-down
dynamic programming (also known as memoization) as you will see next in knapsack problem
• Knapsack problem
– Recursive solution
∗ Each time you choose an item, you assume that you can optimally find a solution to pack the rest of the
knapsack
struct item
{
int size;
int val;
};
Property 1 Dynamic programming reduces the running time of a recursive function to be at most the time required to
evaluate the function for all arguments less than or equal to the given argument, treating the cost of a recursive call as
constant.
• Property 1 implies that the running time for the knapsack problem is O(N M )
• Dynamic programming becomes ineffective when the number of possible function values that may be needed is so high
that we cannot afford to save or precompute all of them
• Dynamic programming solution to 0/1 knapsack
– The intermediate knapsack problem knap(l,j,y) can be represented by
P
Maximize px
Pl≤i≤j i i
subject to l≤i≤j wi xi ≤ y
xi ∈ {0, 1}, l ≤ i ≤ j
– The original knapsack problem now is: knap(0,n-1,m)
– Let gj (y) be the value of an optimal solution to knap(j + 1, n, y).
∗ g0 (m) is the value of an optimal solution to knap( 1, n, m )
∗ The possible decisions for x1 are 0 and 1 (D1 = {0, 1})
∗ From the principle of optimality, it follows that
∗ The recursion is solved by using gn (y) = 0 for y ≥ 0 and gn (y) = −∞ for y < 0
∗ From gn (y), we can obtain gn−1 (y) using the above recurrence; then, using gn−1 (y), we can find gn−2 (y),
and so on; finally, we can determine g0 (m)
– Example
∗ n = 3, w = {2, 3, 4}, p = {1, 2, 5}, m = 6
∗ We have to compute g0 (6)
= 5
g2 (3) = max(g3 (3), g3 (3 − 4) + 5)
= max(0, −∞)
= 0
g1 (6) = max(5, 2)
= 5
g1 (4) = max(g2 (4), g2 (4 − 3) + 2)
g2 (4) = max(g3 (4), g3 (4 − 4) + 5)
= max(0, 0 + 5)
= 5
g2 (1) = max(g3 (1), g3 (1 − 4) + 5)
= max(0, −∞)
= 0
g1 (4) = max(5, 2)
= 5
g0 (6) = max(5, 5 + 1)
= 6
– A tour of G is a directed cycle that includes every vertex in V , and no vertex occurs more than once except for the
starting vertex
– Cost of a tour is the sum of the cost of edges on the tour
– Traveling salesperson problem is to find a tour of minimum cost
• Comments
– 0/1 knapsack is a subset selection problem
– Traveling salesperson is a permutation problem
– Permutation problems are harder to solve
∗ n! different permutations of n objects
∗ 2n different subsets of n objects
∗ n! > 2n
• Greedy algorithm
– Start with vertex v1 ; call it vi
– Visit the vertex vj that is nearest to vi , or can be reached from vi with least cost
– Repeat the above starting at vertex vj (call it as new vi ) taking care never to visit a vertex already visited
• Dynamic programming algorithm
– Regard the tour to be a simple path that starts and ends at vertex 1
– Every tour consists of an edge h1, ki for some k ∈ V − {1} and a path from vertex k to vertex 1
– The path from vertex k to vertex 1 goes through each vertex in V − {1, k} exactly once
– If the tour is optimal, then the path from k to 1 must be a shortest k to 1 path going through all vertices in V − {1, k}
– Let g(i, S) be the length of a shortest path starting at vertex i, going through all vertices in S, and terminating at
vertex 1
– g(1, V − {1}) is the length of an optimal salesperson tour
– From the principal of optimality
– Generalizing (for i 6∈ S)
g(i, S) = min{cij + g(j, S − {j})} (2)
j∈S
– Equation 1 may be solved for g(1, V − {1}) if we know g(k, V − {1, k}) for all values of k
– The g values may be obtained by using Equation 2
∗ g(i, φ) = ci,1 , 1 ≤ i ≤ n
∗ We can use Equation 2 to obtain g(i, S) for all S of size 1
∗ Then we can obtain g(i, S) for S with |S| = 2
∗ When |S| < n − 1, the values of i and S for which g(i, S) is needed are such that i 6= 1, 1 6∈ S, and i 6∈ S
• Solving traveling salesperson problem with dynamic programming – example
– Consider the directed graph presented below
0 10 15 20
5 0 9 10
6 13 0 12
8 8 9 0
Dynamic Programming 8
– Solving for 2, 3, 4
g(2, φ) = c21 = 5
g(3, φ) = c31 = 6
g(4, φ) = c41 = 8
– Using Equation 2, we get
g(1, {2, 3, 4}) = min{c12 + g(2, {3, 4}), c13 + g(3, {2, 4}), c14 + g(4, {2, 3})}
= min{35, 40, 43}
= 35
– Optimal tour
∗ Has cost 35
∗ A tour of this length may be constructed if we retain with each g(i, S) the value of j that minimizes the right
hand side of Equation 2
∗ Let this value be called J(i, S)
∗ Then, J(1, {2, 3, 4}) = 2
∗ Thus the tour starts from 1 and goes to 2
∗ The remaining tour may be obtained from g(2, {3, 4})
∗ Now, J(2, {3, 4}) = 4
∗ Thus the next edge is h2, 4i
∗ The remaining tour is for g(4, {3})
∗ J(4, {3}) = 3
∗ The optimal tour is 1, 2, 4, 3, 1
• Analysis of traveling salesperson
– Let N be the number of g(i, S)s that have to be computed before Equation 1 may be used to compute g(1, V − {1})
– For each value of |S|, there are n − 1 choices of i
n−2
– The number of distinct sets S of size k not including 1 and i is
k
– Hence,
n−2
X n−2
N= (n − k − 1) = (n − 1)2n−2
k
k=0
– An algorithm that finds an optimal tour using Equations 1 and 2 will require Θ(n2 2n ) time as the computation of
g(i, S) with |S| = k requires k − 1 comparisons when solving Equation 2
– Better than enumerating all n! different tours to find the best one
– The most serious drawback of the dynamic programming solution is the space needed (O(n2n ))
∗ This can be too large even for modest values of n.
Dynamic Programming 9
✓
Station
✓✏ S1,1 Station
✓✏ S1,2 Station
✓✏ S1,3 Station
✓✏ S1,n−1 Station
✓✏ ✏
S1,n
Assembly line 1 a 1,1 ✲ a 1,2 ✲ a 1,3 ✲ ··· ✲ a 1,n−1 ✲ a 1,n
✒✒✑ ✒✑ ✒✑ ✒✑ ✒✑ ✑
✒ ❆ ✕ ❆
✁ ✁✕ ✁✕ ❆ ✁✕ ❅
✓✏ ❆ ✁ ❆ ✁ ✁ ❆ ✁ ❅❘✓✏
e1 ❯
❆
✓✏ ✁ ✓✏❆❯ ✁ ✁ ❆❯
✓✏ ✁ x1
✒✑ ✁ ✁ ✁ ✒✑
✒ t1,1
✁
t1,2
✁
t1,n−1
✁ ❅
✒✑ ✒✑ ✒✑ ❅❘
Chassis ❆ ✁ ❆ ✁ ❆ ✁ Completed
enters ✁
❆ ✁
❆ ··· ✁
❆
✓✏ ✁ ❆ ✓✏ ✁ ❆ ✓✏ ✁ ❆ auto exits
❅ ✒
t2,1 ❆ t2,2 ❆ t2,n−1 ❆
❅❅✓✏
❘ ✒✑ ❆ ✒✑ ❆ ✒✑ ❆
✓✏
e2 ✕
✁ ✁✕ ❆ ✁✕ x2
✒✑ ❆ ❆ ❆ ✒✑
❅ ✁ ❆ ✁ ❆ ❆ ✁ ❆ ✒
❅✓
❘✓✏ ✁ ❯ ✁
❆
✓✏ ✓✏❆❯ ❆❯ ✁
✓✏ ❆❯
✓✏ ✏
Assembly line 2 a2,1 ✲ a2,2 ✲ a2,3 ✲ · · · ✲a2,n−1 ✲ a2,n
✒✒✑ ✒✑ ✒✑ ✒✑ ✒✑ ✑
Station S2,1 Station S2,2 Station S2,3 Station S2,n−1 Station S2,n
– Entry time for assembly line i denoted by ei
– Exit time for assembly line i denoted by xi
• Chassis can go from one station to another
– Within the same assembly line in negligible time, or at no cost
– To the other assembly line at some cost
∗ Cost to go from one assembly line to another after having gone through station Sij is tij
• Problem is to schedule the assembly line such that the selection of stations from each assembly line minimizes the overall
assembly cost
– Need to determine the stations to choose from assembly line 1 and 2 to minimize the assembly time
– In the following example, choose stations 1, 3, 6 from line 1 and 2, 4, 5 from line 2
Dynamic Programming 10
– Optimal substructure
∗ Optimal solution to a problem contains the optimal solution to the subproblems within it
∗ Fastest way to a station requires that the chassis must have taken the fastest way to the previous station in the
line
∗ To find the fastest way to a station Si,j , solve the subproblem to compute the fastest way to the two previous
stations – Si,j−1 and Si′ ,j−1
• Step 2: A recursive solution
– Define the value of an optimal solution recursively in terms of optimal solution to subproblems
∗ Subproblems will be defined as the problem of finding the fastest way through station j on both lines, for
j = 1, 2, . . . , n
– Let fi [j] be the fastest possible time to get a chassis from starting point through station Si,j
– Let f ∗ be the fastest time to get the chassis all the way through the factory
f1 [1] = e1 + a1,1
f2 [1] = e2 + a2,1
– Define li [j] to keep track of line number whose station j − 1 is used to get to station Si,j
∗ No need to define l1 [j] because no station precedes station 1 on either line
– Define l∗ to be the line whose station n is used as the last station to get through the assembly line
– Starting with l∗ = 1, use station S1,6
– l1 [6] = 2 ⇒ station S2,5
– l2 [5] = 2 ⇒ S2,4
– l2 [4] = 1 ⇒ S1,3
– l1 [3] = 2 ⇒ S2,2
– l2 [2] = 1 ⇒ S1,1
• Step 3: Computing the fastest times
– Simple to write recursive algorithm but its running time is exponential in n
– Compute fi [j] values in increasing order of station numbers
∗ Leads to Θ(n) time