当前位置:文档之家› 算法导论 第十二章 动态规划

算法导论 第十二章 动态规划


Optimal structure最优子结构


Optimal substructure does not apply to all optimization problems Example. Given a directed graph G = (V, E), and a pair of vertices u and v Shortest path find a path u ~> v with the fewest edges Longest path find a path u ~> v with the most edges Shortest path has optimal substructure



Matrix-chain multiplication矩阵链乘
• Goal. Given a sequence of matrices A1,A2,…,An, find an optimal order of multiplication
• Multiplying two matrices of dimension p×q and q×r, takes time which is dominated by the number of scalar multiplication, which is p×q×r
A recursive solution递归定义最优解
• Define m[i,j] as the minimum number of scalar multiplications needed to compute the matrix product Ai..j (We want the value of m[1,n])标量乘法运算次数的最小值
• What if we check all possible ways of multiplying? How many ways of parenthesizing are there?计算括号法方案数 • P(n): number of way of parenthesizing. Then P(1) =1 and for n≥2
Optimal substructure

Longest path does not have optimal substructure


Longest path from q to t: q → r → t. Longest path from r to t: r → q → s → t, which is not contained in the longest path from q to t. Difference between shortest and longest path: Shortest path has independent subproblem (solution to one problem does not depend on the other). If (p1 = uw)(p2 = wv) is a shortest path, then p1 and p2 cannot share any vertex other than w
Matrix-Chain Multiplication DP Algo.
• O(n3)
Example: DP for CMM
• The optimal solution is ( A1 ( A2 A3 ) ) ( ( A4 A5 ) A6 )
Construct an Optimal Solution构造一个最优解
Recursively define the value of the optimal solution递归定义最优解的值 Compute the value of the solution in a bottomup fashion自底向上计算一个最优解的值 Construct the optimal computed information solution using the
• We would like to find the split that uses the minimum number of multiplications. Thus,
• To obtain the actual parenthesization, keep track of the optimal k for each pair (i,j) as s[i,j].
Dynamic programming


动态规划(dynamic programming)是运筹学的一 个分支,20世纪50年代初美国数学家R.E.Bellman等 人 在 研 究 多 阶 段 决 策 过 程 (Multistep decision process)的优化问题时,提出了著名的最优性原理, 把多阶段过程转化为一系列单阶段问题,逐个求解, 创立了解决这类过程优化问题的新方法——动态规 划 多阶段决策问题:求解的问题可以划分为一系列相 互联系的阶段,在每个阶段都需要做出决策,且一 个阶段决策的选择会影响下一个阶段的决策,从而 影响整个过程的活动路线,求解的目标是选择各个 阶段的决策是整个过程达到最优
• Generally. Ai has dimension pi-1×pi and we’d like to minimize the total number of scalar multiplications
Example
• Order 1
• Order 2
Brute force method穷举法

Proof. If there’s a shorter path from u to w, call it p’1, then p’1p2 contains a shorter path than p1p2 from u to v, which contradicts the assumption.
Dynamic programming(动态规 划)
(高级设计和分析技术)
主要内容
•Dynamic programming 动态规划 •Matrix-chain multiplication矩阵链乘法 •Elements of dynamic programming动态规 划要素 •Longest common subsequence最长公共子 序列
Elements of DP Algorithms

For matrix-chain multiplication

There were Θ (n2) subproblems overall, and in each we had at most n –1 choices, giving an O(n3) running time.
• Notation. Ai..j represents Ai…Aj 求值的结果
• Any parenthesization of Ai..j where i < j must split into two products of the form Ai..k and Ak+1..j.
• Optimal substructure. If the optimal parenthesization splits the product as Ai..k and Ak+1..j, then parenthesizations within Ai..k and Ak+1..j must each be optimal
• The final matrix multiplication in computing A1..n optimally is A1..s[1,n]As[1,n]+1..n. s[1,s[1,n]] determines the last matrix multiplication in computing A1..s[1,n] and s[s[1,n]+1,n] determines the last matrix multiplication in computing As[1,n]+1..n.
Dynamic programming

动态规划的思想实质是分治思想和解决冗余 动态规划算法与分治法类似,其基本思想也 是将待求解问题分解成若干个子问题
Dynamic programming

动态规划的思想实质是分治思想和解决冗余 动态规划算法与分治法类似,其基本思想也 是将待求解问题分解成若干个子问题

Opti problem domains in two ways:

across

How many subproblems are used in an optimal solution to the original problem How many choices we have in determining which subproblem(s) to use in an optimal solution
An optimal parenthesization’s structure最优括号化结构
• If the optimal parenthesization of A1 × A2 × … ×An is split between Ak and Ak+1, then
• The only uncertainty is the value of k --Try all possible values of k. The one that returns the minimum is the right choice

If i = j, there is nothing to do, so that m[i,j] = 0;
相关主题