Week8_DynamicProgramming
Week8_DynamicProgramming
2025-Spring
Recall: Last Lecture
• A sorting algorithm is stable when numbers with the same values
appear in the output array in the same order as they do in the
input array.
2
Q. Do we care for simple arrays like the array of integers?
A. No!
4
Agenda
• Dynamic Programming
• Example: Rod Cutting
• Example: Assembly Line Scheduling
5
Dynamic Programming
Dynamic programming, like the divide-and-conquer method, solves problems by
combining the solutions of subproblems.
Example: How should you cut your 4 inch rod to maximize its sale?
9
Example 1: Rod-cutting (cont.)
Example: How should you cut your 4 inch rod to maximize its sale?
Possible combinations:
10
Example 1: Rod-cutting(cont.)
11
Rod-cutting:Recursive implementation
12
Rod-cutting:Recursive implementation
14
rod-cutting problem : using dynamic
programming
17
Example: How should you cut your 8 inch rod to
maximize its sale?
18
We can use memoization!
19
V
20
V
21
V
22
Rod-cutting:Memoized approach
Initializing table
T(n) = Θ(n2).
23
Rod-cutting: Bottom-up Method
The bottom-up version is even This solution uses the natural ordering of
simpler:
the subproblems:
a problem size i is “smaller” than a
BUTTOM-UP-CUT-ROD(p,n) subproblem of size j if i<j. The procedure
1 Let r[0..n] be a new array solves subproblems of sizes j=0,1,...,n, in
that order.
2 r[0]=0
3 for j=1 to n
4 q=-∞ Solve each subproblem of size j in
5 for i=1 to j order of increasing size.
6 q=max(q, p[i]+r[j-i])
7 r[j] = q
In the example, we used this method!
8 return r[n]
24
rod-cutting problem : using dynamic
programming
The running time;
◦ For BOTTOM-UP-CUT-ROD is ( n 2 ,) due to its doubly-
nested loops.
◦ For top-down algorithm MEMOIZED-CUT-ROD is also ( n 2 )
25
Hence:
26
Reconstructing a Solution
EXTENDED-BOTTOM-UP-CUT-ROD(p,n)
1 let r[0..n] and s[0..n] be a new arrays
2 r[0]=0
3 for j=1 to n
4 q=-∞
5 for i=1 to j
The array s holds optimal size i
6 if q< p[i]+r[j-i] of the first piece to cut off when
7 q= p[i]+r[j-i] solving subproblem of size j.
8 s[j] = i
PRINT-CUT-ROD-SOLUTION(p,n)
9 r[j] = q 1 (r,s)=EXTENDED-BOTTOM-UP-CUT-ROD(p,n)
10 return r[n] 2 while n>0
3 print s[n]
4 n=n-s[n]
27
Reconstructing a Solution
EXTENDED-BOTTOM-UP-CUT-ROD(p,n)
1 let r[0..n] and s[0..n] be a new arrays
2 r[0]=0
3 for j=1 to n PRINT-CUT-ROD-SOLUTION(p,n)
1 (r,s)=EXTENDED-BOTTOM-UP-CUT-ROD(p,n)
4 q=-∞ 2 while n>0
5 for i=1 to j 3 print s[n]
4 n=n-s[n]
6 if q< p[i]+r[j-i]
7 q= p[i]+r[j-i]
8 s[j] = i Examle: call PRINT-CUT-ROD-SOLUTION(p,10)
9 r[j] = q
10 return r[n]
Length 1 2 3 4 5 6 7 8 9 10
i
Price pi 1 5 8 9 10 17 17 20 24 30
28
Example 2: Assembly-Line Scheduling
L2.29
30
31
Assembly-Line Scheduling
Assembly-Line Scheduling
Assembly-Line Scheduling
Generally: An optimal solution to a problem (fastest way through S1,j)
contains within it an optimal solution to subproblems (fastest way through
S1,j-1 or S2,j-1).
Use optimal substructure to construct optimal solution to problem from
optimal solutions to subproblems.
Symmetrically:
Assembly-Line Scheduling : Recursive Solution
Assembly-Line Scheduling : Recursive Solution
Algorithm
40
Trace of the algorithm
41
Next Week
Greedy Algorithms