Chapter 1 : Dynamic programming - ENSIIE

Examples
Generalization
When does it not work ?
Tutorials...
Chapter 1 : Dynamic programming
ENSIIE - Operations Research Module
Dimitri Watel ([email protected])
2016-2017
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
What is the shortest path from A to E ?
50
40
25
A
10
B
20
20
C
15
D
10
20
35
Dimitri Watel
MRO Chap 01 Dyn Prog
E
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
What is the shortest path from A to E ?
50
40
25
A
10
B
20
20
C
15
D
10
20
35
Dimitri Watel
MRO Chap 01 Dyn Prog
E
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
Brute force algorithm
If we enumerate all the paths :
(A - E) costs 50
50
(A - B - E) costs 45
40
25
A
10
B
(A - C - E) costs 45
20
20
C
15
D
10
(A - B - C - E) costs 50
E
20
(A - D - E) costs 50
35
...
Every path containing A and E
If there are n vertices : 2n−2 possible paths to enumerate.
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
Interesting property
In order to reach E, there are 4 ways :
I go from A to E with the arc (A,E)
I go to B then I go to E with the arc (B,E)
I go to C then I go to E with the arc (C,E)
I go to D then I go to E with the arc (D,E)
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
Interesting property
In order to reach E, there are 4 ways :
I go to A then I go to E with the arc (A,E)
I go to B then I go to E with the arc (B,E)
I go to C then I go to E with the arc (C,E)
I go to D then I go to E with the arc (D,E)
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
Interesting property
In order to reach E, there are 4 ways :
I go to A from A with a shortest path then I go to E with
the arc (A,E)
I go to B from A with a shortest path then I go to E with
the arc (B,E)
I go to C from A with a shortest path then I go to E with
the arc (C,E)
I go to D from A with a shortest path then I go to E with
the arc (D,E)
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
Interesting property
In order to reach E, there are 4 ways :
I go to A from A with a shortest path then I go to E with
the arc (A,E)
I go to B from A with a shortest path then I go to E with
the arc (B,E)
I go to C from A with a shortest path then I go to E with
the arc (C,E)
I go to D from A with a shortest path then I go to E with
the arc (D,E)
We have to compute the shortest path from A to A, B, C et D in
order to compute the shortest path from A to E .
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
Extending the property : dynamic programming
Let d(v ) be the shortest path from A to the node v .
Let ω(u, v 
) be the weight of the arc (u, v ).
d(A) + ω(A, E )



d(B) + ω(B, E )
d(A) + ω(A, C )
d(E ) = min
d(C ) = min
d(C ) + ω(C , E )


d(B)
+ ω(B, C )

d(D) + ω(D, E )
d(B) = d(A) + ω(A, B)

 d(A) + ω(A, D)
d(A) = 0
d(B) + ω(B, D)
d(D) = min

d(C ) + ω(C , D)
Complete the following table from left to right
v
A B C D E
d(v )
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
Dynamic programming
50
40
25
A
10
B
20
20
C
15
D
10
20
35
Dimitri Watel
MRO Chap 01 Dyn Prog
E
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
Who is the best ?
Dynamic programming : n·(n+1)
sums and comparisons
2
n−2
Brute force : 2
operations and comparisons.
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
Partition the set
X = {1,5,6,7,9,10,16}
It is possible to partition X into two subsets such that the sum of
the elements in the two subsets is the same ?
!
P
P
∃?X1 , X2 | (X = X1 ] X2 ) ∧
x=
x
x∈X1
Dimitri Watel
x∈X2
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
Partition the set
X = {1,5,6,7,9,10,16}
It is possible to partition X into two subsets such that the sum of
the elements in the two subsets is the same ?
!
P
P
∃?X1 , X2 | (X = X1 ] X2 ) ∧
x=
x
x∈X1
Dimitri Watel
x∈X2
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
Brute force algorithm
X = {1, 5, 6, 7, 9, 10, 16}
If we enumerate all the partitions ?
X1 = ∅ and X2 = {1, 5, 6, 7, 9, 10, 16} : No
X1 = {1} and X2 = {5, 6, 7, 9, 10, 16} : No
X1 = {5} and X2 = {1, 6, 7, 9, 10, 16} : No
X1 = {1, 5} and X2 = {6, 7, 9, 10, 16} : No
X1 = {6} and X2 = {1, 5, 7, 9, 10, 16} : No
...
X1 = {1, 7, 9, 10} and X2 = {5, 6, 16} : Yes
...
If |X | = n : 2n possible partitions to enumerate.
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
A similar problem
X = {1, 5, 6, 7, 9, 10, 16}
Property
!
If
P
x∈X1
x=
P
x∈X2
x
then
P
x=
x∈X1
P
x /2 = 27.
x∈X
New problem
P
∃?X1 ⊂ X |
x = 27
x∈X1
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
Interesting property
X = {1, 5, 6, 7, 9, 10, 16}
If there exists a set X1 for which the sum of the elements is 27,
there are 7 ways to build such a set :
X1 contains 16 and there is a subset of X1 for which the sum
is 27 − 16 = 11
X1 contains 10, but does not contain 16 and there is a subset
of X1 for which the sum is 27 − 10 = 17
X1 contains 9, but contains neither 10 nor 16 and there is a
subset of X1 for which the sum is 27 − 9 = 18
...
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
Interesting property
X = {1, 5, 6, 7, 9, 10, 16}
Many new problems : is there a subset of X that does not contain
16 and such that the sum is 11 ?, a subset of X that does not
contain 10 and 16 and such that the sum is 17 ?, a subset of X that
does not contain 9, 10 and 16 and such that the sum is 18 ?, . . .
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
Dynamic programming
X = {1, 5, 6, 7, 9, 10, 16}
New new problem for every i ≤ 7 and B ≤ 27 :
Is there a subset X1 using only the first i elements of X and such
that the sum is B ?
Let f (i, B) be the answer to that question (true or false).
f (7, 27) = f (6, 11) ∨ f (5, 17) ∨ f (4, 18) ∨ f (3, 20) ∨ f (2, 21) ∨
f (1, 22) ∨ f (0, 26)
f (6, 11) = f (5, 1) ∨ f (4, 2) ∨ f (3, 4) ∨ f (2, 5) ∨ f (1, 6) ∨ f (0, 10)
f (5, 17) = f (4, 8) ∨ f (3, 10) ∨ f (2, 11) ∨ f (1, 12) ∨ f (0, 16)
...
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
Dynamic programming
i
X = {1, 5, 6, 7, 9, 10, 16}
Complete the following table from top to bottom with f (i, B)
B
0 1 2 3 4 5 6 7 8 9 10 11 . . . 27
0
1
2
3
4
5
6
7
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Shortest paths
The Partition problem
Who is the best ?
Dynamic programming : n · B sums
Brute force : 2n operations and comparisons.
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Principle
Definition
Dynamic programming is a method for solving problems, it is not
an algorithm but a way to build an algorithm.
In order to solve a problem with dynamic programming, we need
to rewrite the problem as the computation of a recursive
function f
Initialize f
Recursively compute f (in a smart way)
(see the board for examples)
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Principle
Definition
Dynamic programming is a method for solving problems, it is not
an algorithm but a way to build an algorithm.
In order to solve a problem with dynamic programming, we need
to rewrite the problem as the computation of a recursive
function f
Initialize f
Recursively compute f (in a smart way)
(see the board for examples)
Hardest part
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
In other words
Other definition
Dynamic programming consists in defining a problem of size n as a
bunch of subproblems such that
the nature of each subproblem and of the problem itself are
the same (recursion)
each subproblem is smaller, the size is at most n − 1 (the
recursive algorithm stops)
(see the board for examples)
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Problem
Warning ! The recursive calls may be done twice or more. We say
the subproblems overlap.
(see the board for examples)
The recursive computation must be smart : every
subproblem solution must be stored when it is computed
(iterative way (bottom-up) or memoization (top-down)).
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Recursive combinatorial explosion
We want to transform a binary number into another using
transformation rules. Each rule transform a number into a bigger
number.
For example :
(1)
0 −−→ 10
(2)
1 −−→ 01
(3)
101 −−→ 01001
(4)
010 −−→ 0111
We can now transform numbers
(1)
(2)
(4)
(2)
(3)
0 −−→ 10 −−→ 010 −−→ 0111 −−→ 01011 −−→ . . .
Question : we are given two binary numbers x and y and a set T of
m transformation rules, it is possible to transform x into y with the
rules of T ?
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Recursive combinatorial explosion
There is a dynamic programming algorithm to solve the problem.
But we need to store too many information (2|y |−|x| bits).
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Wrong recursion
Latin square problems
Input
An empty grid with n × n cells
Output
n cells such that
exactly one cell per line and
column
maximize the minimum
distance between two chosen
cells.
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Wrong recursion
Latin square problems
Input
×
An empty grid with n × n cells
×
Output
×
n cells such that
×
exactly one cell per line and
column
maximize the minimum
distance between two chosen
cells.
×
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
Wrong recursion
No obvious way to generate a recursive function.
Dimitri Watel
MRO Chap 01 Dyn Prog
Examples
Generalization
When does it not work ?
Tutorials...
In the syllabus, to be seen in tutorials
How to find the solution in addition to the value of the
solution ? (for instance, how to find a shortest path in addition
to the cost of the shortest path ? ; how to find the partition
instead of only proving it exists, . . .)
The Roy-Floyd-Warshall algorithm : find all the pair of shortest
paths in a graph.
Dimitri Watel
MRO Chap 01 Dyn Prog