Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added matrix multiplication algo in dynamic programming#1830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
MishtiGarg250 wants to merge1 commit intoTheAlgorithms:master
base:master
Choose a base branch
Loading
fromMishtiGarg250:Adding-Algo
Open
Show file tree
Hide file tree
Changes fromall 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 deletionsDynamic-Programming/MatrixMultiplication.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsDynamic-Programming/tests/MatrixMultiplication.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp