SlideShare a Scribd company logo
Matrix Multiplication Using
Divide And Conquer
1
Presented by: Ayush Jajoo
Roll No : MCA25016/22
MCA-Second Semester
Presented To: Dr. Madhavi Sinha
Matrix multiplication
Matrix multiplication is an important operation in many
mathematical and image processing applications. Suppose
we want to multiply two matrices A and B, each of size n x
n, multiplication is operation is defined as
C11 = A11 B11 + A12 B21
C12 = A11 B12 + A12 B22
C21 = A21 B11 + A22 B21
C22 = A21 B12 + A22 B22
2
Algorithm for matrix multiplication
Algorithm MATRIX_MULTIPLICATION(A, B, C)
// A and B are input matrices of size n x n
// C is the output matrix of size n x n
for i ← 1 to n do
for j ← 1 to n do
C[i][j] ← 0
for k ← 1 to n do
C[i][j] ← C[i][j] + A[i][k]*B[k][j]
end
end
end
3
4
Divide-and-Conquer
 Divide-and conquer is a general algorithm
design paradigm:
• Divide: divide the input data S in two or more
disjoint subsets S1, S2, …
• Recur: solve the subproblems recursively
• Conquer: combine the solutions for S1, S2, …, into a
solution for S
 The base case for the recursion are
subproblems of constant size
 Analysis can be done using recurrence
equations
5
Divide-and-Conquer
The most-well known algorithm design strategy:
1. Divide instance of problem into two or more
smaller instances
2. Solve smaller instances recursively
3. Obtain solution to original (larger) instance by
combining these solutions
6
Divide-and-Conquer Technique (cont.)
subproblem 2
of size n/2
subproblem 1
of size n/2
a solution to
subproblem 1
a solution to
the original problem
a solution to
subproblem 2
a problem of size n
7
Divide-and-Conquer Examples
 Matrix multiplication: Strassen’s algorithm
 Example for matrix Multiplication
8
Strassens’s Matrix Multiplication
 Belongs to divide and conquer Approach
 Strassen has used some formulas for multiplying the two 2*2
dimension matrices where the number of multiplications is
seven, additions and subtractions are is eighteen, and in brute
force algorithm, there is eight number of multiplications and four
addition.
9
Divide and Conquer Matrix Multiply
 =
a0 b0 a0  b0
A  B = R
• Terminate recursion with a simple base case
10
Divide and Conquer Matrix Multiply
A  B = R
A0 A1
A2 A3
B0 B1
B2 B3
A0B0+A1B2 A0B1+A1B3
A2B0+A3B2 A2B1+A3B3
 =
•Divide matrices into sub-matrices: A0 , A1, A2 etc
•Use blocked matrix multiply equations
•Recursively multiply sub-matrices
11
Basic Matrix Multiplication
Suppose we want to multiply two matrices of size N
x N: for example A x B = C.
C11 = a11b11 + a12b21
C12 = a11b12 + a12b22
C21 = a21b11 + a22b21
C22 = a21b12 + a22b22
2x2 matrix multiplication can be
accomplished in 8 multiplication.(2log
2
8 =23)
12
Strassens’s Matrix Multiplication
P1 = (A11+ A22)(B11+B22)
P2 = (A21 + A22) * B11
P3 = A11 * (B12 - B22)
P4 = A22 * (B21 - B11)
P5 = (A11 + A12) * B22
P6 = (A21 - A11) * (B11 + B12)
P7 = (A12 - A22) * (B21 + B22)
C11 = P1 + P4 - P5 + P7
C12 = P3 + P5
C21 = P2 + P4
C22 = P1 + P3 - P2 + P6
13
C11 = P1 + P4 - P5 + P7
= (A11+ A22)(B11+B22) + A22 * (B21 - B11) - (A11 + A12) * B22+
(A12 - A22) * (B21 + B22)
= A11 B11 + A11 B22 + A22 B11 + A22 B22 + A22 B21 – A22 B11 -
A11 B22 -A12 B22 +A12 B21 + A12 B22 – A22 B21 – A22 B22
= A11 B11 +A12 B21
Comparison
14
Strassen Algorithm
void matmul(int *A, int *B, int *R, int n) {
if (n == 1) {
(*R) += (*A) * (*B);
} else {
matmul(A, B, R, n/4);
matmul(A, B+(n/4), R+(n/4), n/4);
matmul(A+2*(n/4), B, R+2*(n/4), n/4);
matmul(A+2*(n/4), B+(n/4), R+3*(n/4), n/4);
matmul(A+(n/4), B+2*(n/4), R, n/4);
matmul(A+(n/4), B+3*(n/4), R+(n/4), n/4);
matmul(A+3*(n/4), B+2*(n/4), R+2*(n/4), n/4);
matmul(A+3*(n/4), B+3*(n/4), R+3*(n/4), n/4);
}
Divide matrices in
sub-matrices and
recursively multiply
sub-matrices
15
Formulas for Strassen’s Algorithm
M1 = (A00 + A11)  (B00 + B11)
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)
16
Strassen’s Matrix Multiplication
Strassen observed [1969] that the product of two matrices can
be computed as follows:
C00 C01 A00 A01 B00 B01
= *
C10 C11 A10 A11 B10 B11
M1 + M4 - M5 + M7 M3 + M5
=
M2 + M4 M1 + M3 - M2 + M6
17
Analysis of Strassen’s Algorithm
If n is not a power of 2, matrices can be padded with zeros.
Number of multiplications:
M(n) = 7M(n/2), M(1) = 1
Solution: M(n) = 7log 2n = nlog 27 ≈ n2.807 vs. n3 of brute-force alg.
Algorithms with better asymptotic efficiency are known but they
are even more complex.
18
Time Analysis
Example for matrix multiplication
Example cont…
20
Example cont.
21
Example cont.
22
23
Basic Matrix Multiplication
)
(
)
(
Thus 3
1 1
3
1
,
1
,
,
N
O
cN
c
N
T
b
a
C
N
i
N
j
N
k
j
k
N
k
k
i
j
i






  

void matrix_mult (){
for (i = 1; i <= N; i++) {
for (j = 1; j <= N; j++) {
compute Ci,j;
}
}}
algorithm
Time analysis
24
Time Complexity
25
Thank you

More Related Content

Similar to Ayush Jajoo(MCA2501622) AOA .pptx

Linear Algebra Assignment help
Linear Algebra Assignment helpLinear Algebra Assignment help
Linear Algebra Assignment help
Maths Assignment Help
 
Using Formulae Ppp Spm 2009 Ppsx
Using Formulae Ppp Spm 2009 PpsxUsing Formulae Ppp Spm 2009 Ppsx
Using Formulae Ppp Spm 2009 PpsxSaripah Ahmad Mozac
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
Damian T. Gordon
 
Assignments for class XII
Assignments for class XIIAssignments for class XII
Assignments for class XII
indu thakur
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Emmanuel college
 
matrices basic operation.ppt
matrices basic operation.pptmatrices basic operation.ppt
matrices basic operation.ppt
Mahesh Kumar Lohano
 
Lesson 1 matrix
Lesson 1 matrixLesson 1 matrix
Lesson 1 matrix
Melvy Dela Torre
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programmingDamian T. Gordon
 
9.matrices and transformation Further Mathematics Zimbabwe Zimsec Cambridge
9.matrices and transformation   Further Mathematics Zimbabwe Zimsec Cambridge9.matrices and transformation   Further Mathematics Zimbabwe Zimsec Cambridge
9.matrices and transformation Further Mathematics Zimbabwe Zimsec Cambridge
alproelearning
 
Number theory
Number theoryNumber theory
Number theory
dhivyakesavan3
 
Assessments for class xi
Assessments  for class  xi Assessments  for class  xi
Assessments for class xi indu psthakur
 
tutorial5.ppt
tutorial5.ppttutorial5.ppt
tutorial5.ppt
jvjfvvoa
 
Solution of matlab chapter 1
Solution of matlab chapter 1Solution of matlab chapter 1
Solution of matlab chapter 1
AhsanIrshad8
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
giridaroori
 
MATRICES maths project.pptxsgdhdghdgf gr to f HR f
MATRICES maths project.pptxsgdhdghdgf gr to f HR fMATRICES maths project.pptxsgdhdghdgf gr to f HR f
MATRICES maths project.pptxsgdhdghdgf gr to f HR f
premkumar24914
 
Banco de preguntas para el ap
Banco de preguntas para el apBanco de preguntas para el ap
Banco de preguntas para el ap
MARCELOCHAVEZ23
 
Indefinite Integration One shot Revision
Indefinite Integration One shot Revision Indefinite Integration One shot Revision
Indefinite Integration One shot Revision
CHANDHINIJAYARAMAN
 
matrices and determinantes
matrices and determinantes matrices and determinantes
matrices and determinantes
gandhinagar
 

Similar to Ayush Jajoo(MCA2501622) AOA .pptx (20)

2.ppt
2.ppt2.ppt
2.ppt
 
Linear Algebra Assignment help
Linear Algebra Assignment helpLinear Algebra Assignment help
Linear Algebra Assignment help
 
Using Formulae Ppp Spm 2009 Ppsx
Using Formulae Ppp Spm 2009 PpsxUsing Formulae Ppp Spm 2009 Ppsx
Using Formulae Ppp Spm 2009 Ppsx
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Assignments for class XII
Assignments for class XIIAssignments for class XII
Assignments for class XII
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
matrices basic operation.ppt
matrices basic operation.pptmatrices basic operation.ppt
matrices basic operation.ppt
 
Lesson 1 matrix
Lesson 1 matrixLesson 1 matrix
Lesson 1 matrix
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programming
 
9.matrices and transformation Further Mathematics Zimbabwe Zimsec Cambridge
9.matrices and transformation   Further Mathematics Zimbabwe Zimsec Cambridge9.matrices and transformation   Further Mathematics Zimbabwe Zimsec Cambridge
9.matrices and transformation Further Mathematics Zimbabwe Zimsec Cambridge
 
Number theory
Number theoryNumber theory
Number theory
 
Ch04
Ch04Ch04
Ch04
 
Assessments for class xi
Assessments  for class  xi Assessments  for class  xi
Assessments for class xi
 
tutorial5.ppt
tutorial5.ppttutorial5.ppt
tutorial5.ppt
 
Solution of matlab chapter 1
Solution of matlab chapter 1Solution of matlab chapter 1
Solution of matlab chapter 1
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 
MATRICES maths project.pptxsgdhdghdgf gr to f HR f
MATRICES maths project.pptxsgdhdghdgf gr to f HR fMATRICES maths project.pptxsgdhdghdgf gr to f HR f
MATRICES maths project.pptxsgdhdghdgf gr to f HR f
 
Banco de preguntas para el ap
Banco de preguntas para el apBanco de preguntas para el ap
Banco de preguntas para el ap
 
Indefinite Integration One shot Revision
Indefinite Integration One shot Revision Indefinite Integration One shot Revision
Indefinite Integration One shot Revision
 
matrices and determinantes
matrices and determinantes matrices and determinantes
matrices and determinantes
 

Recently uploaded

原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
u86oixdj
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
vikram sood
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
mbawufebxi
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
74nqk8xf
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
v3tuleee
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
balafet
 
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptxData_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
AnirbanRoy608946
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
manishkhaire30
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
Subhajit Sahu
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
John Andrews
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
axoqas
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
oz8q3jxlp
 
The Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series DatabaseThe Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series Database
javier ramirez
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
rwarrenll
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
sameer shah
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
ahzuo
 

Recently uploaded (20)

原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
 
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptxData_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
 
The Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series DatabaseThe Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series Database
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
 

Ayush Jajoo(MCA2501622) AOA .pptx

  • 1. Matrix Multiplication Using Divide And Conquer 1 Presented by: Ayush Jajoo Roll No : MCA25016/22 MCA-Second Semester Presented To: Dr. Madhavi Sinha
  • 2. Matrix multiplication Matrix multiplication is an important operation in many mathematical and image processing applications. Suppose we want to multiply two matrices A and B, each of size n x n, multiplication is operation is defined as C11 = A11 B11 + A12 B21 C12 = A11 B12 + A12 B22 C21 = A21 B11 + A22 B21 C22 = A21 B12 + A22 B22 2
  • 3. Algorithm for matrix multiplication Algorithm MATRIX_MULTIPLICATION(A, B, C) // A and B are input matrices of size n x n // C is the output matrix of size n x n for i ← 1 to n do for j ← 1 to n do C[i][j] ← 0 for k ← 1 to n do C[i][j] ← C[i][j] + A[i][k]*B[k][j] end end end 3
  • 4. 4 Divide-and-Conquer  Divide-and conquer is a general algorithm design paradigm: • Divide: divide the input data S in two or more disjoint subsets S1, S2, … • Recur: solve the subproblems recursively • Conquer: combine the solutions for S1, S2, …, into a solution for S  The base case for the recursion are subproblems of constant size  Analysis can be done using recurrence equations
  • 5. 5 Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain solution to original (larger) instance by combining these solutions
  • 6. 6 Divide-and-Conquer Technique (cont.) subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to the original problem a solution to subproblem 2 a problem of size n
  • 7. 7 Divide-and-Conquer Examples  Matrix multiplication: Strassen’s algorithm  Example for matrix Multiplication
  • 8. 8 Strassens’s Matrix Multiplication  Belongs to divide and conquer Approach  Strassen has used some formulas for multiplying the two 2*2 dimension matrices where the number of multiplications is seven, additions and subtractions are is eighteen, and in brute force algorithm, there is eight number of multiplications and four addition.
  • 9. 9 Divide and Conquer Matrix Multiply  = a0 b0 a0  b0 A  B = R • Terminate recursion with a simple base case
  • 10. 10 Divide and Conquer Matrix Multiply A  B = R A0 A1 A2 A3 B0 B1 B2 B3 A0B0+A1B2 A0B1+A1B3 A2B0+A3B2 A2B1+A3B3  = •Divide matrices into sub-matrices: A0 , A1, A2 etc •Use blocked matrix multiply equations •Recursively multiply sub-matrices
  • 11. 11 Basic Matrix Multiplication Suppose we want to multiply two matrices of size N x N: for example A x B = C. C11 = a11b11 + a12b21 C12 = a11b12 + a12b22 C21 = a21b11 + a22b21 C22 = a21b12 + a22b22 2x2 matrix multiplication can be accomplished in 8 multiplication.(2log 2 8 =23)
  • 12. 12 Strassens’s Matrix Multiplication P1 = (A11+ A22)(B11+B22) P2 = (A21 + A22) * B11 P3 = A11 * (B12 - B22) P4 = A22 * (B21 - B11) P5 = (A11 + A12) * B22 P6 = (A21 - A11) * (B11 + B12) P7 = (A12 - A22) * (B21 + B22) C11 = P1 + P4 - P5 + P7 C12 = P3 + P5 C21 = P2 + P4 C22 = P1 + P3 - P2 + P6
  • 13. 13 C11 = P1 + P4 - P5 + P7 = (A11+ A22)(B11+B22) + A22 * (B21 - B11) - (A11 + A12) * B22+ (A12 - A22) * (B21 + B22) = A11 B11 + A11 B22 + A22 B11 + A22 B22 + A22 B21 – A22 B11 - A11 B22 -A12 B22 +A12 B21 + A12 B22 – A22 B21 – A22 B22 = A11 B11 +A12 B21 Comparison
  • 14. 14 Strassen Algorithm void matmul(int *A, int *B, int *R, int n) { if (n == 1) { (*R) += (*A) * (*B); } else { matmul(A, B, R, n/4); matmul(A, B+(n/4), R+(n/4), n/4); matmul(A+2*(n/4), B, R+2*(n/4), n/4); matmul(A+2*(n/4), B+(n/4), R+3*(n/4), n/4); matmul(A+(n/4), B+2*(n/4), R, n/4); matmul(A+(n/4), B+3*(n/4), R+(n/4), n/4); matmul(A+3*(n/4), B+2*(n/4), R+2*(n/4), n/4); matmul(A+3*(n/4), B+3*(n/4), R+3*(n/4), n/4); } Divide matrices in sub-matrices and recursively multiply sub-matrices
  • 15. 15 Formulas for Strassen’s Algorithm M1 = (A00 + A11)  (B00 + B11) 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)
  • 16. 16 Strassen’s Matrix Multiplication Strassen observed [1969] that the product of two matrices can be computed as follows: C00 C01 A00 A01 B00 B01 = * C10 C11 A10 A11 B10 B11 M1 + M4 - M5 + M7 M3 + M5 = M2 + M4 M1 + M3 - M2 + M6
  • 17. 17 Analysis of Strassen’s Algorithm If n is not a power of 2, matrices can be padded with zeros. Number of multiplications: M(n) = 7M(n/2), M(1) = 1 Solution: M(n) = 7log 2n = nlog 27 ≈ n2.807 vs. n3 of brute-force alg. Algorithms with better asymptotic efficiency are known but they are even more complex.
  • 19. Example for matrix multiplication
  • 23. 23 Basic Matrix Multiplication ) ( ) ( Thus 3 1 1 3 1 , 1 , , N O cN c N T b a C N i N j N k j k N k k i j i           void matrix_mult (){ for (i = 1; i <= N; i++) { for (j = 1; j <= N; j++) { compute Ci,j; } }} algorithm Time analysis