Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Dynamic-Programming/MatrixMultiplication.js
Original file line number Diff line number Diff line change
@@ -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 };
71 changes: 71 additions & 0 deletions Dynamic-Programming/tests/MatrixMultiplication.test.js
Original file line number Diff line number Diff line change
@@ -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'
)
})
Loading