Michael Tsai 2011/03/04
矩陣相乘
問題:
Input: A, B 都是 n x n 的Square Matrix
Ouput: C, n x n 的square matrix, C A ∙矩陣相乘 – 基本法
∙
n個 n個
n個乘法, n‐1個加法, 產生了一個entry, 共有 個entries
Running time = ? Θ
矩陣相乘 – D&C嘗試一
從 可得
從 可得
Recursive case:
矩陣相乘 – D&C嘗試一
n=1
則 ∙ 直接算 (A,B,C各自為一個數)Base case:
矩陣相乘 – D&C嘗試一
以下面的等式可求得C的四個小分塊的解:
∙ ∙
∙ ∙
∙ ∙
∙ ∙Square-Matrix-Multiply-Recursive(A,B) n=A.rows
let C be a new n x n matrix if n==1
∙
else partition the matrix into 4 n/2 x n/2 matrices
Square-Matrix-Multiply-Recursive( , ) +Square-Matrix-Multiply-Recursive( , )
Square-Matrix-Multiply-Recursive( , ) +Square-Matrix-Multiply-Recursive( , )
Square-Matrix-Multiply-Recursive( , ) +Square-Matrix-Multiply-Recursive( , )
Square-Matrix-Multiply-Recursive( , ) +Square-Matrix-Multiply-Recursive( , )
return C
Base case
Recursive case
8 2 Combine
1 Θ 1 Θ 1
Θ
Θ 1 8
2 Θ 8
2 Θ
Θ 1
8 2 Θ
,if 1
,if 1
Θ
矩陣相乘 – Strassen’s method
課本說: “Not at all obvious”
可達到Θ Θ .
Overview:
1. 將A, B及C都切成四塊n/2大小的matrix
2. 做出10個matrices, , , … , .這些matrix都 是以步驟1中n/2大小的matrices加減後得到 的結果.
3. 使用步驟1&2中得到的n/2大小的matrices做 乘法後得到 , , … , .
4. 以 , , … , 相加減後得到的結果產生 , , , 四個matrix
細節先略過, 但我們可以計算running time了…
德國數學家 Volker Strassen 攝於2009年
矩陣相乘 – Strassen’s method
1. 將A, B及C都切成四塊n/2大小的matrix
2. 做出10個matrices, , , … , .這些matrix都是以 步驟1中n/2大小的matrices加減後得到的結果.
3. 使用步驟1&2中得到的n/2大小的matrices做乘法後 得到 , , … , .
4. 以 , , … , 相加減後得到的結果產生 , , , 四個matrix
Θ 1 Θ
7 2
Θ
Θ 1 Θ 7
2 Θ 7
2 Θ
Θ
矩陣相乘 – Strassen’s method
∙
∙
∙
∙
∙
∙
∙Not at all obvious
矩陣相乘 – Strassen’s method
, , 用類似的方法
<Reading assignment> Textbook 4.2
<Homework for yourself> Exercise 4.2‐1
Not at all obvious矩陣相乘 – Strassen’s method
實用嗎? 好用嗎? 讓我們來挑毛病:
1. Θ 的constant比Θ 大
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 . . (Coppersmith and Winograd)
接下來…
問題: 我們要怎麼解遞迴式?
取代法
遞迴樹法
大師定理法http://www.origin‐zero.com/senzi/JOKE1.jpg
取代法
猜答案的形式 用數學歸納法證明此
形式成立 得到遞迴式的解 成功
失敗
取代法‐例子
問題: 2
猜測: log
用歸納法證明 c n log n, for a constant 0
假設上面的bound在m<n時成立
則 時亦成立.
也就是說 c log取代法‐例子
22 2 log
2 log 2
log log 2 log
log as long as 1
栗子助教
舉個栗子
取代法‐例子
接著必須也證明邊界條件成立.
有時候需要多一點努力….
假設 1 1.
我們必須證明 log , 1
天不從人願: 1時, log 0
T 1 1 0 log
娃~取代法‐例子
事實上, 我們只須證明, 當 時, log 即可. (把不聽話的 1拔掉)
從原本的遞迴式, 我們可以得到 2 4, 3 5.
接著設 2. 我們發現:
2 2 log 2 & 3 3 log 3
(只要 2) 至此可以使邊界條件成立.
喔耶.取代法‐怎麼猜?
靠經驗.
跟沒講一樣.
一些小方法:1.
根據以前看過類似的遞迴式來猜測2.
使用等一下要介紹的遞迴樹3.
證明比較鬆的upper bound或lower bound來慢慢 接近tight bound2 2 17
老工匠
取代法‐小技巧1
題目: T 1
猜測:
歸納法證明:
1 1 ≰
爛掉了…取代法‐小技巧1
方法: 改使用 , 0
(減掉一個order較低的term)
1 2 1
(as long as 1)
(然後繼續選c, 使得boundary condition成立, 在 此省略)取代法–小技巧2
看起來挺嚇人的: 2 log
替換變數: log
2 2 2
定義: 2
2
log
2 log
log log log
暫時不管flooring
遞迴樹法
畫出遞迴樹用數學歸納法證明此 解成立
用比較不嚴謹的方法 加總得到解
例子: 3 Θ4 44 4
4 4
16 16 16 16 16 16 16 16 16
16 16 16 16 16 16 16 16 16
………
1 1
3 16
3 16
Θ level log
level 0
level 1
level 2
3 個node
⋯ Θ
3
16 Θ
3
16 Θ
1 1 3
16
Θ 16
13 Θ
遞迴樹法–例子1
用歸納法證明: for some 0.
3
3 4
3 4 3
16
as long as
遞迴樹法–例子2
例子:
請一位同學上來畫遞迴樹 (有點跛腳的遞迴樹)遞迴樹法–例子2
歸納法證明: log
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
大師定理
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 for some constant 0, then Θ
2. if Θ , then Θ log
3. if Ω for some constant 0, and if
for some constant 1 and all sufficiently large , then Θ
大師定理
1. if for some constant 0, then Θ
2. if Θ , then Θ log
3. if Ω for some constant 0, and if
for some constant 1 and all sufficiently large , then Θ
Which one is polynomially larger/smaller?
is larger is larger
The same order
Note: not all possibilities for can be covered by these 3 cases!
9
a=9, b=3, f(n)=n
log log 9 2
satisfies case 1: , 1
so Θ ΘFor your reference:
舉個栗子
大師定理‐例子2
1
a=1, b=3/2, f(n)=1
n n 1
satisfies case 2: Θ log Θ logFor your reference:
大師定理‐更多例子
3 log
2 log
2 Θ
8 Θ
7 Θ上台解題時間…
取數問題
Selection Problem
問題:
Input: n個數字之集合
Output: 取出此n個數字之中位數
中位數之定義: n個數字中第k= 小的數字取數問題
菜瓜布解法:
先把n個數sort好
從最小的數過去算到第 小的數字即為答案
running time=?
Ω log
Can we do a better job?取數問題
n個數
小於等於a的
某數a (pivot)
大於a的
if ,找 中第 小的
if ,找 中第 小的
取數問題
下一個問題: 怎麼選a?
選不好的話… , 0
最好是可以平均分成兩分.
那就是選中位數.
咦, 我們不是就要找中位數嗎?
能不能花少一點時間, 找個”差不多”的中位數取數問題
差不多的中位數:1.
把n個數分成很多大小為5個的sub list (大約共 有n/5個sub list2.
這些sub list中各自找中位數3.
找出n/5個中位數中的中位數此為差不多的中位數
取數問題
有多差不多呢?
比”差不多中位數”小 的至少有
/5個中位數
中位數的中位數
3n 5 10
取數問題
Algorithm:
1. if 5 then 直接找出其中位數
2. else
3. 把數列拆成 個大小為5的小數列
4. 每個小數列找出其中位數
5. 找出 個中位數的中位數m
6. 用此中位數把原本的數列拆成兩部分: 比m大( )
及不比m大的( )
7. if , 找 中第 小的
8. if , 找 中第 小的
Θ 1 Θ
Θ T 5
Θ
Max: T
Θ Θ
5 Θ 7
10 5
7
10 Θ Θ