design and analysis of algorithms lecture dynamic programming alexander kolesnikov 17.10.205

41
Design and Analysis of Algorithms Lecture Dynamic programming Alexander Kolesnikov 17.10.205 DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF JOENSUU JOENSUU, FINLAND

Upload: mufutau-reeves

Post on 02-Jan-2016

34 views

Category:

Documents


0 download

DESCRIPTION

DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF JOENSUU JOENSUU, FINLAND. Design and Analysis of Algorithms Lecture Dynamic programming Alexander Kolesnikov 17.10.205. List of sample problems. Shortest path in trellis graph; Optimal allocation of constrained resource; - PowerPoint PPT Presentation

TRANSCRIPT

Design and Analysis of Algorithms

Lecture

Dynamic programmingAlexander Kolesnikov

17.10.205

DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF JOENSUU JOENSUU, FINLAND

List of sample problemsList of sample problems

• Shortest path in trellis graph;Shortest path in trellis graph;

• Optimal allocation of constrained resource; Optimal allocation of constrained resource;

• Optimal sequence partition (k-link shortest Optimal sequence partition (k-link shortest

path).path).

• to be continued ...

Shortest path in trellis graphShortest path in trellis graph

Stagecoach problemStagecoach problem

A traveler wishes to minimize the length of a journey from town A A to JJ.

Greedy algorithmGreedy algorithm

The length of the route A-B-F-I-J: 2+4+3+4=13.

Can we find shorter route?

Exhaustive search: try all Exhaustive search: try all

Route A-D-F-I-J: 3+1+3+4=11

The total number of routes to be tested: 3321=18

Can we avoid exhaustive search?

Shortest path construction: 1st stage (B)

?

Shortest path construction: 1st stage

S(A,B)=2

S(A,C)=4

S(A,D)=3

Shortest path construction: 2nd stage (E)

1. (A..B)-E: 2+7=9

2. (A..C)-E: 4+3=7

3. (A..D)-E: 3+4=7

----------------------

(A..C)-E: 7

?

Shortest path construction: 2nd stage (E)

1. (A..B)-E: 2+7=9

2. (A..C)-E: 4+3=7 *)

3. (A..D)-E: 3+4=7

----------------------

(A..C)-E: 7

Shortest path construction: 2nd stage (F)

1. (A..B)-F: 2+4=6

2. (A..C)-F: 4+2=6

3. (A..D)-F: 3+1=4 *)

----------------------

(A..C)-F: 4

?

Shortest path construction: 2nd stage (F)

1. (A..B)-F: 2+4=6

2. (A..C)-F: 4+2=6

3. (A..D)-F: 3+1=4 *)

----------------------

(A..D)-F: 4

Shortest path construction: 2nd stage (G)

1. (A..B)-G: 2+6=8 *)

2. (A..C)-G: 4+6=10

3. (A..D)-G: 3+5=8

----------------------

(A..B)-G: 8?

Shortest path construction: 2nd stage (G)

1. (A..B)-G: 2+6=8 *)

2. (A..C)-G: 4+6=10

3. (A..D)-G: 3+5=8

----------------------

(A..B)-G: 8

Shortest path construction: 3rd stage (H)

1. (A..E)-H: 7+1=8 *)

2. (A..F)-H: 4+6=10

3. (A..G)-H: 5+3=8

----------------------

(A..E)-H: 5

Shortest path construction: 3rd stage (H)

1. (A..E)-H: 7+1=8 *)

2. (A..F)-H: 4+6=10

3. (A..G)-H: 5+3=8

----------------------

(A..E)-H: 5

Shortest path construction: 3rd stage (I)

1. (A..E)-I: 7+4=11

2. (A..F)-I: 4+3=7 *)

3. (A..G)-I: 5+3=8

----------------------

(A..F)-I: 7

Shortest path construction: 3rd stage (I)

1. (A..E)-I: 7+4=11

2. (A..F)-I: 4+3=7 *)

3. (A..G)-I: 5+3=8

----------------------

(A..F)-I: 7

Shortest path construction: 4th stage (J)

1. (A..H)-J: 8+3=11 *)

2. (A..I) -J: 7+4=11

----------------------

(A..H)-J: 11

Shortest path construction: 4th stage (J)

1. (A..H)-J: 8+3=11 *)

2. (A..I) -J: 7+4=11

----------------------

(A..H)-J: 11

Backtrack the shortest path

11

8

7

8

4

7

3

4

2

0

The shortest path

11

8

7

8

4

7

3

4

2

0

Route A-C-E-H-J: 4+3+1+3=11

Trellis graphTrellis graph

1 2 3 4 K-1 K

),(),,1( jkikf ),(),,1( jkikf

Trellis graphTrellis graph

• Distance (weight) from point i1 at stage (j1) to point i2

at stage j:

min),(),,1(1

1

K

jjj ijijfL

•The total value of cost function:

Principle of optimality of BellmanPrinciple 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.

Dynamic programmingDynamic programming

• Initialization:

),(),,1()(min)( nkikfiGnG ki

k • Recursive eqution:

• Cost function:

k

jjjjjk ipipfnG

111 )(),(min)(

0)(0 nG

ComplexityComplexity

• 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

Optimal allocation Optimal allocation of constrained resourceof constrained resource

Problem formulationProblem 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:

K

jjj nfP

1

max)(

K

jj Nn

1

subject to: 0jn

Dynamic programming formulationDynamic programming formulation

k

jjj

nnnk nfnG

j 1...1

max)(

.0;)(max)( 10

NnmnfmGnG kknm

k

NnnG 0;0)(0

Initialization:

Recursive equation:

Optimal value function:

)(NGF K

Allocate 3 mln euros into four projectsAllocate 3 mln euros into four projects

x Proj #1 Proj #2 Proj #3 Proj #40 0 0 0 01 2 5 4 22 8 6 4 43 9 7 4 5

Profit fk(x), K=3, N=3.

4

1

max)(j

jj nfP

4

1

3j

jn

Trellis graphTrellis graph

SolutionSolution

#1: 2; f1(2)=8#2: 1; f2(1)=5#3: 0; f3(0)=0#4: 0; f4(0)=0---------------------- N=3; G4(3)=13

*)

Search in the state space

K

N00

n

k

j

GGkk(n)(n)

GGk-1k-1(j)(j)

GGKK(N)(N)

Start state

fk(n-j)

Gk(n) = max{Gk(0) + fk(n), Gk(1) + fk(n-1), . . . Gk(j-1)+ fk(j),

. . . Gk(n) + fk(0)}

k-1 *

Ak(n)=jopt

1

Optimal partition of Optimal partition of data sequencedata sequence

Problem formulationProblem formulation

Given a sequence of data X={x1, 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:

K

jii jj

xxf1

min),(1

min),(...),(),(12111

Niiii xxfxxfxxfK

Partition into groups: Example• Data: x 0= < x1 <... < xj < ... < xN

• Partition indices: i0= 0 < i1 <... < ij < ... < iM =N. • Groups:

• • • • •• • • •• •• • • •• ... ]( ]( ](#1 #2 #3

(x0=) x1 x2 x3 x4 x5 x6 x7x8 x9 x10 x11 x12 x13 x14 xN

(i0=0) i1=4 i2=10 iK =N=15

15,1010,44,0 fffP

K=3

Problem formulationProblem formulation

k

jjjjk niiifnG

11 .;,)(

Recursive equation:

Cost function:

.0;,)(min)( 11

NnnjfjGnG knjk

k

.0;0)(0 NnnG

Initialization:

Search in the state space

K

N10

b

n

k

j

State space

Gk (n)

Gk-1(j)

GGKK(N)(N)

Start state

f(j,n)

Gk(n) = min{Gk(k) + f(k, n], Gk(k) + f(k+1,n], . . . Gk(j-1) + f(j, n]*),

. . . Gk(n-1) + f(n, n]}

k-1 *

Ak(n)=jopt

Scheme of the DP algorithm// Initialization

FOR n = 1 TO N DO GG11(n)(n)= f(1,n]// Minimum searchFOR k = 2 TO K DO FOR n = k TO N DO

dmin FOR j= k-1 TO n-1 DO c GGk-1k-1(j)(j) + f(j,n]

IF(c < cmin)

cmin c;

jmin j ENDIF ENDFOR GGk k (n)(n) dmin

AAk k (n)(n) jmin

ENDFORENDFOR

Complexity: O(KN2)Complexity: O(KN2)

Backtrack in the state space

K

N10

b

nj

State space

AK(N)

Start state

S(M+1)= NFOR m = K+1 TO 2 DO S(m1) = A(S(m), m))P = GGKK(N)(N)

S(M+1)= NFOR m = K+1 TO 2 DO S(m1) = A(S(m), m))P = GGKK(N)(N)

N=22, K=8: S={22,18,14,12,9,6,4,3,1}

(x0,x3], (x3,x4], (x4,x6], (x6,x9], (x9,x12],

(x12,x14], (x14,x18], (x18,x22]