Michael Tsai 2011/03/04
矩陣相乘
問題 :
Input: A, B 都是 n x n 的 Square Matrix
Ouput: C, n x n 的 square matrix,
矩陣相乘 – 基本法
[ � � �1 12131 � � �
122232 � � �
132333][ � � �1 12131 � � �
122232 � � �
132333] = [ � � �112131 � � �
122232 � � �
132333]
� � �
122232� � �
132333] = [ � � �112131 � � �
122232 � � �
132333]
�
��= ∑
�=1
�
�
��∙ �
��
n 個 n 個
n 個乘法 , n-1 個 加法 ,產生了一個 entry, 共有 個 entries
Running time = ? Θ(
�
3)
矩陣相乘 – D&C 嘗試一
�
12�
11�
21
�
22�
12�
11�
21
�
22�
12�
11�
21�
22�
11= �
11�
11+ �
12�
21從可得
從可得
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 matric es
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 Recursiv
e case
8� (�
2) Combin
e
�
( 1 ) =Θ(1) Θ(1)
Θ(
�
2)
� (�)=Θ(1)+8�
(
�2)
+Θ(�2)=8�(
�2)
+Θ(�2)� (�)=
{
8�(
�2Θ (1))
+Θ (�2) ,if ,if� (� )=Θ
( �
3)
矩陣相乘 – 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)
Θ(
�
2)
7� (�
2)
Θ(�
2)
� (�)=Θ(1)+Θ(�2)+7�
(
�2)
+Θ(�2)=7�(
�2)
+Θ(�2)�
( � ) =Θ ( �
log7)
矩陣相乘 – 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 algorith ms.
目前所知 , the most asymptotically efficient algorithm ha s a running time of (Coppersmith and Winograd)
接下來…
問題 : 我們要怎麼解遞迴式 ?
取代法
遞迴樹法
大師定理法
http://www.origin-zero.com/senzi/JOKE1.jpg
取代法
猜答案的形式 用數學歸納法證明此
形式成立 得到遞迴式的解
成功
失敗
取代法 - 例子
問題 :
猜測 :
用歸納法證明 , for a constant
假設上面的 bound 在 m<n 時成立
則時亦成立 .
也就是說
取代法 - 例子
as long as
栗子助教
舉個栗子
取代法 - 例子
接著必須也證明邊界條件成立 .
有時候需要多一點努力… .
假設 .
我們必須證明
天不從人願 : 時 ,
娃 ~
取代法 - 例子
事實上 , 我們只須證明 , 當時 , 即可 . ( 把 不聽話的拔掉 )
從原本的遞迴式 , 我們可以得到 .
接著設 . 我們發現 :
&
( 只要 ) 至此可以使邊界條件成立 .
喔耶 .
取代法 - 怎麼猜 ?
靠經驗 .
跟沒講一樣 .
一些小方法 :
1. 根據以前看過類似的遞迴式來猜測
2. 使用等一下要介紹的遞迴樹
3. 證明比較鬆的 upper bound 或 lower bound 來慢 慢接近 tight bound
� (�)=2�
(
⌊ �2 ⌋ +17)
+�老工匠
取代法 - 小技巧 1
題目 :
猜測 :
歸納法證明 :
爛掉了…
取代法 - 小技巧 1
方法 : 改使用 ,
( 減掉一個 order 較低的 term)
(as long as )
( 然後繼續選 c, 使得 boundary condition 成 立 , 在此省略 )
取代法–小技巧 2
看起來挺嚇人的 :
替換變數 :
定義 :
暫時不管 flooring
遞迴樹法
畫出遞迴樹用數學歸納法證明此 解成立
用比較不嚴謹的方法 加總得到解
例子 :
� (�)
� (� 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)
� �
23
16
� �
2(
16 3 )
2� �
2Θ
( �
log43)
level
level
level
level
個 node
遞迴樹法–例子 1
用歸納法證明 : for some .
as long as
遞迴樹法–例子 2
例子 :
請一位同學上來畫遞迴樹 ( 有點跛腳的遞迴 樹 )
遞迴樹法–例子 2
歸納法證明 :
as long as
Master Theorem:
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 or . Then has the following asymptotic bounds:
1.
if for some constant , then
2.
if , then
3.
if for some constant , and if for some consta nt and all sufficiently large , then
大師定理
1. if for some constant , then
2. if , then
3. if for some constant , and if for some con stant and all sufficiently large , then
� (�)
� log
��
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!
a=9, b=3, f(n)=n
satisfies case 1:
so
For your reference:
舉個栗子
� (�)
�
log��大師定理 - 例子 2
a=1, b=3/2, f(n)=1
satisfies case 2:
For your reference:
� (�)
�
log��大師定理 - 更多例子
上台解題時間…
取數問題
Selection Problem
問題 :
Input: n 個數字之集合
Output: 取出此 n 個數字之中位數
中位數之定義 : n 個數字中第 k= 小的數字
取數問題
菜瓜布解法 :
先把 n 個數 sort 好
從最小的數過去算到第小的數字即為答案
running time=?
Can we do a better job?
取數問題
n 個數
小於等於 a 的
某數 a (pivot)
大於 a 的
�
1�
2if 找中第小的
if 找中第小的
取數問題
下一個問題 : 怎麼選 a?
選不好的話…
最好是可以平均分成兩分 .
那就是選中位數 .
咦 , 我們不是就要找中位數嗎 ?
能不能花少一點時間 , 找個”差不多”的中位數
取數問題
差不多的中位數 :
1. 把 n 個數分成很多大小為 5 個的 sub list ( 大約共有 n/5 個 sub list
2. 這些 sub list 中各自找中位數
3. 找出 n/5 個中位數中的中位數
此為差不多的中位數
取數問題
有多差不多呢 ?
比”差不多中位數”
小的至少有
個中位數
中位數的中位數
3 n 5 10
取數問題
Algorithm:
1.
if then 直接找出其中位數
2.
else
3.
把數列拆成個大小為 5 的小數列
4.
每個小數列找出其中位數
5.
找出個中位數的中位數 m
6.
用此中位數把原本的數列拆成兩部分 : 比 m 大 () 及不 比 m 大的 ()
7.
if 找中第小的
8.
if 找中第小的
Θ(1)
Θ(�)
Θ(�)
T (� 5 )
Θ(�)
Max:
�
( � ) =Θ ( � ) +Θ ( � ) + � ( � 5 ) + Θ ( � ) +� ( 7 10 � ) �
( � ) =Θ ( � )
下一次…
Programming on a piece of paper (well, no t exactly programming)
Quicksort running time analysis: revisit
…
作業一上線
5 problem sets, 100 points
1 programming assignment, 4 writing assig nments
Judge girl system accounts are sent last night
Please give it a try
Due 2 weeks from yesterday