Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.8k
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:masterChoose a base branch fromMishtiGarg250:Adding-Algo
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| 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 }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| 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' | ||
| ) | ||
| }) |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.