動態規劃
演算法方式總覽
1. The Divide-and-Conquer Strategy ( 個各擊破 )
binary Searching 、 Quick Sort….
2. The Greedy Method( 貪婪演算法 )
Prim MST 、 Kruskal MST 、 Djikstra's algorithm
3. Dynamic Programming( 動態演算法 )
二項是係數、矩陣連乘、最佳二元搜尋樹…
4. Trace Back( 回溯 )
圖形著色、漢米爾迴路問題… .
費氏數列 (Fibonacci sequence)
Fibonacci sequence: 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , … Fi = i if i 1
Fi = Fi-1 + Fi-2 if i 2
Solved by a recursive program:
Much replicated computation is done. It should be solved by a simple loop.
f2 f4 f3 f1 f3 f2 f1 f1 f0 f2 f1 f0 f1 f5 f0
Dynamic Programming
Dynamic Programming
is an
algorithm design method that can be
used when the solution to a problem
may be viewed as the result of a
sequence of decisions
動態規劃是一種演算法的設計於當ㄧ
個問題的解決方式是可以視為根據ㄧ
連續的結果而得到。
動態規劃介紹
1. 當ㄧ個問題可以被分解成數各的性質相同的
小問題。
2. 會先計算較小的問題的結果,並且存儲。
3. 如果有需要先前已經計算過的部份,就不需
要重新計算,直接從先前存儲的結果中獲得
。
4. 由最小的問題開始計算,循序向上求取最後
整個問提的答案。
5. 是一種由下而上( bottom-up )的解決問題
的方式。
動態規劃設計步驟
1. 建立一個遞迴機制,用它來求取ㄧ個問題經
過切割後,所產生較小但性質相同的問題解
。
2. 用 Bottom-up 的方式解題,首先由最小的問
題開始,逐步向上求取最後整各問題的解。
動態規劃 VS 個各擊破
相同 1. 將ㄧ個問題切成數個較小問題來解。 相異 1. 會先計算較小的問題並儲存計算結果 ( 動態規劃 ) 2. 有計算過的小問題就無須重複計算 ( 動態規劃 ) 3. Bottom-up 的方式 ( 動態規劃 ) 4. 盲目的遞迴計算 ( 個各擊破 )最短路徑 (The shortest path)
To find a shortest path in a multi-stage graph
Apply the greedy method :
the shortest path from S to T : 1 + 2 + 5 = 8 S A B T 3 4 5 2 7 1 5 6
The shortest path in multistage graphs
e.g.
The greedy method can not be applied to this case:
(S, A, D, T) 1+4+18 = 23. The real shortest path is:
(S, C, F, T) 5+2+2 = 9. S 2 B E 13 T 9 A 4 D C 2 F 1 5 11 5 16 18 2
動態規劃
Dynamic programming approach (forward approach):
d(S, T) = min{1+d(A, T), 2+d(B, T), 5+d(C, T)} S 2 B T A C 1 5 d(C, T) d(B, T) d(A, T) A 4 D d(A,T) = min{4+d(D,T), S 2 B E 13 T 9 A 4 D C 2 F 1 5 11 5 16 18 2
d(B, T) = min{9+d(D, T), 5+d(E, T), 16+d(F, T)} = min{9+18, 5+13, 16+2} = 18. d(C, T) = min{ 2+d(F, T) } = 2+2 = 4 d(S, T) = min{1+d(A, T), 2+d(B, T), 5+d(C, T)} = min{1+22, 2+18, 5+4} = 9. S 2 B E 13 T 9 A 4 D C 2 F 1 5 11 5 16 18 2
Backward approach
d(S, A) = 1 d(S, B) = 2 d(S, C) = 5 d(S,D)=min{d(S,A)+d(A,D), d(S,B)+d(B,D)} = min{ 1+4, 2+9 } = 5 d(S,E)=min{d(S,A)+d(A,E), d(S,B)+d(B,E)} = min{ 1+11, 2+5 } = 7 S 2 B E 13 T 9 A 4 D C 2 F 1 5 11 5 16 18 2 d(S,T) = min{d(S, D)+d(D, T), d(S,E)+ d(E,T), d(S, F)+d(F, T)} = min{ 5+18, 7+13, 7+2 } = 9 S 2 B E 13 T 9 A 4 D C 2 F 1 5 11 5 16 18 2
練習
0-1 背包問題
假設有 n 個物品,令:
S = {item1 , item2 , ... , itemn} wi = itemi 的重量 pi = itemi 的價值 W = 背包的最大載重 其中, wi 、 Pi 、 W 均為正整數,找出子集合 A 使得:
A item i A item i i i p W w 的限制下, 為最大值 在貪婪解法範例
(1) 先拿價值最高的。 (2) 先拿重量最輕的。 (3) 先拿「價值 / 重量」比率最高的。 5 磅 10 磅 20 磅 最 大 載 重 30 磅 5 磅 20 磅 10 磅 20 磅 $50 $60 $140 浪費 5 磅空間 7 $ 20 140 $ : 3 6 $ 10 60 $ : 2 10 $ 5 50 $ : 1 物品 物品 物品 拿取順序: 1 , 3 , 2 。Example
n objects , weight:
W1, W2, ,Wn profit:
P1, P2, ,Pn capacity:
M maximize:
subject to:
M xi = 0 or 1, 1in e. g.
ni i ix P 1
ni i ix W 1 i Wi Pi 1 10 40 2 3 20 3 5 30 M=10The multistage graph solution
The 0/1 knapsack problem can be described
by a multistage graph.
S T 0 1 0 10 00 01 100 010 011 001 0 0 0 0 0 0 40 0 20 0 30 0 0 30 x1=1 x1=0 x2=0 x2=1 x2=0 x3=0 x3=1 x3=0 x3=1動態規劃
1. The longest path represents the optimal solution: 2. x1=0, x2=1, x3=1
練習
資源分配問題
The resource allocation problem
m resources, n projects
profit Pi, j : j resources are allocated to project i. maximize the total profit.
Resource Project 1 2 3 1 2 8 9 2 5 6 7 3 4 4 4 4 2 4 5
The multistage graph solution
The resource allocation problem can be described as a multistage gra ph.
(i, j) : i resources allocated to projects 1, 2, …, j
e.g. node H=(3, 2) : 3 resources allocated to projects 1, 2.
S T 6 0,1 1,1 2,1 3,1 0,2 1,2 2,2 3,2 0,3 1,3 2,3 3,3 A 7 6 4 4 4 B C D H G F E I J K L 0 5 8 9 0 0 0 0 5 5 5 0 0 0 0 4 4 4 4 2 2 0
Find the longest path from S to T :
(S, C, H, L, T), 8+5+0+0=13
2 resources allocated to project 1. 1 resource allocated to project 2.
練習
準則
In summary, if a problem can be described by a multistage graph, then it can be solved by dyna mic programming.
如果一個問題可以轉成多階圖形問題
,那ㄧ定
二項式係數
二項式係數
(divide-and-conquer 版 )
遞迴推疊圖
(The recursive stack)
呼叫過程 : B2,1 B2,0 B3,2 B3,1 B4,2 B2,2 B1,1 B2,0 C4,2 B2,1 B1,1 B2,0