Divide and Conquer3-1
Divide and Conquer3-1
Divide and Conquer3-1
2
Algorithm
SELECTION-SORT(A)
n ← length[A]
for j ← 1 to n - 1
do smallest ← j
for i ← j + 1 to n
do if A[i] < A[smallest]
then smallest ← i
exchange A[j] ↔ A[smallest]
3
Analysis of Selection sort
Selection Sort(A) Cost Times
n ← length[A] C1 1
for j ← 1 to n - 1 C2 n
do smallest ← j C3 (n-1)
n 1
for i ← j + 1 to n C4 (n j 1)
j 1
then smallest ← i C6
n 1
j 1
(n j )
exchange A[j] ↔ A[smallest] C7 (n-1)
4
Strassen’s matrix multiplication
The problem:
n n
Multiply two matrices A and B, each of size
A B C
nn nn nn
5
The conventional method
n
C ij Aik Bkj
k 1 Time complexity =O(N3)
6
The divide and conquer approach
ae+bg af+bh a b e f
= *
ce+dg cf+dh c d g h 8 multiplications
C A B
7
Strassen’s Matrix Multiplication
c00 c01 a00 a01 b00 b01
= *
c10 c11 a10 a11 b10 b11
7 multiplications
m1+ m4- m5 + m7 m3 + m5
=
m2 + m4 m1+ m3 - m2 + m6
6 additions
m1 = (a00 + a11) * (b00 + b11)
4 subtractions
m2 = (a10 + a11) * b00
m3 = a00 * (b01 - b11)
m4 = a11 * (b10 - b00)
m5 = (a00 + a01) * b11
m6 = (a10 - a00) * (b00 + b01)
m7 = (a01 - a11) * (b10 + b11)
8
Analysis of Strassen’s method
Strassen’s matrix multiplication has better asymptotic efficiency but is not used
in practice.
9
Reasons
10