Title: Design and Analysis of Algorithms
1 DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF
JOENSUU JOENSUU, FINLAND
- Design and Analysis of Algorithms
- Lecture
- Dynamic programming
- Alexander Kolesnikov
- 17.10.205
2List of sample problems
- Shortest path in trellis graph
- Optimal allocation of constrained resource
- Optimal sequence partition (k-link shortest
path). - to be continued ...
3Shortest path in trellis graph
4Stagecoach problem
A traveler wishes to minimize the length of a
journey from town A to J.
5Greedy algorithm
The length of the route A-B-F-I-J 243413.
Can we find shorter route?
6Exhaustive search try all
Route A-D-F-I-J 313411 The total number
of routes to be tested 3?3?2?118 Can
we avoid exhaustive search?
7Shortest path construction 1st stage (B)
?
8Shortest path construction 1st stage
S(A,B)2 S(A,C)4 S(A,D)3
9Shortest path construction 2nd stage (E)
1. (A..B)-E 279 2. (A..C)-E 437 3.
(A..D)-E 347 ----------------------
(A..C)-E 7
?
10Shortest path construction 2nd stage (E)
1. (A..B)-E 279 2. (A..C)-E 437 ) 3.
(A..D)-E 347 ----------------------
(A..C)-E 7
11Shortest path construction 2nd stage (F)
1. (A..B)-F 246 2. (A..C)-F 426 3.
(A..D)-F 314 ) ----------------------
(A..C)-F 4
?
12Shortest path construction 2nd stage (F)
1. (A..B)-F 246 2. (A..C)-F 426 3.
(A..D)-F 314 ) ----------------------
(A..D)-F 4
13Shortest path construction 2nd stage (G)
1. (A..B)-G 268 ) 2. (A..C)-G 4610 3.
(A..D)-G 358 ----------------------
(A..B)-G 8
?
14Shortest path construction 2nd stage (G)
1. (A..B)-G 268 ) 2. (A..C)-G 4610 3.
(A..D)-G 358 ----------------------
(A..B)-G 8
15Shortest path construction 3rd stage (H)
1. (A..E)-H 718 ) 2. (A..F)-H 4610 3.
(A..G)-H 538 ----------------------
(A..E)-H 5
16Shortest path construction 3rd stage (H)
1. (A..E)-H 718 ) 2. (A..F)-H 4610 3.
(A..G)-H 538 ----------------------
(A..E)-H 5
17Shortest path construction 3rd stage (I)
1. (A..E)-I 7411 2. (A..F)-I 437 ) 3.
(A..G)-I 538 ----------------------
(A..F)-I 7
18Shortest path construction 3rd stage (I)
1. (A..E)-I 7411 2. (A..F)-I 437 ) 3.
(A..G)-I 538 ----------------------
(A..F)-I 7
19Shortest path construction 4th stage (J)
1. (A..H)-J 8311 ) 2. (A..I) -J
7411 ---------------------- (A..H)-J 11
20Shortest path construction 4th stage (J)
1. (A..H)-J 8311 ) 2. (A..I) -J
7411 ---------------------- (A..H)-J 11
21Backtrack the shortest path
7
2
8
11
4
4
0
7
3
8
22The shortest path
7
2
8
11
4
4
0
7
3
8
Route A-C-E-H-J 431311
23Trellis graph
1 2 3 4
K-1 K
24Trellis graph
- Distance (weight) from point i1 at stage (j?1)
to point i2 - at stage j
- The total value of cost function
25Principle of optimality of Bellman
An optimal path has the property that whatever
the initial conditions and control variables
(choices) over some initial period, the control
(or decision variables) chosen over the remaining
period must be optimal for the remaining problem,
with the state resulting from the early decisions
taken to be the initial condition.
26Dynamic programming
27Complexity
- Exhaustive search O(nK)
- Dynamic programming algorithm O(Kn2)
- where K is the number of stages,
- n is the number of points in a stage
28Optimal allocation of constrained resource
29Problem formulation
- N units of a resource
- This resource must be distributed among K
activities - Functions fk(x) - profit for allocated resource
- Allocate N units of resource to K activities
with given - return functions so that the total profit is
maximal
subject to
30Dynamic programming formulation
Optimal value function
Recursive equation
Initialization
31Allocate 3 mln euros into four projects
Profit fk(x), K3, N3.
32Trellis graph
33Solution
)
1 2 f1(2)8 2 1 f2(1)5 3 0
f3(0)0 4 0 f4(0)0 ----------------------
N3 G4(3)13
34Search in the state space
GK(N)
K
Gk(n)
fk(n-j)
k
k-1
Gk-1(j)
1
0
j
N
0
n
Start state
Gk(n) maxGk(0) fk(n),
Gk(1) fk(n-1), . . .
Gk(j-1) fk(j), . . .
Gk(n) fk(0)
Ak(n)jopt
35Optimal partition of data sequence
36Problem formulation
- Given a sequence of data Xx1, x2, ,xN
- Do partition of the sequence X into to K groups
with - given cost functions f(xi,xj) so that the total
value of - the cost function is minimal
37Partition into groups Example
- Data x 0 ?? lt x1 lt... lt xj lt ... lt xN
- Partition indices i0 0 lt i1 lt... lt ij lt ... lt
iM N.
1
2
3
(
(
(
...
(x0??) x1 x2 x3 x4
x5 x6 x7x8 x9 x10 x11 x12 x13
x14 xN
(i00) i14
i210
iK N15
K3
38Problem formulation
Cost function
Recursive equation
Initialization
39Search in the state space ?
GK(N)
K
b
State space ?
Gk (n)
f(j,n)
k
k-1
Gk-1(j)
0
j
N
1
n
Start state
Gk(n) minGk(k) f(k, n,
Gk(k) f(k1,n, . . .
Gk(j-1) f(j, n), . .
. Gk(n-1) f(n, n
Ak(n)jopt
40Scheme of the DP algorithm
- // Initialization
- FOR n 1 TO N DO G1(n) f(1,n
- // Minimum search
- FOR k 2 TO K DO
- FOR n k TO N DO
- dmin ? ?
- FOR j k-1 TO n-1 DO
- c ? Gk-1(j) f(j,n
- IF(c lt cmin)
- cmin ? c
- jmin ? j
- ENDIF
- ENDFOR
- Gk (n) ? dmin
- Ak (n) ? jmin
- ENDFOR
- ENDFOR
Complexity O(KN2)
41Backtrack in the state space ?
AK(N)
K
b
State space ?
0
N
1
n
j
Start state
S(M1) N FOR m K1 TO 2 DO S(m?1)
A(S(m), m)) P GK(N)
N22, K8 S22,18,14,12,9,6,4,3,1 (x0,x3,
(x3,x4, (x4,x6, (x6,x9, (x9,x12, (x12,x14,
(x14,x18, (x18,x22