L17&L18 DP
Outline:
- Basic Idea of Dynamic Programming(DP)
- Smart scheduling of subproblems
- Minimum Cost Matrix Multiplication
- BF1, BF2
- A DP solution
- Weighted Binary Search Tree
- The "same" DP with matrix multiplication
- From the DP perspective
- All-pairs shortest paths SSSP over DAG
- More DP problems
- Edit distance
- Highway restaurants; Separating Sequence of words
- Changing coins
- Elements of DP
Ref:
- 算法设计与分析(Algorithm design and analysis) by 黄宇
Basic Idea of DP
- Smart recursion
- Compute each subproblem only once
- Basic process of a "smart" recursion
- Find a reverse topological order for the subproblem graph
- In most cases, the order can be determined by partial knowledge of the problem.
- General method based on DFS is available.
- Scheduling the subproblems according to the reverse topological order
- Record the subproblem solutions in a dictionary.
- Find a reverse topological order for the subproblem graph
Minimum Cost Matrix Multiplication
BF1
- 总是在某个位置开始第一次乘法
1 | mmTry1(dim,len,seq) |
BF2
- 必然在某个位置k相乘
- BF1“总是在某个位置开始第一次乘法”,子问题规模下降过慢
1 | mmTry2(dim, low,high) |
A DP solution
1 | matrixOrder(n,cost,last) //last记的是位置 |
- 时间
- 空间
Weighted Binary Search Tree
- 规定
, 其中 , 是节点i被访问到的概率。 如何优化WBST使得A(T)最小?
Problem Rephrased
- Subproblem identification
- The keys are in sorted order
- Each subproblem can be identified as a pair of index (low,high)
- Expected solution of the subproblem
- For each key
, a weight is associated. is the possibility that the key is searched for
- The subproblem (low,high) is to find the binary search tree with minimum weighted retrieval cost
- For each key
minimum weighted retrieval cost
- A(low,high,r) is the minimum weighted retrieval cost for subproblem (low,high) where
is chosen as the root of its BST - A(low,high) is the minimum weighted retrieval cost for subproblem (low,high) over all choices of the root key
- p(low,high), equal to
is the weight of the subproblem (low,high)- p(low,high) is the possibility that the key searched for is in this interval.
Subproblem solutions
Weighted retrieval cost of a subtree
T contains
, and the weighted retrieval cost of R is W, with T being a whole tree.As a subtree with the root at level 1, the weighted retrieval cost of T will be :
是子问题并入大问题时所付出的代价(修正量),即 “whole tree”变成子树所付出的修正量
根
的代价是
So, the recursive relations are:
1 | optimalBST( prob,n,cost,root ) |
1 | bestChoice(prob,cost,root,low,high) |
From the DP perspective
All-pairs shortest paths SSSP over DAG
Highway restaurants
- Highway restaurants
- n possible locations on a straight line
- At most one restaurant at one location
- Expected profit for location i is
- Expected profit for location i is
- Any two restaurants should be at least k miles apart
- n possible locations on a straight line
- How to arrange the restaurants
- To obtain the maximum expected profit
- The recursion
- P(j): the max profit achievable using only first j locations 只开若干个餐厅,其中最大序号为j, 所获得的利润
- P(0) = 0
- prev[j]: largest index before j and k miles away
- P(j): the max profit achievable using only first j locations 只开若干个餐厅,其中最大序号为j, 所获得的利润
- One dimension DP algorithm
- Fill in P[0],P[1], ... , P[n]
1 | (First compute the prev[.] array) //预处理 |
Separating Sequence of words
- Words into lines:
- Word-length
and line-width: W
- Word-length
- Basic constraint
- If
are in one line, then
- If
- Penalty for one line: some function of X, X is:
- 0 for the last line in a paragraph, and
for other lines
- The problem
- How to make the penalty of the paragraph, which is the sum of the penalties of individual lines, minimized
1 | LineBreakDP |
Analysis of LineBreakDP
- Each subproblem is identified by only one integer k, for (k,n)
- Number of vertex in the subproblem graph: at most n
- So, in DP version, the recursion is executed at most n times.
- So, the running time is in
- The loop is executed at most W/2 times. //每个单词后都有空格,标点
- In fact, W, the line width, is usually a constant. So,
- The extra space for the dictionary is
Changing coins
How to pay a given amount of money?
贪心不行
Subproblems
Assumptions
- Given n different denotations
- A coin of denomination i has
units 面额为i的硬币代表了 的金钱 - The amount to be paid: N
Subproblems [i,j]
- The minimum number of coins required to pay an amount of j units, using only coins of denominations 1 to i.
The problem
- Figure out subproblems [ n, N ] ( as c[n, N] )
易得:
c[i,0] is 0 for all i
1 | int coinChange( int N, int n, int[] coin) |
Elemens of DP
Principle of Optimality
- 重叠子问题
- 蛮力找最优
- Optimal substructure: 大问题的最优解必然由小问题的最优解组合而成.
- Given an optimal sequence of decisions, each subsequence must be optimal by itself
- Positive example: shortest path
- Counter example: longest (simple) path
- DP relies on the principle of optimality