a09-chap15(3)-dp 算法导论 教学课件
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
8
BeihangSoft.cn
15.3.1 Optimal substructure Optimal substructure varies across problem domains:
How many choices ; How many subproblems
Ai Ai+1 … Ak Ak+1 Ak+2 … Aj
(动态规划法的特征)
Longest common subsequence (最长相同子序列) Optimal binary search trees (最优二叉搜索树)
1
BeihangSoft.cn
Optimal substructure (We have already known)
A1A2A3A4A5A6 ((A1(A2A3)) ((A4A5)A6))
It seems like it should. It does not.
Suppose p is longest simple path u v. p1 mustn’t be a longest simple path u w? p2 mustn’t be a longest simple path w v? No!
13
BeihangSoft.cn
15.3.1 Optimal substructure : Subtleties Shortest path has optimal substructure.
Suppose p is shortest path u v. Let w be any vertex on p. Let p1 be the portion of p, u w. Then p1 is a shortest path u w. Proof Suppose there exists a shorter path p’1, u
9
BeihangSoft.cn
15.3.1 Optimal substructure
Informally, running time depends on
(# of subproblems overall)×(# of choices for each subproblem)
Assembly-line scheduling Θ(n) subproblems, 2 choices for each⇒Θ(n) running time.
Cost of the problem solution = subproblem costs + cost directly attributable to the choice itself.
Matrix-chain mult百度文库plication
Ai Ai+1 … Ak Ak+1 Ak+2 … Aj
intermediate vertices w, then finding shortest paths u w and
w v.
14
BeihangSoft.cn
15.3.1 Optimal substructure : Subtleties Does longest simple path have optimal substructure?
11
BeihangSoft.cn
15.3.1 Optimal substructure
Dynamic programming uses optimal substructure bottom up. find optimal solutions to subproblems, choose which to use in optimal solution to the problem.
3
BeihangSoft.cn
BeihangSoft.cn
BeihangSoft.cn
15.3.1 Optimal substructure
How to characterize the space of subproblems?
Keep the space as simple as possible, Expand it as necessary.
BeihangSoft.cn
15 Dynamic Programming Scheduling two automobile assembly lines
(流水线、装配线调度)
Matrix-chain multiplication
(矩阵链相乘,或矩阵连乘问题)
Characteristics of dynamic programming
(选择子问题,使得原问题的最优解中包含此子问题的最优解)
Cost of the problem solution = subproblem costs + cost directly attributable to the choice itself.
(问题求解的开销 = 子问题的开销 + 与选择子问题相关的开销)
subproblem: optimal parenthesizations of AiAi+1… Ak and Ak+1Ak+1… Aj ,
cost attributable to the choice: pi-1pk pj .
12
BeihangSoft.cn
15.3.1 Optimal substructure : Subtleties
Optimal substructure doesn’t apply to all optimization problems.
Here are two problems that look similar. In both, we’re given an unweighted, directed graph G = (V, E ). (无权有向图) V is a set of vertices; E is a set of edges.
w. cut out p1,
replace it with p’1, get path
with fewer edges than p.
Same argument applies to p2. Therefore, we can find shortest path u v by considering all
15.3.1 Optimal substructure
Dynamic programming uses optimal substructure bottom up. find optimal solutions to subproblems,(求子问题的最优解) choose which to use in optimal solution to the problem.
Mmatrix-chain multiplication Ai Ai+1…Aj j-i choices We choose from among j-i candidates for the index k. two optimal subproblems
For a given matrix Ak , two subproblems: parenthesizing AiAi+1…Ak and parenthesizing Ak+1Ak+2…Aj , How to solve both of them optimally?
A1 A2 … Ak Ak+1 Ak+2 … Aj
Matrix-chain multiplication constrain subproblem space to matrix products A1A2…Aj , an optimal parenthesization split this product between Ak and Ak+1 for some 1≤k<j , we had subproblems of the form A1A2…Ak and Ak+1Ak+2…Aj , the latter subproblem is not of the form A1A2…Aj , it was necessary to allow our subproblems to vary at “both ends”, that is, to allow both i and j to vary in the subproblem AiAi+1…Aj .
Assembly-line scheduling subproblems: fastest way through stations S1, j-1 and S2, j-1, cost attributable to the choice: ai, j if we stay on the same line; or ti′,j-1 + ai,j , where i′≠ i, if we switch.
2
BeihangSoft.cn
15.3 Elements of dynamic programming When the dynamic-programming method applies? Mentioned already:
optimal substructure overlapping subproblems
Matrix-chain multiplication Ai Ai+1 … Ak Ak+1 Ak+2 … Aj Θ(n2) subproblems overall, at most n-1 choices for each ⇒ O(n3) running time.
10
BeihangSoft.cn
After the choice, how many optimal subproblems are shown in an optimal solution to the original problem.
Assembly-line scheduling two choices (for Si, j use either S1, j-1 or S2, j-1) one optimal subproblem
7
BeihangSoft.cn
15.3.1 Optimal substructure
Optimal substructure varies across problem domains:
How many choices in determining which subproblem(s) to use.
Examples: Assembly-line scheduling
space of subproblems : fastest way from factory entry through stations S1, j and S2, j ,
(子空间:从入口处开始,通过 S1,j , S2,j 的最快方式)
no need to try a more general space of subproblems.
6
BeihangSoft.cn
15.3.1 Optimal substructure
How to characterize the space of subproblems? simple, expand
Finding a path from vertex u to vertex v. Shortest path: find path u to v with fewest edges. Must be simple (no cycles), since removing a cycle from a path gives a path with fewer edges. (最短路径) Longest simple path: find simple path u to v with most edges. If didn’t require simple, could repeatedly traverse a cycle to make an arbitrarily long path. (最长简单路径)
BeihangSoft.cn
15.3.1 Optimal substructure Optimal substructure varies across problem domains:
How many choices ; How many subproblems
Ai Ai+1 … Ak Ak+1 Ak+2 … Aj
(动态规划法的特征)
Longest common subsequence (最长相同子序列) Optimal binary search trees (最优二叉搜索树)
1
BeihangSoft.cn
Optimal substructure (We have already known)
A1A2A3A4A5A6 ((A1(A2A3)) ((A4A5)A6))
It seems like it should. It does not.
Suppose p is longest simple path u v. p1 mustn’t be a longest simple path u w? p2 mustn’t be a longest simple path w v? No!
13
BeihangSoft.cn
15.3.1 Optimal substructure : Subtleties Shortest path has optimal substructure.
Suppose p is shortest path u v. Let w be any vertex on p. Let p1 be the portion of p, u w. Then p1 is a shortest path u w. Proof Suppose there exists a shorter path p’1, u
9
BeihangSoft.cn
15.3.1 Optimal substructure
Informally, running time depends on
(# of subproblems overall)×(# of choices for each subproblem)
Assembly-line scheduling Θ(n) subproblems, 2 choices for each⇒Θ(n) running time.
Cost of the problem solution = subproblem costs + cost directly attributable to the choice itself.
Matrix-chain mult百度文库plication
Ai Ai+1 … Ak Ak+1 Ak+2 … Aj
intermediate vertices w, then finding shortest paths u w and
w v.
14
BeihangSoft.cn
15.3.1 Optimal substructure : Subtleties Does longest simple path have optimal substructure?
11
BeihangSoft.cn
15.3.1 Optimal substructure
Dynamic programming uses optimal substructure bottom up. find optimal solutions to subproblems, choose which to use in optimal solution to the problem.
3
BeihangSoft.cn
BeihangSoft.cn
BeihangSoft.cn
15.3.1 Optimal substructure
How to characterize the space of subproblems?
Keep the space as simple as possible, Expand it as necessary.
BeihangSoft.cn
15 Dynamic Programming Scheduling two automobile assembly lines
(流水线、装配线调度)
Matrix-chain multiplication
(矩阵链相乘,或矩阵连乘问题)
Characteristics of dynamic programming
(选择子问题,使得原问题的最优解中包含此子问题的最优解)
Cost of the problem solution = subproblem costs + cost directly attributable to the choice itself.
(问题求解的开销 = 子问题的开销 + 与选择子问题相关的开销)
subproblem: optimal parenthesizations of AiAi+1… Ak and Ak+1Ak+1… Aj ,
cost attributable to the choice: pi-1pk pj .
12
BeihangSoft.cn
15.3.1 Optimal substructure : Subtleties
Optimal substructure doesn’t apply to all optimization problems.
Here are two problems that look similar. In both, we’re given an unweighted, directed graph G = (V, E ). (无权有向图) V is a set of vertices; E is a set of edges.
w. cut out p1,
replace it with p’1, get path
with fewer edges than p.
Same argument applies to p2. Therefore, we can find shortest path u v by considering all
15.3.1 Optimal substructure
Dynamic programming uses optimal substructure bottom up. find optimal solutions to subproblems,(求子问题的最优解) choose which to use in optimal solution to the problem.
Mmatrix-chain multiplication Ai Ai+1…Aj j-i choices We choose from among j-i candidates for the index k. two optimal subproblems
For a given matrix Ak , two subproblems: parenthesizing AiAi+1…Ak and parenthesizing Ak+1Ak+2…Aj , How to solve both of them optimally?
A1 A2 … Ak Ak+1 Ak+2 … Aj
Matrix-chain multiplication constrain subproblem space to matrix products A1A2…Aj , an optimal parenthesization split this product between Ak and Ak+1 for some 1≤k<j , we had subproblems of the form A1A2…Ak and Ak+1Ak+2…Aj , the latter subproblem is not of the form A1A2…Aj , it was necessary to allow our subproblems to vary at “both ends”, that is, to allow both i and j to vary in the subproblem AiAi+1…Aj .
Assembly-line scheduling subproblems: fastest way through stations S1, j-1 and S2, j-1, cost attributable to the choice: ai, j if we stay on the same line; or ti′,j-1 + ai,j , where i′≠ i, if we switch.
2
BeihangSoft.cn
15.3 Elements of dynamic programming When the dynamic-programming method applies? Mentioned already:
optimal substructure overlapping subproblems
Matrix-chain multiplication Ai Ai+1 … Ak Ak+1 Ak+2 … Aj Θ(n2) subproblems overall, at most n-1 choices for each ⇒ O(n3) running time.
10
BeihangSoft.cn
After the choice, how many optimal subproblems are shown in an optimal solution to the original problem.
Assembly-line scheduling two choices (for Si, j use either S1, j-1 or S2, j-1) one optimal subproblem
7
BeihangSoft.cn
15.3.1 Optimal substructure
Optimal substructure varies across problem domains:
How many choices in determining which subproblem(s) to use.
Examples: Assembly-line scheduling
space of subproblems : fastest way from factory entry through stations S1, j and S2, j ,
(子空间:从入口处开始,通过 S1,j , S2,j 的最快方式)
no need to try a more general space of subproblems.
6
BeihangSoft.cn
15.3.1 Optimal substructure
How to characterize the space of subproblems? simple, expand
Finding a path from vertex u to vertex v. Shortest path: find path u to v with fewest edges. Must be simple (no cycles), since removing a cycle from a path gives a path with fewer edges. (最短路径) Longest simple path: find simple path u to v with most edges. If didn’t require simple, could repeatedly traverse a cycle to make an arbitrarily long path. (最长简单路径)