演算法
Show, by means of a counterexample, that the following “greedy” strategy does not always determine an optimal way to cut rods. Define the density of a rod of length i to be pi / i, that is, its value per inch. The greedy strategy for a rod of length n cuts off a first piece of length i, where 1 ≤ i ≤ n, having maximum density. It then continues by applying the greedy strategy to the remaining piece of length n-i.
Answer:
給定一個n=3且p[3]={2,0,3}的價目表,分別代表n=1~3的時候的售價。依照greedy strategy來解決此題的話,n=1的時候有最大值2/1=2,因此依照此方法會將n=3的rod切成長度1與長度2的組合,這樣的收益會是2+0=2,但如果完全不切,長度n=3的rod收益是3,大於依照greedy strategy所得到的結果,因此greedy strategy在這裡不適用。
Consider a modification of the rod-cutting problem in which, in addition to a price pi for each rod, each cut incurs a fixed cost of c. The revenue associated with a solution is now the sum of the prices of the pieces minus the costs of making the cuts. Give a dynamic-programming algorithm to solve this modified problem.
Answer:
以下為改動過後的rod-cutting problem的程式碼
初始值:
遞回式: (在程式碼當中以Revenue[i]表示)
解:
Modify MEMORIZED-CUT-ROD to return not only the value but the actual solution, too.
Answer:
多開一個陣列s[]來記錄切rod的位置,s[i]代表長度i的rod切在s[i]的位置的時候會有最佳解,s[i]=i代表不用切
以下是程式碼
Find an optimal parenthesization of a matrix-chain product whose sequence of dimensions is《5, 10, 3, 12, 5, 50, 6》.
Answer:
題目給了7個dimensions,也就是說有6個矩陣,假設這些矩陣叫做
依據下面程式碼可以求得這六個矩陣相乘cost最少的方式
最少的cost為2010
相乘的方式為
A mathematical expression is given without parentheses. Design an algorithm to parenthesize the expression such that the value of the expression is maximized. For example, consider the expression: 2+7⋅5. There are two ways to parenthesize the expression 2+(7⋅5) = 37 and (2+7)⋅5 = 45, so in this case, your algorithm should output the second expression. Here, you may assume the given expressions contain only 3 kinds of binary operators ‘+’, ‘−’, and ‘⋅’.
Answer:
以下使用DP的方式解決此題,此題的optimal structure比較不同,因為一個算式的最大值不一定是由它最大的兩個子算式組合而成,其它也有可能,例如兩個很小的負數相乘可能會比兩個很大的正整數相乘更大。使用到的表格都有以註解方式寫在以下pseudo code當中。
輸入:E[n],長度為n的算式,E[i]可能為一個整數或者一個算符,長度都是1,例如E[i]=100,E[i]的長度也是1。
初始值:max[i][i]=E[i],min[i][i]=E[i],表示算式當中第i個數字到第i個數字的最大值跟最小值即為該數字。
遞迴式:
代表算符,可能為
解答:解答以recursive function的方式呼叫並列印。
Which is a more efficient way to determine the optimal number of multiplications in a matrix-chain multiplication problem: enumerating all the ways of parenthesizing the product and computing the number of multiplications for each, or running RECURSIVE-MATRIX-CHAIN? Justify your answer.
Answer:
我認為兩種方式一樣沒有效率,第一種是將所有可能的運算順序都做一遍然後比較,時間複雜度會和catalan number一樣。第二種方式是使用上述的Recursive-Martrix-Chain,看起來雖然有用一個表格紀錄運算結果,類似DP的方式,但每次進行一個新的function call的時候,m[i][j]都需要被重新計算一次,而找m[i][j]的方式也是將所有能切割的可能都試過一次,這樣跟第一種方式沒有不同,也是每一次計算一種括號的方式就將它以下的所有子問題都重新解一遍,時間複雜度同樣也會和catalan number一樣。
更新:Recurive method比較有效率
As stated, in dynamic programming we first solve the sub-problems and then choose which of them to use in an optimal solution to the problem. Professor Capulet claims that it is not always necessary to solve all the sub-problems in order to find an optimal solution. She suggests that an optimal solution to the matrix-chain multiplication problem can be found by always choosing the matrix Ak at which to split the sub-product (by selecting k to minimize the quantity ) before solving the sub-problems. Find an instance of the matrix-chain multiplication problem for which this greedy approach yields a sub-optimal solution.
Answer:
假設有三個矩陣,他們組成的dimensions是{5,10,3,4},依照DP的解法,運算順序應該是,但依照這個greedy approach的方法結果會是。