From 3d8f28c3a7e7d6e5d9a1d36b168f5ce6b9b64309 Mon Sep 17 00:00:00 2001 From: MishtiGarg250 Date: Wed, 8 Oct 2025 08:38:13 +0530 Subject: [PATCH] Added matrix multiplication algo in dynamic programming --- Dynamic-Programming/MatrixMultiplication.js | 63 ++++++++++++++++ .../tests/MatrixMultiplication.test.js | 71 +++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 Dynamic-Programming/MatrixMultiplication.js create mode 100644 Dynamic-Programming/tests/MatrixMultiplication.test.js diff --git a/Dynamic-Programming/MatrixMultiplication.js b/Dynamic-Programming/MatrixMultiplication.js new file mode 100644 index 0000000000..ea7ee8c771 --- /dev/null +++ b/Dynamic-Programming/MatrixMultiplication.js @@ -0,0 +1,63 @@ +/* +Wikipedia -> https://en.wikipedia.org/wiki/Matrix_multiplication + +Q. -> Given two matrices `A` and `B`, find the product of both matrices. +Matrix multiplication is only possible when the number of columns in A equals the number of rows in B. + +Formula -> +If A is of size (n × m) and B is of size (m × p), +then the result matrix C will be of size (n × p), +where each element C[i][j] = Σ (A[i][k] * B[k][j]) for k = 0 to m-1. + +Algorithm details -> +time complexity - O(n * m * p) +space complexity - O(n * p) +*/ + +const matrixMultiplication = (A, B) => { + const n = A.length; // rows in A + const m = A[0].length; // columns in A (and rows in B) + const p = B[0].length; // columns in B + + // Check if multiplication is possible + if (m !== B.length) { + throw new Error('Matrix multiplication not possible: columns of A must equal rows of B'); + } + + // Create a result matrix filled with 0s of size (n × p) + const result = new Array(n).fill(0).map(() => new Array(p).fill(0)); + + /* + Perform matrix multiplication: + For each row in A and column in B, + multiply and sum corresponding elements. + */ + for (let i = 0; i < n; i++) { + for (let j = 0; j < p; j++) { + let sum = 0; + for (let k = 0; k < m; k++) { + sum += A[i][k] * B[k][j]; + } + result[i][j] = sum; + } + } + + return result; +}; + +// Example usage +const A = [ + [1, 2, 3], + [4, 5, 6] +]; + +const B = [ + [7, 8], + [9, 10], + [11, 12] +]; + +console.log(matrixMultiplication(A, B)); +// Output: [ [ 58, 64 ], [ 139, 154 ] ] + +export { matrixMultiplication }; diff --git a/Dynamic-Programming/tests/MatrixMultiplication.test.js b/Dynamic-Programming/tests/MatrixMultiplication.test.js new file mode 100644 index 0000000000..fff2d2f06e --- /dev/null +++ b/Dynamic-Programming/tests/MatrixMultiplication.test.js @@ -0,0 +1,71 @@ +import { matrixMultiplication } from '../MatrixMultiplication' + +test('matrixMultiplication([[1,2,3],[4,5,6]], [[7,8],[9,10],[11,12]]) => [[58,64],[139,154]]', () => { + const A = [ + [1, 2, 3], + [4, 5, 6] + ] + const B = [ + [7, 8], + [9, 10], + [11, 12] + ] + const res = matrixMultiplication(A, B) + expect(res).toEqual([ + [58, 64], + [139, 154] + ]) +}) + +test('matrixMultiplication([[2,4],[3,4]], [[1,2],[1,3]]) => [[6,16],[7,18]]', () => { + const A = [ + [2, 4], + [3, 4] + ] + const B = [ + [1, 2], + [1, 3] + ] + const res = matrixMultiplication(A, B) + expect(res).toEqual([ + [6, 16], + [7, 18] + ]) +}) + +test('matrixMultiplication([[1,2],[3,4]], [[5,6],[7,8]]) => [[19,22],[43,50]]', () => { + const A = [ + [1, 2], + [3, 4] + ] + const B = [ + [5, 6], + [7, 8] + ] + const res = matrixMultiplication(A, B) + expect(res).toEqual([ + [19, 22], + [43, 50] + ]) +}) + +test('matrixMultiplication([[1]], [[2]]) => [[2]]', () => { + const A = [[1]] + const B = [[2]] + const res = matrixMultiplication(A, B) + expect(res).toEqual([[2]]) +}) + +test('throws error when matrices cannot be multiplied', () => { + const A = [ + [1, 2, 3], + [4, 5, 6] + ] + const B = [ + [1, 2], + [3, 4] + ] + expect(() => matrixMultiplication(A, B)).toThrow( + 'Matrix multiplication not possible: columns of A must equal rows of B' + ) +})