Beruflich Dokumente
Kultur Dokumente
Nadia Brauner
Nadia.Brauner@imag.fr
Dynamic Programming
1
Dynamic Programming
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Plan
1 Some games
2 Principle of Optimality
3 Dynamic Programming
4 The knapsack
5 Typical problems
3
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Plan
1 Some games
2 Principle of Optimality
3 Dynamic Programming
4 The knapsack
5 Typical problems
4
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Some games
Find a path from the top to the bottom of the pyramid that
maximizes the sum of the numbers encountered
3
7 4
2 4 6
8 5 9 3
6 7 4 2 8
3 3
7 4 7 4
2 4 6 2 4 6
8 5 9 3 8 5 9 3
6 7 4 2 8 6 7 4 2 8
5
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Some games
33
710 47
212 414 613
820 519 923 316
626 727 427 225 826
6
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Some games
Sharing coins
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 V V
2 V V V V
3 V V V V V V V
4 V V V V V V V V V
7
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Some games
8
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Plan
1 Some games
2 Principle of Optimality
3 Dynamic Programming
4 The knapsack
5 Typical problems
9
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Principle of Optimality
P1 P2 P
3
10
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Principle of Optimality
Principle of Optimality
The optimum on instance I can be constructed from optimal
solutions for simpler instances I1 , . . . , Ik
11
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Principle of Optimality
I3
12
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Plan
1 Some games
2 Principle of Optimality
3 Dynamic Programming
4 The knapsack
5 Typical problems
13
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Dynamic Programming
DAG
DAG : Directed Acyclic Graph
nodes can be linearized
all arcs from left to right
in DP
the DAG is implicit
nodes : sub-problems
edges : dependencies between sub-problems
If the vertices are viewed from left to right, all predecessors are
known : the information needed is available
Every DP has an underlying DAG structure
14
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Dynamic Programming
15
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Dynamic Programming
16
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Dynamic Programming
The steps
Find the sub-problems
Find the relation (write down the recurrence formula)
Find an order that respects the recurrence
Define the base cases
17
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Dynamic Programming
Some principles
In general, do not use recursion
risk of calculating many times the same sub-problem
If recursivity is interesting, use memoisation
If only a subset of the sub-problems has to be computed
In a state, store the necessary information to reach the
optimum
Dominance properties to decrease the number of states
18
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Dynamic Programming
Complexity
Time complexity : often based on the number of states
(nodes of the DAG)
Space complexity : needs some thought.
Do not necessarily store all the states...
See page 178 of the book Algorithms by Dasgupta et al.
19
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Plan
1 Some games
2 Principle of Optimality
3 Dynamic Programming
4 The knapsack
5 Typical problems
20
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Combinatorial problems
21
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
The knapsack
Example
A hiker wants to fill his knapsack of capacity 6kg with the most
useful objects
utility weight
A a picture 12 0.2
B a bottle 7 2
C another bottle 3 2
D a pullover 4 0.8
E chocolate bars 5 1
F dried fruit 8 1
22
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
The knapsack
Model
INSTANCE :
FEASIBLE SOLUTIONS :
CRITERION :
23
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
The knapsack
Instance I to be solved
Partition according to object n
F 0 = {S ∈ F|n ∈ / S} does not contain n
00
F = {S ∈ F|n ∈ S} contains n
We obtain OPT (I ) = max{c(S 0∗ ), c(S 00∗ )}
24
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
The knapsack
F’ F’’
The knapsack
F’ F’’
The knapsack
Knapsack
Instance: n objects of weight wi and of utility ui , a
knapsack of size W .
Solution: subset S of objects such that w (S) ≤ W .
Criterion: the total utility u(S) of the objects
27
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
The knapsack
Parametrization
Optimality principle : the problems which appear in the
decomposition correspond to the initial problem on simpler
instances
Instance I 0 for the sub-problem
⇒ I 0 differs from I by some parameters (integers) p1 , . . . , pl
For the knapsack : the objects under consideration and the
size of the knapsack
We describe I 0 by the value of its parameters (x10 , . . . , xl0 )
Definition
A state is the vector of parameters (x1 , . . . , xl ) which describes the
sub-instance
28
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
The knapsack
State graph
Parameter vector (x1 , . . . , xl ) : state
Dependencies between the instances (calculation of f )
parametre 2
I
3
0
parametre 1
0 1 2 3 4 5 6
29
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
The knapsack
Knapsack
Instance: n objects of weight wi and of utility ui , a
knapsack of size W .
Solution: subset S of objects such that w (S) ≤ W .
Criterion: the total utility u(S) of the objects
30
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
The knapsack
31
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
The knapsack
32
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
The knapsack
Effectiveness
What is the resolution time ?
It depends on
the number of states
the time t to evaluate function f in each state
The resolution time is then
X
t(x1 , . . . , xl )
(x1 ,...,xl )∈States
T × #States
33
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
The knapsack
34
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
The knapsack
35
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Plan
1 Some games
2 Principle of Optimality
3 Dynamic Programming
4 The knapsack
5 Typical problems
36
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Typical problems
Shortest paths
Shortest paths
Shortest reliable paths
All pairs shortest path (Floyd Warshall)
Travelling Salesman Problem
Notice that conditions on the distances (positive) or on the graph
(no absorbing cycle) are necessary
37
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Typical problems
Inventory problems
time decomposed into periods
a demand for each period
limited production capacities
fixed or unitary production costs
limited storage capacity
unitary storage costs
Determine for each period the production in order to minimize the
costs
38
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Typical problems
Resource allocation
w units of a resource available
T activities to which the resource can be allocated
If activity t is implemented at a level xt ≥ 0 then
gt (xt ) units of the resource are used,
a benefit rt (xt ) is obtained.
Objective : determine the allocation of resources that
maximizes the total benefit
If gt and rt are linear ⇒ LP
If the values of xt are discrete ⇒ DP
39
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
Typical problems
40
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems
41