• 沒有找到結果。

Dynamic Programming

N/A
N/A
Protected

Academic year: 2021

Share "Dynamic Programming"

Copied!
79
0
0

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

全文

(1)

動態規劃

(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( 回溯 )

 圖形著色、漢米爾迴路問題… .

(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 2 B E 13 T 9 A 4 D C 2 F 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 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

(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 2 B E 13 T 9 A 4 D C 2 F 1 5 11 5 16 18 2

(12)

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

(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 2 B E 13 T 9 A 4 D C 2 F 1 5 11 5 16 18 2

(14)

練習

(15)

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 的限制下, 為最大值

(16)

貪婪解法範例

(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 。

(17)

Example

 n objects , weight

W1, W2, ,Wn profit

P1, P2, ,Pn capacity

M maximize

subject to

 M xi = 0 or 1, 1in  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=10

(18)

The 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

(19)

動態規劃

1. The longest path represents the optimal solution: 2. x1=0, x2=1, x3=1

(20)

練習

(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 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

(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.

(24)

練習

(25)

準則

 In summary, if a problem can be described by a multistage graph, then it can be solved by dyna mic programming.

 如果一個問題可以轉成多階圖形問題

,那ㄧ定

(26)

二項式係數

(27)

二項式係數

(divide-and-conquer 版 )

(28)

遞迴推疊圖

(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

(29)
(30)
(31)
(32)
(33)
(34)

練習

(35)
(36)

動態規劃和最佳化問題

定義:

最佳化原則

(principle of optimality) 要可以

應用在一個問題上,他必須符合一個原則:

當一個問題存在著最佳解,則表示其所有的子

問題也必存在著最佳解。

(37)

連鎖矩陣相乘

Ex: 2x3 矩陣乘 3x4 矩陣

(38)
(39)

問題分析

1. 使用暴力法 (brute-force)

2. 找出具有最少乘法的組合

(40)

問題分析

1. 矩陣相乘問題符合最佳化問題。 2. 最佳化原則:如果有存在最佳的相乘順序,則此最 佳相乘順序的任一子集合,也是最佳的相乘順序。 3. 假設下列為六個矩陣的最佳相乘順序 4. 則子集合亦是最佳相乘順序

(41)
(42)
(43)
(44)
(45)
(46)
(47)
(48)
(49)
(50)
(51)
(52)
(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 ,必

(57)

二元搜尋樹

50 40 30 65 60 45 45 40 30 65 60 60 40 30 65 45

(58)
(59)

深度

(depth)

從根節點到該節點所經過路徑的邊

(edge)

(60)

平衡

(balanced)

在一個樹中的任何一節點,其左右子樹的深

(61)
(62)

搜尋時間

(search time)

search() 程序中搜尋一個 key 所需比較 (com

parison) 指令的數目

EX : depth(key)+1

(63)

Key 值的搜尋機率不同

搜尋時間

其中 pi 為 keyi 的搜尋機率

(64)
(65)
(66)
(67)
(68)
(69)
(70)
(71)
(72)
(73)
(74)
(75)
(76)
(77)
(78)
(79)

參考文獻

相關文件

In Section 3, the shift and scale argument from [2] is applied to show how each quantitative Landis theorem follows from the corresponding order-of-vanishing estimate.. A number

The hashCode method for a given class can be used to test for object equality and object inequality for that class. The hashCode method is used by the java.util.SortedSet

understanding of what students know, understand, and can do with their knowledge as a result of their educational experiences; the process culminates when assessment results are

It is well known that second-order cone programming can be regarded as a special case of positive semidefinite programming by using the arrow matrix.. This paper further studies

We can therefore hope that the exact solution of a lower-dimensional string will provide ideas which could be used to make an exact definition of critical string theory and give

It is the author’s hope that the Zuting shiyuan may be effectively used as a supplement for understanding Chan texts, and its contributions be fully valued.. Furthermore, the

Programming languages can be used to create programs that control the behavior of a. machine and/or to express algorithms precisely.” -

Dynamic programming is a method that in general solves optimization prob- lems that involve making a sequence of decisions by determining, for each decision, subproblems that can