Skip to content

Commit 3d8f28c

Browse files
committed
Added matrix multiplication algo in dynamic programming
1 parent 08d8c6b commit 3d8f28c

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Wikipedia -> https://en.wikipedia.org/wiki/Matrix_multiplication
3+
4+
Q. -> Given two matrices `A` and `B`, find the product of both matrices.
5+
Matrix multiplication is only possible when the number of columns in A equals the number of rows in B.
6+
7+
Formula ->
8+
If A is of size (n × m) and B is of size (m × p),
9+
then the result matrix C will be of size (n × p),
10+
where each element C[i][j] = Σ (A[i][k] * B[k][j]) for k = 0 to m-1.
11+
12+
Algorithm details ->
13+
time complexity - O(n * m * p)
14+
space complexity - O(n * p)
15+
*/
16+
17+
const matrixMultiplication = (A, B) => {
18+
const n = A.length; // rows in A
19+
const m = A[0].length; // columns in A (and rows in B)
20+
const p = B[0].length; // columns in B
21+
22+
// Check if multiplication is possible
23+
if (m !== B.length) {
24+
throw new Error('Matrix multiplication not possible: columns of A must equal rows of B');
25+
}
26+
27+
// Create a result matrix filled with 0s of size (n × p)
28+
const result = new Array(n).fill(0).map(() => new Array(p).fill(0));
29+
30+
/*
31+
Perform matrix multiplication:
32+
For each row in A and column in B,
33+
multiply and sum corresponding elements.
34+
*/
35+
for (let i = 0; i < n; i++) {
36+
for (let j = 0; j < p; j++) {
37+
let sum = 0;
38+
for (let k = 0; k < m; k++) {
39+
sum += A[i][k] * B[k][j];
40+
}
41+
result[i][j] = sum;
42+
}
43+
}
44+
45+
return result;
46+
};
47+
48+
// Example usage
49+
const A = [
50+
[1, 2, 3],
51+
[4, 5, 6]
52+
];
53+
54+
const B = [
55+
[7, 8],
56+
[9, 10],
57+
[11, 12]
58+
];
59+
60+
console.log(matrixMultiplication(A, B));
61+
// Output: [ [ 58, 64 ], [ 139, 154 ] ]
62+
63+
export { matrixMultiplication };
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { matrixMultiplication } from '../MatrixMultiplication'
2+
3+
test('matrixMultiplication([[1,2,3],[4,5,6]], [[7,8],[9,10],[11,12]]) => [[58,64],[139,154]]', () => {
4+
const A = [
5+
[1, 2, 3],
6+
[4, 5, 6]
7+
]
8+
const B = [
9+
[7, 8],
10+
[9, 10],
11+
[11, 12]
12+
]
13+
const res = matrixMultiplication(A, B)
14+
expect(res).toEqual([
15+
[58, 64],
16+
[139, 154]
17+
])
18+
})
19+
20+
test('matrixMultiplication([[2,4],[3,4]], [[1,2],[1,3]]) => [[6,16],[7,18]]', () => {
21+
const A = [
22+
[2, 4],
23+
[3, 4]
24+
]
25+
const B = [
26+
[1, 2],
27+
[1, 3]
28+
]
29+
const res = matrixMultiplication(A, B)
30+
expect(res).toEqual([
31+
[6, 16],
32+
[7, 18]
33+
])
34+
})
35+
36+
test('matrixMultiplication([[1,2],[3,4]], [[5,6],[7,8]]) => [[19,22],[43,50]]', () => {
37+
const A = [
38+
[1, 2],
39+
[3, 4]
40+
]
41+
const B = [
42+
[5, 6],
43+
[7, 8]
44+
]
45+
const res = matrixMultiplication(A, B)
46+
expect(res).toEqual([
47+
[19, 22],
48+
[43, 50]
49+
])
50+
})
51+
52+
test('matrixMultiplication([[1]], [[2]]) => [[2]]', () => {
53+
const A = [[1]]
54+
const B = [[2]]
55+
const res = matrixMultiplication(A, B)
56+
expect(res).toEqual([[2]])
57+
})
58+
59+
test('throws error when matrices cannot be multiplied', () => {
60+
const A = [
61+
[1, 2, 3],
62+
[4, 5, 6]
63+
]
64+
const B = [
65+
[1, 2],
66+
[3, 4]
67+
]
68+
expect(() => matrixMultiplication(A, B)).toThrow(
69+
'Matrix multiplication not possible: columns of A must equal rows of B'
70+
)
71+
})

0 commit comments

Comments
 (0)