Matrix Multiplication | Recursive
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”.
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.
30 36 42 66 81 96 102 126 150
Time Complexity: O(row1 * col2)
Auxiliary Space: O(log (max(row1,col2)), As implicit stack is used due to recursion
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.