26
Chapter 5 Dynamic Programming 충북대학교 알고리즘연구실

Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

Chapter 5

Dynamic Programming

충북대학교 알고리즘연구실

Page 2: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

Dynamic Programming

• Used when the solution of a problem is a result of a sequence of decisions

• Example

– Knapsack

– Shortest path

– Optimal merge patterns

Page 3: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.3 All pairs shortest paths

• G(V,E) : A directed graph with n vertices

• Ak(i,j) : Length of shortest path from i to j going through no vertex of index greater than k

• A(i,j) = min{min{Ak-1(i,k) + Ak-1(k,j)},cost(i,j)}

• Ak (i,j) = Ak-1(i,k) + Ak-1(k,j)

Ak (i,j) = min{Ak-1(i,j),Ak-1(i,k) + Ak-1(k,j)},k>=1

C(i,j) = 0, 1<=i<=n

C(i,j) Cost of edge if (i,j) E

if i j and (i,j) E ∞∈

∉≠

Page 4: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.3 All pairs shortest paths

• Figure 5.5 Graph with negative cycle

1 2 3 1 1

-2

∞∞−

0102

10

Page 5: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.3 All pairs shortest paths

• Program 5.3 Function to compute lengths of shortest paths

Page 6: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.3 All pairs shortest paths

• Figure 5.6 Directed Graph and associated matrices

Page 7: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.4 Single-source shortest paths: General weights

• dist[u] = cost[v][u]

• distl[u] = length of a shortest path from the source vertex V to vertex U under the constraint that the shortest path contains at most L edges

• 순환관계 – distk[u] = min(distk-1[u],min(distk-1[i] + cost[i][u]))

,2<=k<=n-1

Page 8: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.4 Single-source shortest paths: General weights

• Figure 5.10 Shortest paths with negative edge lengths

Page 9: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.4 Single-source shortest paths: General weights

• Program 5.4 Bellman and Ford algorithm to compute shortest paths

Page 10: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.5 Optimal binary search trees

• Definition – A binary search tree T • All identifies in the Tleft < Troot

• All identifies in the Tright < Troot

• The left and right subtres of T are also BST

• 가정 – a1 < a2 < … < an

– Ti,j : OBST for ai+1,…,aj

– Ci,j : cost for Ti,j

– Ri,j : root of Ti,j

– Weight of Ti,j : Wi,j = Qi + ∑+=

+j

ikkk PQ

1)(

Page 11: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.5 Optimal binary search trees

• P(i) : probability Search(a(i))

• Q(i) : probability search (a(i) < x < a(i+1))

• : Probability of an unsuccessful search

∑≤≤ ni

iQ0

)(

1)()(01

=+∴ ∑∑≤≤≤≤ nini

iQiPInternal node

External node

∑∑≤≤≤≤

−+ni

ini

i EleveliqaleveliP01

)1)((*)()(*)( (5.9)

Page 12: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

Optimal Binary Search Tree

Cost of the search Tree

p(k)+cost(l)+cost(r)+w(0,k-1)+w(k,n)

c(0, n) = min{c(0, k-1) + c(k, n) +p(k)

+w(0, k-1)+w(k, n)}

c(i, j) = min{c(i, k-1), c(k, j)+p(k) +

w(i,k-1)+w(k,j)}

C(i,j) = min{c(i,k-1)+c(k,j)} + w(i,j)

Page 13: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.5 Optimal binary search trees

• Figure 5.12 Two possible binary search trees

Page 14: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.5 Optimal binary search trees

• Figure 5.13 Binary search trees of Figure 5.12 with external nodes added

Page 15: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.5 Optimal binary search trees

Page 16: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.5 Optimal binary search trees

Page 17: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.5 Optimal binary search trees

• Figure 5.16 Computation of c(0,4), w(0,4), and r(0,4)

Page 18: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.8 Reliability design

• Solve a problem with a multiplicative optimization function

• Several devices are connected in series

• ri be the reliability of device Di

• Reliability of the entire system

• Duplicate : multiple copies of the same device type are connected in parallel use switching circuits

∏ ir

Page 19: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.8 Reliability design

Figure 5.19 n devices Di, 1<=i<=n, connected in series

Figure 5.20 Multiple devices connected in parallel in each stage

Page 20: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

Multiple copies

stage in contain mi copies of Di

P(all mi malfunction) = (1-ri)mi

Reliability of stage i =1-(1-ri)mi

Page 21: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.8 Reliability design

• Maximum allowable cost of the system Maximize

Subject to

Mi >=1 and integer, 1<=i<=n

• Assume ci>0 ui =

∏ ≤≤ ni ii m1

)(φ

cmcni

ii ≤∑≤≤1

−+ ∑ i

n

ji cccc /)(1

Page 22: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.9 The traveling salesperson problem

• 우체부 : n개의 틀린 장소에서 mail pickup

– n+1 vertex graph

– Edge <i,j> distance from i to j

– Tour of minimum cost

• Permutation problem

– n! different permutation of n object while there are 2n different subset of n object

n! > O(2n)

Page 23: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.9 The traveling salesperson problem

• Tour : simple path that starts and ends at vertex 1

• Every tour : edge<1,k> for some k v-{1} each <k,1>

• Optimal tour : path(k,1) – Shortest k to 1 path all the vertices in V-{1,k}

• Let g(i, S) be the length of a shortest path starting at vertex i, going through all vertices in S and terminating at vertex 1

})},1{,({min})1{,1( 12kVkgcVg knk

−+=−≤≤

})}{,({min),( jSjgcSig ijSj−+=

Page 24: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.9 The traveling salesperson problem

• Figure 5.21 Directed graph and edge length matrix c

Page 25: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.9 The traveling salesperson problem

• Thus g(2, ) = c21 = 5, g(3, ) = c31 = 6, and g(4, ) = c41 = 8. We obtain

g(2,{3}) = c23 + g(3, ) = 15 g(2,{4}) = 18

g(3,{2}) = 18 g(3,{4}) = 20

g(4,{2}) = 13 g(4,{3}) = 15

g(2,{3,4}) = min{c23+g(3,{4}),c24+g(4,{3})} = 25

g(3,{2,4}) = min{c32+g(2,{4}),c34+g(4,{2})} = 25

g(4,{2,3}) = min{c42+g(2,{3}),c43+g(3,{2})} = 23

g(1,{2,3,4}) = min{c12+g(2,{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}

= min(35,40,43}

= 35

φ

φφ φ

Page 26: Chapter 5 DYNAMIC PROGRAMMING - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016-09-09 · Dynamic Programming •Used when the solution of a problem is a

5.9 The traveling salesperson problem

• Let N be the number of g(i,s), that have to be computed before g(1,V-{1}) i, computed for each value of |s|

• n-1 choices of i

• The number of distinct sets of S of size k not including 1 and i

i

−k

n 2

22

02)1()

2)(1( −

=

−=−

−=∴ ∑ nn

kn

kn

nN

)2(lg 2 nnorithma θ=

)2()( nnOneedspace =