DynamicProgramming Part1 FEUP
DynamicProgramming Part1 FEUP
Design of Algorithms
Dynamic Programming
Introduction
Principle of Optimality and Recurrences
Graph Shortest Path Example
2 4
6
9 2 2 6 9
5 4
7 3 1 4
7 3 2
7 10 12
1 3 5
5
4 11 6
2
11 8 11
8
5
2 4
6
9 2 2 6 9
5 4
7 3 1 4
7 7 3 10 2 12
1 3 5 5
2 4 11 6
11 8 8 11
5
V1 V2 V3 V4 V5
source sink
Analysis and Synthesis of Algorithms 3
Minimum Cost Source to Sink Path
2 4
6
9 2
2 6 9
5 4
7 3 1 4
7 3 2
source 1 7 10 12 sink
3 5
5
2 4 11
6
11 8 11
8
2 4
6
9 2
2 6 9
5 4
7 3 1 4
7 3 2
source 1 7 10 12 sink
3 5
5
2 4 11
6
11 8 11
8
2 4
6
9 2
2 6 9
5 4
7 3 1 4
7 3 2
source 1 7 10 12 sink
3 5
5
2 4 11
6
11 8 11
8
• Problems:
– Concatenation of best paths of sub-problems may not yield a path
– Best overall path may be suboptimal at different sub-problem stages
Analysis and Synthesis of Algorithms 6
Greedy Approach?
2 4
6
9 2
2 6 9
5 4
7 3 1 4
7 3 2
source 1 7 10 12 sink
3 5
5
2 4 11
6
11 8 11
8
• Problems
– Guaranteed to always have a feasible path but…
– Decisions based on local information (for a single stage at a time)
– Cannot “look ahead” to anticipated good or bad follow-on paths
Analysis and Synthesis of Algorithms 7
Minimum Cost Source to Sink Path
2 4
6
9 2
2 6 9
5 4
7 3 1 4
7 3 2
source 1 7 10 12 sink
3 5
5
2 4 11
6
11 8 11
8
S S
m vertices
at each stage
• Problem? s stages
– Worst Case Complexity is O(ms) for all Possible Paths
– Exponential in the Input Problem Size
Analysis and Synthesis of Algorithms 10
Principle of Optimality
Principle: If a solution is optimal, any portion of it should be optimal
with respect to all the other possible choices of that particular portion.
i
S S
• Computation:
– In reverse from the sink backwards to the source.
– Recurrence is forward though…
Analysis and Synthesis of Algorithms 13
Back to Our Example
• Principle of Optimality
– Shown by Contradiction
– Recurrence Relation uses Previous Stage Results
• Forward Approach
– cost(i,j) is cost of optimal path from node j at stage i to sink
– c(j,k) is the cost associated with the (j,k) edge
4
min(4)
(9-12)
2
min(2)
(10-12)
5
min(5)
(11-12)
7 4
min(6+4,5+2) min(4)
(6-12) (9-12)
5 2
min(4+4,3+2) min(2)
(7-12) (10-12)
7 5
min(5+2,6+5) min(5)
(8-12) (11-12)
7
min(4+7,2+5,1+7) 4
(2-12) 7
min(6+4,5+2) min(4)
9 (6-12) (9-12)
min(2+7,7+5) 2
(3-12) 5
min(4+4,3+2) min(2)
18 (7-12) (10-12)
min(11+7) 5
(4-12) 7
min(5+2,6+5) min(5)
15 (8-12) (11-12)
min(11+5,8+7)
(5-12)
Analysis and Synthesis of Algorithms 18
Building the Table for the Example
4 6
2 2 6 9
9 2 5 4
7 3 1 4
7 3 2
source 1 3 7 10 12 sink
4 11 5 5
2
11 6
5 8 8 11
7
min(4+7,2+5,1+7) 4
(2-12) 7
min(6+4,5+2) min(4)
9 (6-12) (9-12)
16 min(2+7,7+5) 2
(3-12) 5
min(9+7,7+9,3+18,2+15) min(4+4,3+2) min(2)
(1-12) 18 (7-12) (10-12)
min(11+7) 5
(4-12) 7
min(5+2,6+5) min(5)
15 (8-12) (11-12)
min(11+5,8+7)
(5-12)
Analysis and Synthesis of Algorithms 19
Building the Table for the Example
4 6
2 2 6 9
9 2 5 4
7 3 1 4
7 3 2
source 1 3 7 10 12 sink
4 11 5 5
2
11 6
5 8 8 11
7
min(4+7,2+5,1+7) 4
(2-12) 7
min(6+4,5+2) min(4)
9 (6-12) (9-12)
16 min(2+7,7+5) 2
(3-12) 5
min(9+7,7+9,3+18,2+15) min(4+4,3+2) min(2)
(1-12) 18 (7-12) (10-12)
min(11+7) 5
(4-12) 7
min(5+2,6+5) min(5)
15 (8-12) (11-12)
min(11+5,8+7)
(5-12)
Analysis and Synthesis of Algorithms 20
Determining the Paths
• Trace Back Table Values
– Keep a Link to the nodes on the previous stage that derived
the current minimum cost
– Trace Back Links when Reaching Source
– Ties means some branches might have Multiple sub-paths
• Example:
– Minimum Cost is 16
– Two paths:
• 1 ® 2 ® 7 ® 10 ® 12
• 1 ® 3 ® 6 ® 10 ® 12
• A Sequence of Decisions
– Impossible to make optimal decision based only on ”local” information
• Greedy Approaches Fails
– Decisions are Interdependent
• Divide-and-Conquer Approaches Fail
– Enumerate All Possible Alternatives
• But Avoids Recomputing them using table (memorization)
• Time Complexity
– Nodes as Visited Once for a given Stage
– Edges are Visited Once to Compute the Recurrence Equation
!"#$ %, ' = min ! ', 4 + !"#$(% + 1, 4)
, ∈ ./01
2,, ∈ 3