• 沒有找到結果。

假如我們有點記性的話…

N/A
N/A
Protected

Academic year: 2022

Share "假如我們有點記性的話…"

Copied!
30
0
0

加載中.... (立即查看全文)

全文

(1)

II

Michael Tsai 2011/3/25

(2)

上課!

(3)

假如我們有點記性的話…

空間換取時間

做過的事情就不要再做

“記得結果”就好: 填表與查表

Dynamic programming: 這裡的 programming指填表, 不是寫程式

所有不同的subprogram數目是 polynomial, 及

解掉subprogram的時間也是 polynomial, 

dynamic programming方法可以在 polynomial time內完成

(4)

Dynamic programming的做法

Top‐down with memoization

還是用遞迴的方式來做

但是每次做之前就先檢查是不是做過一樣的

如果沒做過就遞迴下去, 但是要記錄結果

如果做過就用查表取得之前的結果

Bottom‐up method

把所有的subprogram從小排到大

然後從小排到大來解

解一個subproblem時候, 所有它所需要的 subsubproblem都已經被解完了

請問哪一個快?

課本p.365有答案.

(5)

Cut‐Rod v0.1 beta (Top‐down)

Memoized_Cut_Rod(p,n)

let r[0..n] be a new array for i=0 to n

r[i]=-

return Memoized_Cut_Rod_Aux(p,n,r)

Memoized_Cut_Rod_Aux(p,n,r)

if 0

return r[n]

if n==0 q=0 else q=-

for i=1 to n

q=max(q,p[i]+Memoized_Cut_Rod_Aux(p,n-i,r)) r[n]=q

return q

(6)

Cut‐Rod v0.1 gamma (Bottom‐up)

Bottom_Up_Cut_Rod(p,n)

let r[0..n] be a new array r[0]=0

for j=1 to n q=-∞

for i=1 to j

q=max(q,p[i]+r[j-i]) r[j]=q

return r[n]

Θ

(7)

Subproblem graphs

4

3

2 1 0

1 0 0

0

2 1 0

1 0 0

0

4

3

2 1 0

Bottom‐up method: Reverse Topological Sort Subproblem x必須要考慮subproblem 之optimal solution <x,y>

Top‐down method: Depth First Search

(8)

Subproblem graphs

Subproblem graph的大小讓我們可以估計 dynamic programming演算法的執行時間

大小?

, . 我們指 和 .

=有幾個要解的subproblem (因為有記結果,  所以一個subproblem只要做一次)

| |=每個subproblem需要幾個比它小的 subproblem的結果

大致的估計: 和 和 成線性

(9)

最後的小問題

剛剛的程式只把可以拿多少錢算出來

沒有真的印出要怎麼切

如何解決?

用另外一個陣列(大小Θ )紀錄optimal solution 應該切的大小.

最後依序印出結果即可.

課本p. 369有解答.

(10)

連串矩陣相乘問題

Matrix multiplication is associative.

A

A

A

以上算出來答案都一樣

……

題目: 求 矩陣相乘之解.

.cols= .rows

and  are compatible.

(11)

連串矩陣相乘問題

Matrix-Multiply(A,B) if A.columns != B.rows

error “incompatible dimensions”

else let C be a new A.rows x B.cols matrix for i=1 to A.rows

for j=1 to B.cols

=0

for k=1 to A.cols

return C

主要花費時間在這邊!

p q

共花費pqr次乘法的時間

(12)

看一個例子

花費之乘法數目=10 100 5 10 5 50 7500

花費之乘法數目=100 5 50 10 100 50 75000

差十倍!!

10

100 100 5

5

50

(13)

連串矩陣相乘問題‐正式版

給一連串的矩陣 , , … , , 其中矩陣 的大 小為 ( 1, 2, … , , 找出一種乘法可 以使計算時的乘法數目最少

沒有真的要算結果, 而只是找出能最快算出結果 的方法.

因為算”怎麼算比較快”多花的時間, 比”用爛方 法直接算”多花的時間少很多

(14)

暴力法有多暴力

全部到底有幾種算法呢?

用遞迴定義:

上學期有講過喔~

P(n)之解為Catalan numbers, Ω , or is also Ω 2

神父,我有罪.

1 if  1,

if  2.

假設先這樣分:

(15)

所以不耍暴力了.

使用dynamic programming

正規步驟:

1. 找出最佳解的”結構”

2. 使用遞迴來定義最佳解的花費

3. 計算最佳解的花費

4. 使用已經計算的資訊來構築最佳解

(16)

找出最佳解的”結構”

總花費= . . 的花費+ . . 的花費+把 . . 和 . . 乘起來的花費

最佳解的結構: 假設有 . . 的最佳解, 此一方法為在k切一刀. 

則在此 . . 最佳解中,  . . 的相乘方法一定是 . . 的最 佳相乘方法

假設 . . 不是最佳解, 則我們可以在 . . 中把 . . 換成 更好的方法, 則可以找到一個更好的 . . 的相乘方法矛盾.

子問題的最佳解可以導出大問題的最佳解!

最後結論: 分成兩個子問題, 並嘗試所有可以切分的地方(k值)

在k切一刀

(17)

使用遞迴來定義最佳解的花費

遞迴定義:使用子問題最佳解的cost來定義大問 題最佳解的cost

定義:  , 為 . . 所需花的最少乘法數

.. .. 所花乘法數=

.. ..

.rows=

.cols=

.rows=

.cols=

(18)

使用遞迴來定義最佳解的花費

, , 1,

但是我們不知道最佳解中的k的值

因此我們必須找所有 , 1, … , 1

最後版本:

, 0

min , 1, if  ,

if  .

(19)

計算最佳解的花費

純recursive解法: exponential time

使用前面教的Bottom‐up填表方法: 每個不同的 subprogram僅需解1次

有幾個不同的問題?

1 , 幾個i和j的組合?

答案:  2 n Θ

(20)

計算最佳解的花費

如何決定填表的順序呢? (這次有i,j兩個變數)

, 0

min , 1, if  ,

if  .

1個矩陣相乘 個矩陣相乘

都小於 1個

我們可以把j‐i+1(也就是要相乘的matrix個數) 當作problem size的定義

(21)

n=p.length-1

let m[1..n,1..n] and s[1..n-1,2..n] be new tables

for i=1 to n m[i,i]=0 for l=2 to n

for i=1 to n-l+1 j=i+l-1

m[i,j]=∞

for k=i to j-1

q=m[i,k]+m[k+1,j]+p if q<m[i,j]

m[i,j]=q s[i,j]=k return m and s

大problem的解只會用到小problem的解.因此慢慢往上長.

邊界條件先設好.

把同樣problem size的所有i,j組合都依序做過

使用遞迴式找出最佳切點k

Θ

(22)

計算最佳解的花費

用一個例子來trace code比較快

Matrix

Dimension 30 x 35 35 x 15 15 x 5 5 x 10 10 x 20 20 x 25

(23)

使用已經計算的資訊來構築最佳 解

前面只印出了花費, 不是真正的解

怎麼乘才是最後的解

使用s陣列的資訊

(24)

使用已經計算的資訊來構築最佳 解

Print-Optimal-Parens(s,i,j) if i==j

print else

print “(“

Print-Optimal-Parens(s,i,s[i,j]) Print-Optimal-Parens(s,s[i,j]+1,j)

print “)”

(25)

看了兩個例子以後…

問: 一個問題要有什麼要件才能使用dynamic  programming?

答:

1. Optimal substructure

2. Overlapping Subproblems

(26)

什麼是Optimal substructure?

Definition: A problem exhibits  optimal substructure if an 

optimal solution to the problem  contains within it optimal 

solutions to subproblems.

怎麼尋找optimal substructure呢?

(27)

怎麼尋找optimal substructure呢?

1. 要得到問題的解答有許多選擇(砍在哪邊, 切在哪 邊), 而做這個選擇之後, 我們有一些subproblem要 解決.

2. 我們假設對於一個問題, 我們可以找到那個選擇

3. 知道這個選擇以後, 我們找出哪個subproblem可以 被拿來應用, 及剩下的問題(沒有對應到subproblem 的)怎麼解決

4. 證明大問題的最佳解中可以直接應用(剪下貼上)子 問題的最佳解. 

(28)

Optimal substructure越簡單越好

=? =?

=?

這一段砍下來就不再砍

成更小的了(拿去賣) 這一段是subproblem, 找遞迴朋友去解 versus

(29)

Optimal substructure越簡單越好

1

1

在k切一刀 假設把問題定義成 … 就好 (少一個變數)

此為一個子問題 此不為一個子問題!

除非k一直都是j‐1, 否則…

(30)

Optimal substructure的變化

1. 原始問題的最佳解用了多少個子問題

2. 大問題有多少選擇(選擇用不同的子問題們來 獲得最佳解)

大略來說, 以上兩者決定dynamic programming  algorithm的執行時間.

(之前說的Subproblem graphs是另外一種算法)

多少個子問題 有多少選擇 執行時間

鐵條資源回收 Θ n

連串矩陣相乘 Θ n‐1

參考文獻

相關文件

z Given user preferences and constraints on resource or utility, develop strategies to find the optimal adaptation operator(s) satisfying the constraints..

Greedy-Choice Property : making locally optimal (greedy) choices leads to a globally optimal

For machine learning applications, no need to accurately solve the optimization problem Because some optimal α i = 0, decomposition methods may not need to update all the

Using the solution U of Riemann problem to obtain an e approximate solution for the solution U of balance laws..

Specifically, for a locally optimal solution to the nonlinear second-order cone programming (SOCP), under Robinson’s constraint qualification, we establish the equivalence among

 Definition: A problem exhibits optimal subst ructure if an optimal solution to the proble m contains within it optimal solutions to su bproblems..  怎麼尋找 optimal

✓ Express the solution of the original problem in terms of optimal solutions for subproblems. Construct an optimal solution from

✓ Express the solution of the original problem in terms of optimal solutions for subproblems.. Construct an optimal solution from