• 沒有找到結果。

Divide and Conquer II

N/A
N/A
Protected

Academic year: 2022

Share "Divide and Conquer II"

Copied!
41
0
0

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

全文

(1)

Michael Tsai 2012/09/20

(2)

矩陣相乘

 問題:

 Input: A, B 都是 n x n 的Square Matrix

 Output: C, n x n 的square matrix, C = A ∙ 𝐵

(3)

矩陣相乘 – 基本法

𝑎 11 𝑎 12 𝑎 13 𝑎 21 𝑎 22 𝑎 23 𝑎 31 𝑎 32 𝑎 33

𝑏 11 𝑏 12 𝑏 13 𝑏 21 𝑏 22 𝑏 23

𝑏 31 𝑏 32 𝑏 33 =

𝑐 11 𝑐 12 𝑐 13 𝑐 21 𝑐 22 𝑐 23 𝑐 31 𝑐 32 𝑐 33

𝑐𝑖𝑗 = 𝑎𝑖𝑘 ∙ 𝑏𝑘𝑗

𝑛 𝑘=1

n個 n個

n個乘法, n-1個加法, 產生了一個entry, 共有 𝑛2個entries

Running time = ? Θ(𝑛

3

)

(4)

矩陣相乘 – D&C嘗試一

𝐴12 𝐴11

𝐴21 𝐴22

𝐵12 𝐵11

𝐵21 𝐵22

𝐶12 𝐶11

𝐶21 𝐶22

𝐶11 = 𝐴11𝐵11 + 𝐴12𝐵21 從𝐴11𝐵11可得

從𝐴12𝐵21可得 Recursive

case:

(5)

矩陣相乘 – D&C嘗試一

 n=1

 則𝐶 = 𝐴 ∙ 𝐵直接算 (A,B,C各自為一個數)

Base case:

(6)

矩陣相乘 – D&C嘗試一

 以下面的等式可求得C的四個小分塊的解:

 𝐶11 = 𝐴11 ∙ 𝐵11 +𝐴12 ∙ 𝐵21

 𝐶12 = 𝐴11 ∙ 𝐵12 +𝐴12 ∙ 𝐵22

 𝐶21 = 𝐴21 ∙ 𝐵11 +𝐴22 ∙ 𝐵21

 𝐶22 = 𝐴21 ∙ 𝐵12 +𝐴22 ∙ 𝐵22

(7)

Square-Matrix-Multiply-Recursive(A,B) n=A.rows

let C be a new n x n matrix if n==1

𝑐11 = 𝑎11 ∙ 𝑏11

else partition the matrix into 4 n/2 x n/2 matrices

𝐶11 =Square-Matrix-Multiply-Recursive(𝐴11, 𝐵11) +Square-Matrix-Multiply-Recursive(𝐴12, 𝐵21) 𝐶12 =Square-Matrix-Multiply-Recursive(𝐴11, 𝐵12) +Square-Matrix-Multiply-Recursive(𝐴12, 𝐵22) 𝐶21 =Square-Matrix-Multiply-Recursive(𝐴21, 𝐵11) +Square-Matrix-Multiply-Recursive(𝐴22, 𝐵21) 𝐶22 =Square-Matrix-Multiply-Recursive(𝐴21, 𝐵12) +Square-Matrix-Multiply-Recursive(𝐴22, 𝐵22)

return C

Base case

Recursive case

8𝑇(𝑛 2) Combine

𝑇 1 = Θ(1) Θ(1)

Θ(𝑛2)

𝑇 𝑛 = Θ 1 + 8𝑇 𝑛

2 + Θ 𝑛2 = 8𝑇 𝑛

2 + Θ 𝑛2 𝑇 𝑛 = Θ(1)

8𝑇 𝑛

2 + Θ(𝑛2)

,if 𝑛 = 1

,if 𝑛 > 1

𝑇 𝑛 = Θ 𝑛

3

(8)

矩陣相乘 – Strassen’s method

課本說: “Not at all obvious”

可達到Θ 𝑛𝑙𝑜𝑔7 = Θ 𝑛2.8074

Overview:

1. 將A, B及C都切成四塊n/2大小的matrix

2. 做出10個matrices, 𝑆1, 𝑆2, … , 𝑆10.這些matrix都 是以步驟1中n/2大小的matrices加減後得到 的結果.

3. 使用步驟1&2中得到的n/2大小的matrices做 乘法後得到𝑃1, 𝑃2, … , 𝑃7.

4. 以𝑃1, 𝑃2, … , 𝑃7相加減後得到的結果產生 𝐶11, 𝐶12, 𝐶21, 𝐶22四個matrix

細節先略過, 但我們可以計算running time了…

德國數學家 Volker Strassen 攝於2009年

(9)

矩陣相乘 – Strassen’s method

1. 將A, B及C都切成四塊n/2大小的matrix

2. 做出10個matrices, 𝑆1, 𝑆2, … , 𝑆10.這些matrix都是以 步驟1中n/2大小的matrices加減後得到的結果.

3. 使用步驟1&2中得到的n/2大小的matrices做乘法後 得到𝑃1, 𝑃2, … , 𝑃7.

4. 以𝑃1, 𝑃2, … , 𝑃7相加減後得到的結果產生 𝐶11, 𝐶12, 𝐶21, 𝐶22四個matrix

Θ(1) Θ(𝑛2)

7𝑇(𝑛 2)

Θ(𝑛2)

𝑇 𝑛 = Θ 1 + Θ 𝑛2 + 7𝑇 𝑛

2 + Θ 𝑛2 = 7𝑇 𝑛

2 + Θ 𝑛2

𝑇 𝑛 = Θ 𝑛

log 7

(10)

矩陣相乘 – Strassen’s method

𝑆1 = 𝐵12 − 𝐵22

𝑆2 = 𝐴11 + 𝐴12

𝑆3 = 𝐴21 + 𝐴22

𝑆4 = 𝐵21 − 𝐵11

𝑆5 = 𝐴11 + 𝐴22

𝑆6 = 𝐵11 + 𝐵22

𝑆7 = 𝐴12 − 𝐴22

𝑆8 = 𝐵21 + 𝐵22

𝑆9 = 𝐴11 − 𝐴21

𝑆10 = 𝐵11 + 𝐵12

 𝑃1 = 𝐴11 ∙ 𝑆1

 𝑃2 = 𝑆2 ∙ 𝐵22

 𝑃3 = 𝑆3 ∙ 𝐵11

 𝑃4 = 𝐴22 ∙ 𝑆4

 𝑃5 = 𝑆5 ∙ 𝑆6

 𝑃6 = 𝑆7 ∙ 𝑆8

 𝑃7 = 𝑆9 ∙ 𝑆10

Not at all obvious

(11)

矩陣相乘 – Strassen’s method

𝑃

5

= 𝐴

11

𝐵

11

+ 𝐴

11

𝐵

22

+ 𝐴

22

𝐵

11

+ 𝐴

22

𝐵

22

𝑃

4

= −𝐴

22

𝐵

11

+𝐴

22

𝐵

21

−𝑃

2

= −𝐴

11

𝐵

22

− 𝐴

12

𝐵

22

𝑃

6

= 𝐴

12

𝐵

21

− 𝐴

22

𝐵

22

− 𝐴

22

𝐵

21

+ 𝐴

12

𝐵

22

𝐶

11

= 𝐴

11

𝐵

11

+ 𝐴

12

𝐵

21

= 𝑃

5

+ 𝑃

4

− 𝑃

2

+ 𝑃

6

𝐶

12

, 𝐶

21

, 𝐶

22

用類似的方法

<Reading assignment> Textbook 4.2

<Homework for yourself> Exercise 4.2-1

Not at all obvious

(12)

矩陣相乘 – Strassen’s method

實用嗎? 好用嗎? 讓我們來挑毛病:

1. Θ(𝑛𝑙𝑔7)的constant比Θ(𝑛3)大

2. 那些sub-matrices要花額外的空間

3. 當是Sparse Matrix時, 特別為Sparse Matrix設計的方法比較

4. Strassen’s method is not as numerically stable as 基本法.

1990年代的部分研究減輕了2&4的壞處

所以, Strassen’s method有什麼用呢?

Key: find the crossover point and combine the two algorithms.

目前所知, the most asymptotically efficient algorithm has a running time of 𝑂 𝑛2.376 . (Coppersmith and Winograd)

(13)

接下來…

 問題: 我們要怎麼解遞迴式?

 取代法

 遞迴樹法

 大師定理法

http://www.origin-zero.com/senzi/JOKE1.jpg

(14)

取代法

猜答案的形式 用數學歸納法證明此

形式成立 得到遞迴式的解 成功

失敗

(15)

取代法-例子

 問題: 𝑇 𝑛 = 2𝑇 𝑛2 + 𝑛

 猜測: 𝑇 𝑛 = 𝑂 𝑛 log 𝑛

 用歸納法證明 𝑇 𝑛 ≤ c n log n, for a constant 𝑐 > 0

 假設上面的bound在m<n時成立

 則𝑚 = 𝑛2 時亦成立.

 也就是說𝑇 𝑛2 ≤ c 𝑛

2 log 𝑛

2

(16)

取代法-例子

 𝑇 𝑛 = 2𝑇 𝑛2 + 𝑛

≤ 2 𝑐 𝑛

2 log 𝑛

2 + 𝑛

≤ 𝑐𝑛 log 𝑛

2 + 𝑛

= 𝑐𝑛 log 𝑛 − 𝑐𝑛 log 2 + 𝑛

= 𝑐𝑛 log 𝑛 − 𝑐𝑛 + 𝑛

≤ 𝑐𝑛 log 𝑛

as long as 𝑐 ≥ 1

(17)

取代法-例子

 接著必須也證明邊界條件成立.

 有時候需要多一點努力….

 假設𝑇 1 = 1.

 我們必須證明𝑇 𝑛 ≤ 𝑐𝑛 log 𝑛 , 𝑛 = 1

 天不從人願: 𝑛 = 1時, 𝑐𝑛 log 𝑛 = 0

 T 1 = 1 > 0 = 𝑐𝑛 log 𝑛

 娃~

(18)

取代法-例子

 事實上, 我們只須證明, 當𝑛 > 𝑛0時, 𝑇 𝑛 ≤ 𝑐𝑛 log 𝑛即可. (把不聽話的𝑛 = 1拔掉)

 從原本的遞迴式, 我們可以得到𝑇 2 = 4, 𝑇 3 = 5.

 接著設𝑛0 = 2. 我們發現:

 𝑇 2 ≤ 𝑐 2 log 2 & 𝑇 3 ≤ 𝑐 3 log 3

 (只要𝑐 ≥ 2) 至此可以使邊界條件成立.

 喔耶.

(19)

取代法-怎麼猜?

 靠經驗.

 跟沒講一樣.

 一些小方法:

1. 根據以前看過類似的遞迴式來猜測

2. 使用等一下要介紹的遞迴樹

3. 證明比較鬆的upper bound或lower bound來慢慢 接近tight bound

𝑇 𝑛 = 2𝑇 𝑛

2 + 17 + 𝑛 老工匠

(20)

取代法-小技巧1

 題目: T 𝑛 = 𝑇 𝑛2 + 𝑇 𝑛2 + 1

 猜測: 𝑇 𝑛 = 𝑂 𝑛

 歸納法證明:

 𝑇 𝑛 ≤ 𝑐 𝑛

2 + 𝑛

2 + 1 = 𝑐𝑛 + 1 ≰ 𝑐𝑛

 爛掉了…

(21)

取代法-小技巧1

 方法: 改使用𝑇 𝑛 ≤ 𝑐𝑛 − 𝑑, 𝑑 ≥ 0

(減掉一個order較低的term)

 𝑇 𝑛 ≤ 𝑐 𝑛

2 − 𝑑 + 𝑐 𝑛

2 − 𝑑 + 1

= 𝑐𝑛 − 2𝑑 + 1

≤ 𝑐𝑛 − 𝑑

 (as long as 𝑑 ≥ 1)

 (然後繼續選c, 使得boundary condition成立, 在 此省略)

(22)

取代法–小技巧2

看起來挺嚇人的: 𝑇 𝑛 = 2𝑇 𝑛 + log 𝑛

替換變數: 𝑚 = log 𝑛

𝑇 2𝑚 = 2𝑇 2𝑚2 + 𝑚

定義: 𝑆 𝑚 = 𝑇(2𝑚)

𝑆 𝑚 = 2𝑆 𝑚2 + 𝑚

𝑆 𝑚 = 𝑂 𝑚 log 𝑚

𝑇 𝑛 = 𝑇 2𝑚 = 𝑆 𝑚 = 𝑂 𝑚 log 𝑚 = 𝑂(log 𝑛 log log 𝑛)

暫時不管flooring

(23)

遞迴樹法

畫出遞迴樹

用數學歸納法證明此 解成立

用比較不嚴謹的方法 加總得到解

(24)

 例子: 𝑇 𝑛 = 3𝑇 𝑛4 + Θ 𝑛2

𝑇(𝑛)

𝑇(𝑛 4) 𝑇(𝑛

4) 𝑇(𝑛 4) 𝑐𝑛2

𝑐 𝑛 4 𝑐 𝑛 2

4

2

𝑐 𝑛 4

2

𝑇( 𝑛

16) 𝑇( 𝑛

16) 𝑇( 𝑛

16) 𝑇( 𝑛

16) 𝑇( 𝑛

16) 𝑇( 𝑛 16) 𝑇( 𝑛

16) 𝑇( 𝑛

16) 𝑇( 𝑛 16) 𝑐 𝑛

16

2

𝑐 𝑛 16

2𝑐 𝑛 16

2𝑐 𝑛 16

2

𝑐 𝑛 16

2𝑐 𝑛 16

𝑐2 𝑛 16

2

𝑐 𝑛 16

2𝑐 𝑛 16

2

………

𝑇(1) 𝑇(1)

𝑐𝑛2

3

16𝑐𝑛2

3 16

2

𝑐𝑛2

Θ 𝑛log43 level log4𝑛

level 0

level 1

level 2

3log4𝑛 = 𝑛log43個node

(25)

𝑇 𝑛 = 𝑐𝑛2 + 163 𝑐𝑛2 + 163 2 𝑐𝑛2 + ⋯ + 163 log4 𝑛−1 𝑐𝑛2 + Θ 𝑛log4 3

= 3

16

𝑖

𝑐𝑛2 + Θ 𝑛log4 3

log4 𝑛−1

𝑖=0

3 16

𝑖

𝑐𝑛2 + Θ 𝑛log4 3

𝑖=0

= 1

1 − 316

𝑐𝑛2 + Θ 𝑛log4 3

= 16

13 𝑐𝑛2 + Θ 𝑛log4 3

= 𝑂 𝑛2

(26)

遞迴樹法–例子1

用歸納法證明: 𝑇 𝑛 ≤ 𝑑𝑛2 for some 𝑑 > 0.

𝑇 𝑛 ≤ 3𝑇 𝑛4 + 𝑐𝑛2

≤ 3𝑑 𝑛 4

2 + 𝑐𝑛2

≤ 3𝑑 𝑛 4

2 + 𝑐𝑛2

= 3

16 𝑑𝑛2 + 𝑐𝑛2

≤ 𝑑𝑛2

as long as 𝑑 ≥ 1613 𝑐

(27)

遞迴樹法–例子2

 例子: 𝑇 𝑛 = 𝑇 𝑛3 + 𝑇 2𝑛3 + 𝑂 𝑛

 請一位同學上來畫遞迴樹 (有點跛腳的遞迴樹)

(28)

遞迴樹法–例子2

歸納法證明: 𝑇 𝑛 ≤ 𝑑𝑛 log 𝑛

𝑇 𝑛 ≤ 𝑇 𝑛

3 + 𝑇 2𝑛

3 + 𝑐𝑛

≤ 𝑑 𝑛

3 log𝑛

3 + 𝑑 2𝑛

3 log2𝑛

3 + 𝑐𝑛

= 𝑑 𝑛

3 log 𝑛 + 𝑑 2𝑛

3 log 𝑛 − 𝑑 𝑛

3 log 3 − 𝑑 2𝑛

3 log 3

2 + 𝑐𝑛

= 𝑑𝑛 log 𝑛 − 𝑑 𝑛

3 log 3 + 2𝑛

3 log 3 − 2𝑛

3 log 2 + cn

= 𝑑𝑛 log 𝑛 − 𝑑𝑛 log 3 −2

3 + 𝑐𝑛

≤ 𝑑𝑛 log 𝑛

as long as 𝑑 ≥ 𝑐

log 3−23

(29)

大師定理

Master Theorem:

Let 𝑎 ≥ 1 and 𝑏 ≥ 1 be constants, let 𝑓 𝑛 be a function, and let 𝑇 𝑛 be defined on the nonnegative integers by the recurrence

𝑇 𝑛 = 𝑎𝑇 𝑛

𝑏 + 𝑓 𝑛

where we interpret 𝑛𝑏 to mean either 𝑛𝑏 or 𝑛𝑏 . Then 𝑇 𝑛 has the following asymptotic bounds:

1. if 𝑓 𝑛 = 𝑂 𝑛log𝑏 𝑎−𝜖 for some constant 𝜖 > 0, then 𝑇 𝑛 = Θ 𝑛log𝑏 𝑎

2. if 𝑓 𝑛 = Θ 𝑛log𝑏 𝑎 , then 𝑇 𝑛 = Θ 𝑛log𝑏 𝑎 log 𝑛

3. if 𝑓 𝑛 = Ω 𝑛logba+𝜖 for some constant 𝜖 > 0, and if

𝑎𝑓 𝑛𝑏 ≤ 𝑐𝑓 𝑛 for some constant 𝑐 < 1 and all sufficiently large 𝑛, then 𝑇 𝑛 = Θ 𝑓 𝑛

(30)

大師定理

1. if 𝑓 𝑛 = 𝑂 𝑛log𝑏 𝑎−𝜖 for some constant 𝜖 > 0, then 𝑇 𝑛 = Θ 𝑛log𝑏 𝑎

2. if 𝑓 𝑛 = Θ 𝑛log𝑏 𝑎 , then 𝑇 𝑛 = Θ 𝑛log𝑏 𝑎 log 𝑛

3. if 𝑓 𝑛 = Ω 𝑛logba+𝜖 for some constant 𝜖 > 0, and if

𝑎𝑓 𝑛𝑏 ≤ 𝑐𝑓 𝑛 for some constant 𝑐 < 1 and all sufficiently large 𝑛, then 𝑇 𝑛 = Θ 𝑓 𝑛

𝑓(𝑛) 𝑛 log

𝑏

𝑎

Which one is polynomially larger/smaller?

𝑓(𝑛)is larger 𝑛log𝑏 𝑎is larger

The same order

Note: not all possibilities for 𝒇 𝒏 can be covered by these 3 cases!

(31)

 𝑇 𝑛 = 9𝑇 𝑛

3 + 𝑛

 a=9, b=3, f(n)=n

 log𝑏 𝑎 = log3 9 = 2

 satisfies case 1: 𝑓 𝑛 = 𝑂 𝑛log𝑏 𝑎−𝜖 , 𝜖 = 1

 so 𝑇 𝑛 = Θ 𝑛log𝑏 𝑎 = Θ 𝑛2

For your reference: 𝑇 𝑛 = 𝑎𝑇 𝑛𝑏 + 𝑓 𝑛

𝑓(𝑛) 𝑛

log𝑏 𝑎

(32)

大師定理-例子2

 𝑇 𝑛 = 𝑇 2𝑛3 + 1

 a=1, b=3/2, f(n)=1

 𝑛log𝑏 𝑎 = nlog321 = n0 = 1

 satisfies case 2: 𝑓 𝑛 = Θ 𝑛log𝑏 𝑎 log 𝑛 = Θ log 𝑛

For your reference: 𝑇 𝑛 = 𝑎𝑇 𝑛𝑏 + 𝑓 𝑛

𝑓(𝑛) 𝑛

log𝑏 𝑎

(33)

大師定理-更多例子

 𝑇 𝑛 = 3𝑇 𝑛

4 + 𝑛 log 𝑛

 𝑇 𝑛 = 2𝑇 𝑛2 + 𝑛 log 𝑛

 𝑇 𝑛 = 2𝑇 𝑛2 + Θ 𝑛

 𝑇 𝑛 = 8𝑇 𝑛

2 + Θ 𝑛2

 𝑇 𝑛 = 7𝑇 𝑛2 + Θ 𝑛2

上台解題時間…

(34)

取數問題

 Selection Problem

 問題:

 Input: n個數字之集合

 Output: 取出此n個數字之中位數

 中位數之定義: n個數字中第k= 𝑛2 小的數字

(35)

取數問題

 菜瓜布解法:

 先把n個數sort好

 從最小的數過去算到第𝑘小的數字即為答案

 running time=?

 Ω(𝑛 log 𝑛)

 Can we do a better job?

(36)

取數問題

n個數

小於等於a的

某數a (pivot)

大於a的 𝐿1 𝐿2

if 𝑘 > 𝐿2 , 找𝐿1中第𝑘 − 𝐿2 小的 if 𝑘 ≤ 𝐿2 , 找𝐿2中第𝑘小的

(37)

取數問題

 下一個問題: 怎麼選a?

 選不好的話…𝐿1 = 𝑛, 𝐿2 = 0

 最好是可以平均分成兩分.

 那就是選中位數.

 咦, 我們不是就要找中位數嗎?

 能不能花少一點時間, 找個”差不多”的中位數

(38)

取數問題

 差不多的中位數:

1. 把n個數分成很多大小為5個的sub list (大約共 有n/5個sub list

2. 這些sub list中各自找中位數

3. 找出n/5個中位數中的中位數

此為差不多的中位數

(39)

取數問題

 有多差不多呢?

 比”差不多中位數”小 的至少有3𝑛

10

 𝐿23𝑛

10

 𝐿17𝑛10

𝑛/5個中位數

中位數的中位數

3n 5 10

(40)

取數問題

Algorithm:

1. if 𝑛 ≤ 5 then 直接找出其中位數

2. else

3. 把數列拆成𝑛

5個大小為5的小數列

4. 每個小數列找出其中位數

5. 找出𝑛

5個中位數的中位數m

6. 用此中位數把原本的數列拆成兩部分: 比m大(𝐿1) 及不比m大的(𝐿2)

7. if 𝑘 > 𝐿2 , 找𝐿1中第𝑘 − 𝐿2 小的

8. if 𝑘 ≤ 𝐿2 , 找𝐿2中第𝑘小的

Θ(1) Θ(𝑛)

Θ(𝑛) T(𝑛

5)

Θ(𝑛)

Max: T(7𝑛10) 𝑇 𝑛 = Θ 𝑛 + Θ 𝑛 + 𝑇 𝑛

5 + Θ 𝑛 + 𝑇 7𝑛 10

= 𝑇 𝑛

5 + 𝑇 7𝑛

10 + Θ(𝑛) 𝑇 𝑛 = Θ 𝑛

(41)

Today’s Reading Assignment

 Cormen ch 4.2 – 4.5

參考文獻

相關文件

Mathematically, 5-equation model approaches to same relaxation limits as HRM, but is difficult to solve numerically to ensure solution to be feasible.. 5 -equation

• Students annotate a text using an annotation tool that identifies their authorship. • Advantage: student annotations may

How would this task help students see how to adjust their learning practices in order to improve?..

Associate Professor of Department of Mathematics and Center of Teacher Education at National Central

if left_sum&gt;=right_sum and left_sum&gt;=cross_sum return (left_low,left_high,left_sum). else if right_sum&gt;=left_sum and right_sum&gt;=cross_sum

Let and be constants, let be a function, and let be defined on the nonnegative integers by the recu rrence. where we interpret to mean either

證明比較鬆的upper bound或lower bound來慢慢 接近tight

主定理 (Master Theorem) 主定理 (Master Theorem). reference: