Advanced Algorithms TUTORIAL 6 Dynamic Programming
Advanced Algorithms TUTORIAL 6 Dynamic Programming
Dynamic Programming
Chapter 6
Optimization
This, generally, refers to classes of problems that possess
multiple solutions at one level, and where we have a real-
valued function defined on the solutions.
Independent subproblems
Overlapping subproblems
24 - 1 = 23 = 8
For a rod of length n: 2n-1. Exponential: we cannot try all possibilities for n
"large". The obvious exhaustive approach won't work.
Example: Rod Cutting
Step 1: Characterizing an Optimal Solution
Question: in how many different ways can we cut a rod of length n?
Proof Details: a rod of length n can have exactly n-1 possible cut positions –
choose 0 ≤ k ≤ n-1 actual cuts. We can choose the k cuts (without repetition)
anywhere we want, so that for each such k the number of different choices is
n 1
k
When we sum up over all possibilities (k = 0 to k = n-1):
n 1 (n 1)!
k 0 k k 0 k!(n 1 k)! 1 1n1 2n1.
n1 n1
Let us find a way to solve the problem recursively (we might be able to modify the
solution so that the maximum can be actually computed): assume we have cut a
rod of length n into 0 ≤ k ≤ n pieces of length i1, …, ik,
n = i1 +…+ ik,
with revenue
rn = pi1 + … + pik
Assume further that this solution is optimal.
Advice: when you don’t know what to do next, start with a simple example and
hope something will occur to you…
Example: Rod Cutting
Characterizing an Optimal Solution
Length i 1 2 3 4 5 6 7 8 9 10
Price pi 1 5 8 9 10 17 17 20 24 30
We begin by constructing (by hand) the optimal solutions for i = 1, …, 10:
r1 = 1 from sln. 1 = 1 (no cuts)
r2 = 5 from sln. 2 = 2 (no cuts)
r3 = 8 from sln. 3 = 3 (no cuts)
r4 = 10 from sln. 4 = 2 + 2
r5 = 13 from sln. 5 = 2 + 3
r6 = 17 from sln. 6 = 6 (no cuts)
r7 = 18 from sln. 7 = 1 + 6 or 7 = 2 + 2 + 3
r8 = 22 from sln. 8 = 2 + 6
r9 = 25 from sln. 9 = 3 + 6
r10 = 30 from sln. 10 = 10 (no cuts)
Example: Rod Cutting
Characterizing an Optimal Solution
Notice that in some cases rn = pn, while in other cases the optimal revenue rn is
obtained by cutting the rod into smaller pieces.
In ALL cases we have the recursion
rn = max(pn, r1 + rn-1, r2 + rn-2, …, rn-1 + r1)
exhibiting optimal substructure (how?)
A slightly different way of stating the same recursion, which avoids repeating some
computations, is
rn = max1≤i≤n(pi + rn-i)
And this latter relation can be implemented as a simple top-down recursive
procedure:
Example: Rod Cutting
Characterizing an Optimal Solution
Summary: How to justify the step from:
rn = max(pn, r1 + rn-1, r2 + rn-2, …, rn-1 + r1)
to
rn = max1≤i≤n(pi + rn-i)
Note: every optimal partitioning of a rod of length n has a first cut – a segment of,
say, length i. The optimal revenue, rn, must satisfy rn = pi + rn-i, where rn-i is the
optimal revenue for a rod of length n – i. If the latter were not the case, there would
be a better partitioning for a rod of length n – i, giving a revenue r’n–i > rn-i and a
total revenue r’n = pn + r’n-i > pi + rn-i = rn.
Since we do not know which one of the leftmost cut positions provides the largest
revenue, we just maximize over all the possible first cut positions.
Example: Rod Cutting
Characterizing an Optimal Solution
We can also notice that all the items we choose the maximum are optimal in their
own right: each substructure (max revenue for rods of lengths 1, …, n-1) is also
optimal (again, optimal substructure property).
T(n) 1
n1
T 0 1, T( j) 2n , n 1.
j 0
Example: Rod Cutting
Beyond Naïve Time Complexity
Specifically:
• Note that navigating the whole tree requires 2n stack-frame activations.
• Note also that no more than n + 1 stack-frames are active at any one time and that
no more than n + 1 different values need to be computed or used.
When we solve the problem in a bottom-up manner the asymptotic time is Θ(n2)
Algorithmic Paradigm Context
Divide & Dynamic Greedy
Conquer Programming Algorithm
View problem as collection of
subproblems
ÒRecursiveÓnature
Independent subproblems overlapping typically
sequential
dependence
Number of subproblems depends on typically small
partitioning
factors
Preprocessing typically sort
Characteristic running time typically log depends on number often dominated
function of n and difficulty of by nlogn sort
subproblems
Primarily for optimization
problems
Optimal substructure:
optimal solution to problem
contains within it optimal
solutions to subproblems
Greedy choice property:
locally optimal produces
globally optimal
Heuristic version useful for
bounding optimal value
Dynamic Programming
Dynamic Programming is an algorithm
design method that can be used when the
solution to a problem may be viewed as the
result of a sequence of decisions
The General Dynamic
Programming Technique
Applies to a problem that at first seems to require a
lot of time (possibly exponential), provided we
have:
Subproblem optimality: the global optimum value can be
defined in terms of optimal subproblems
Subproblem overlap: the subproblems are not
independent, but instead they overlap (hence, should be
constructed bottom-up).
Dynamic Programming: Example
Consider the problem of finding a shortest path between a pair
of vertices in an acyclic graph.
An edge connecting node i to node j has cost c(i,j).
The graph contains n nodes numbered 0,1,…, n-1, and has an
edge from node i to node j only if i < j. Node 0 is source and
node n-1 is the destination.
Let f(x) be the cost of the shortest path from node 0 to node x.
The shortest path
To find a shortest path in a multi-stage graph
3 2 7
1 4
S A B 5
T
5 6
Apply the greedy method :
the shortest path from S to T :
1+2+5=8
Dynamic Programming: Example
2 5 13
S B E T
16 2
5
C 2
F
A
4
D 1 A
1 d(A, T)
18
11 9
2 d(B, T)
S
2
B
5
E
13
T S B T
16 2
5 d(C, T)
5
C 2
F C
5 d(E, T)
S
2
B
5
E
13
T B E T
16 2
d(F, T)
16
5 F
C 2
F
backward reasoning.
Backward approach
(forward reasoning) A
4
D
1 18
11 9
2 5 13
S B E T
16 2
d(S, A) = 1
5
d(S, B) = 2 C 2
F
d(S, C) = 5
d(S,D)=min{d(S,A)+d(A,D), d(S,B)+d(B,D)}
2 5 13
S B E T
16 2
5
C 2
F
Principle of optimality
Principle of optimality: Suppose that in solving a
problem, we have to make a sequence of decisions
D1, D2, …, Dn. If this sequence is optimal, then the
last k decisions, 1 k n must be optimal.
e.g. the shortest path problem
m resources, n projects
profit Pi, j : j resources are allocated to project i.
maximize the total profit.
Project Resource
1 2 3
1 2 8 9
2 5 6 7
3 4 4 4
4 2 4 5
7 -49
The multistage graph solution
A E I
0,1 0 0,2 0 0,3
5 4
0 5
B 7 6 F 4 4 J
1,1 0 1,2 0 1,3
2 4
5 4
S 8 C
6
G
4
K 2
T
2,1 0 2,2 0 2,3
9 0
5 4
D H L
3,1 0 3,2 0 3,3
7 -50
Assignment Find the longest path from S to T :
A E I
0,1 0 0,2 0 0,3
5 4
0 5
B 7 6 F 4 4 J
1,1 0 1,2 0 1,3
2 4
5 4
S 8 C
6
G
4
K 2
T
2,1 0 2,2 0 2,3
9 0
5 4
D H L
3,1 0 3,2 0 3,3
7 -52
Assignment:
Determine the shortest path from node A to node J with Dynamic Programming
Discussion of Parallel Dynamic Programming Algorithms