VOOZH about

URL: https://www.geeksforgeeks.org/dsa/matrix-multiplication-recursive/

⇱ Matrix Multiplication | Recursive - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Matrix Multiplication | Recursive

Last Updated : 22 Dec, 2023

Given two matrices A and B. The task is to multiply matrix A and matrix B recursively. If matrix A and matrix B are not multiplicative compatible, then generate output "Not Possible".

Examples :

Input: A = 12 56
45 78
B = 2 6
5 8
Output: 304 520
480 894
Input: A = 1 2 3
4 5 6
7 8 9
B = 1 2 3
4 5 6
7 8 9
Output: 30 36 42
66 81 96
102 126 150

It is recommended to first refer Iterative Matrix Multiplication.

First check if multiplication between matrices is possible or not. For this, check if number of columns of first matrix is equal to number of rows of second matrix or not. If both are equal than proceed further otherwise generate output "Not Possible".

In Recursive Matrix Multiplication, we implement three loops of Iteration through recursive calls. The inner most Recursive call of multiplyMatrix() is to iterate k (col1 or row2). The second recursive call of multiplyMatrix() is to change the columns and the outermost recursive call is to change rows.

Below is Recursive Matrix Multiplication code. 


Output
30 36 42 
66 81 96 
102 126 150 

Time Complexity: O(row1 * col2* col1)
Auxiliary Space: O(log (max(row1,col2)), As implicit stack is used due to recursion

Comment
Article Tags:
Article Tags: