Dynamic Programming I

Embed Size (px)

DESCRIPTION

Dynamic Programming I. Michael Tsai 2013/10/3. 台大資訊鐵條回收公司. 回收鐵條 , 依長度算錢 . 假設你有一條很長的鐵條 , 長度為 n (n 為正整數 ) 可以 把長鐵條砍成長度為整數的短鐵條 請問如何砍才能夠用這條鐵條換到最多錢 ?. 價目表. 長度為 4 的鐵條. 8. 9. 1. 8. 1. 5. 1. 1. 5. 1. 1. 1. 1. 5. 5. 1. 5. 1. 1. 1. 價目表. 長度 為 n 的 鐵條. 長度為 n 的鐵條 , 共有幾種要檢查的 ? - PowerPoint PPT Presentation

Text of Dynamic Programming I

Data Structure and Algorithm II

Dynamic Programming IMichael Tsai2013/10/3

2123456789101589101717202430, ., n (n)?

4312345678910158910171720243098155111151151151181

n4

n5

6divide-and-conquer, ?

7i

.Optimal SubstructureOptimal solutions to a problem incorporate optimal solutions to related subproblems, which we may solve independently.

8

subproblem

9

()subprogram, Cut-Rod v0.1 alpha10, ?

n1, x2

Recursion tree114321001002100100!~12: Dynamic programming: programming, subprogrampolynomial, subprogrampolynomial, dynamic programmingpolynomial time13

Dynamic programmingTop-down with memoization, Bottom-up methodsubprogramsubproblem, subsubproblem?p.365.14Cut-Rod v0.1 beta (Top-down)15p: n: p: n: r: Cut-Rod v0.1 gamma (Bottom-up)16

Subproblem graphs17432100100210010043210Bottom-up method: Reverse Topological SortSubproblem xsubproblemoptimal solution directed edgeTop-down method: Depth First SearchSubproblem graphs18192021!pqqrpqr22101001005550-2324

.dynamic programming:2526k27272829

? (i,j)

30j-i+1(matrix)problem size31problemproblem...problem sizei,jk

trace code

32, s

3334: dynamic programming?:Optimal substructureOverlapping Subproblems35Optimal substructure?Definition: A problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems.

optimal substructure?36optimal substructure?(, ), , subproblem., , subproblem, (subproblem)().

37Optimal substructure38

()subproblem, versusOptimal substructure39k!kj-1, Optimal substructure(), dynamic programming algorithm.(Subproblem graphs)40