動態規劃 dynamic programming
Post on 09-Jan-2016
144 Views
Preview:
DESCRIPTION
TRANSCRIPT
1
動態規劃Dynamic Programming
2
演算法方式總覽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( 回溯 ) 圖形著色、漢米爾迴路問題… .
5. Tree Searching Strategy( 樹的追蹤 )
3
費氏數列 (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
4
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
動態規劃是一種演算法的設計於當ㄧ個問題的解決方式是可以視為根據ㄧ連續的結果而得到。
5
動態規劃介紹1. 當ㄧ個問題可以被分解成數各的性質相同的
小問題。2. 會先計算較小的問題的結果,並且存儲。3. 如果有需要先前已經計算過的部份,就不需
要重新計算,直接從先前存儲的結果中獲得。4. 由最小的問題開始計算,循序向上求取最後
整個問提的答案。5. 是一種由下而上( bottom-up )的解決問題
的方式。
6
動態規劃設計步驟1. 建立一個遞迴機制,用它來求取ㄧ個問題經
過切割後,所產生較小但性質相同的問題解。
2. 用 Bottom-up 的方式解題,首先由最小的問題開始,逐步向上求取最後整各問題的解。
7
動態規劃 VS 個各擊破 相同1. 將ㄧ個問題切成數個較小問題來解。
相異1. 會先計算較小的問題並儲存計算結果 ( 動態規劃 )
2. 有計算過的小問題就無須重複計算 ( 動態規劃 )
3. Bottom-up 的方式 ( 動態規劃 )
4. 盲目的遞迴計算 ( 個各擊破 )
8
最短路徑 (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
9
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 T132
B E
9
A D4
C F2
1
5
11
5
16
18
2
10
動態規劃 Dynamic programming approach (forward approach):
d(S, T) = min{1+d(A, T), 2+d(B, T), 5+d(C, T)}
S T2
B
A
C
1
5d(C, T)
d(B, T)
d(A, T)
A
T
4
E
D
11d(E, T)
d(D, T) d(A,T) = min{4+d(D,T), 11+d(E,T)} = min{4+18, 11+13} = 22.
S T132
B E
9
A D4
C F2
1
5
11
5
16
18
2
11
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 T132
B E
9
A D4
C F2
1
5
11
5
16
18
2
12
Backward approach
d(S, A) = 1d(S, B) = 2d(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 d(S,F)=min{d(S,B)+d(B,F), d(S,C)+d(C,F)} = min{ 2+16, 5+2 } = 7
S T132
B E
9
A D4
C F2
1
5
11
5
16
18
2
13
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 T132
B E
9
A D4
C F2
1
5
11
5
16
18
2
14
練習
S T132
B
2
A D4
C F2
1
5
11
16
1
2
Find out the shortest path in the following graph.
15
0-1 背包問題假設有 n 個物品,令:
S = {item1 , item2 , ... , itemn}wi = itemi 的重量pi = itemi 的價值W = 背包的最大載重
其中, wi 、 Pi 、 W 均為正整數,找出子集合 A 使得:
Aitemi
Aitemi
ii
pWw 為最大值的限制下,在
16
貪婪解法範例(1) 先拿價值最高的。(2) 先拿重量最輕的。(3) 先拿「價值 / 重量」比率最高的。
5 磅10 磅
20 磅
最大載重
30 磅5 磅
20 磅
10 磅
20 磅
$50
$60
$140
浪費 5 磅空間
貪婪解法
最佳解背包物品 1 物品 2 物品 3
7$20
140$:3
6$10
60$:2
10$5
50$:1
物品
物品
物品
拿取順序: 1 , 3 , 2 。
17
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
iixP1
ni
iixW1
i Wi Pi 1 10 40 2 3 20 3 5 30
M=10
18
The multistage graph solutionThe 0/1 knapsack problem can be described
by a multistage graph.
S T
0
1 0
10
00
01
100
010
011
000
001
0
0
0
0
00
40
020
0
30
0
0
30
x1=1
x1=0
x2=0
x2=1
x2=0
x3=0
x3=1
x3=0
x3=1
x3=0
19
動態規劃1. The longest path represents the optimal solution:
2. x1=0, x2=1, x3=1
3. = 20+30 = 50 iixP
20
練習
i Wi Pi 1 8 40 2 4 20 3 5 30 4 2 5
背包重量=10
0-1 背包問題:求出下列最佳解
21
資源分配問題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
22
The multistage graph solution
The resource allocation problem can be described as a multistage graph.
(i, j) : i resources allocated to projects 1, 2, …, je.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
44
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
42
2
0
23
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.
0 resource allocated to projects 3, 4.
24
練習
Resource Project
1
2
3
1 2 2 9 2 5 6 7 3 4 4 4 4 2 4 5 5 3 3 2
找出下列最佳的分配
25
準則
In summary, if a problem can be described by a multistage graph, then it can be solved by dynamic programming.
如果一個問題可以轉成多階圖形問題,那ㄧ定可以用動態規劃解決
26
二項式係數
分解
27
二項式係數 (divide-and-conquer 版 )
}
28
遞迴推疊圖(The recursive stack)
呼叫過程 :
B2,1B2,0
B3,2B3,1
B4,2
B2,2
B1,1B2,0
C4,2
B2,1
B1,1B2,0
29
設計動態規劃
30
矩陣 B
31
二項式係數 (dynamic programming 版 )
32
33
二項式係數時間複雜度
34
練習計算出 ( 6,2 )之矩陣 B 的值。
35
動態規劃與個各擊破
36
動態規劃和最佳化問題
定義: 最佳化原則 (principle of optimality) 要可以應用在一個問題上,他必須符合一個原則: 當一個問題存在著最佳解,則表示其所有的子問題也必存在著最佳解。
37
連鎖矩陣相乘Ex: 2x3 矩陣乘 3x4 矩陣
38
連鎖矩陣相乘最佳化問題
39
問題分析
1. 使用暴力法 (brute-force)
2. 找出具有最少乘法的組合3. 時間複雜度:指數 (exponential)
40
問題分析1. 矩陣相乘問題符合最佳化問題。
2. 最佳化原則:如果有存在最佳的相乘順序,則此最佳相乘順序的任一子集合,也是最佳的相乘順序。
3. 假設下列為六個矩陣的最佳相乘順序
4. 則子集合亦是最佳相乘順序
5. 使用動態規劃找尋最佳化的組合
41
動態規劃
42
陣列 M 規劃
43
Example
44
最少乘法次數
45
最少乘法次數 (M 陣列 )
46
最少乘法次數 (M 陣列 )
47
最少乘法次數 (M 陣列 )
48
最少乘法次數演算法
49
50
最少乘法次數的時間複雜度
51
最佳乘法順序 (P 矩陣 )
52
最佳乘法順序 (P 矩陣 )
53
最佳乘法順序
54
印出最佳順序
55
練習找出下列五個矩陣相乘之最佳順序及乘法次數
A1 is (10x4)
A2 is (4x5)
A3 is (5x20)
A4 is (20x2)
A5 is (2x50)
56
最佳二元搜尋樹
定義: 二元搜尋樹 (binary search tree)
1. 每個節點包含一個 key
2. 節點 N 的左子樹中任一節點的 key ,必須小於或等於節點 N 的 key
3. 節點 N 的右子樹中任一節點的 key ,必須大於或等於節點 N 的 key
57
二元搜尋樹50
40
30
65
6045
45
40
30
65
60
60
40
30
65
45
58
兩個二元搜尋樹
59
深度 (depth)
從根節點到該節點所經過路徑的邊 (edge)的數目
60
平衡 (balanced)
在一個樹中的任何一節點,其左右子樹的深度相差不超過 1
61
搜尋二元樹演算法
62
搜尋時間 (search time)在 search() 程序中搜尋一個 key 所需比較 (com
parison) 指令的數目
EX : depth(key)+1
depth(Ursula)+1=2+1=3
63
當 Key 值的搜尋機率不同
搜尋時間:
其中 pi 為 keyi 的搜尋機率,
Ci 為 keyi 的搜尋指令總數 (keyi 的深度 )
64
Example
65
最佳二元搜尋樹目標
66
Example
67
最佳化原則
68
動態規劃設計
69
動態規劃設計
70
動態規劃設計
71
最佳二元搜尋樹演算法
72
73
最佳二元搜尋樹所有情況的時間複雜度
74
建立最佳二元搜尋樹演算法
75
76
Example
77
A 與 R 陣列
78
最佳二元樹
79
top related