Matrix Chain Multiplication by using JavaScript Last Updated : 11 Sep, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we are going to learn about Matrix Chain Multiplication by using JavaScript, Matrix Chain Multiplication refers to finding the most efficient way to multiply a sequence of matrices together, minimizing the total number of scalar multiplications required. Approaches to perform Matrix Chain Multiplication by using JavaScriptRecursive Solution (Naive)Top-Down MemoizationWe will explore all the above methods along with their basic implementation with the help of examples. Approach 1: Recursive Solution (Naive)The recursive solution (naive) for matrix chain multiplication repeatedly splits the chain at different positions and calculates the cost, returning the minimum number of multiplications needed. Example: JavaScript function matrixChain(p, i, j) { if (i === j) return 0; let minCost = Number.MAX_SAFE_INTEGER; for (let k = i; k < j; k++) { let cost = matrixChain(p, i, k) + matrixChain(p, k + 1, j) + p[i - 1] * p[k] * p[j]; if (cost < minCost) { minCost = cost; } } return minCost; } const dimensions = [10, 30, 5, 60]; const result = matrixChain(dimensions, 1, dimensions.length - 1); console.log("Minimum number of multiplications:", result); OutputMinimum number of multiplications: 4500Approach 2: Top-Down MemoizationTop-Down Memoization for matrix chain multiplication optimizes by storing computed results, reducing redundant calculations, and recursively finding the minimum multiplications needed for matrix chain ordering. Syntax: Example: JavaScript function matrixChain(p) { const n = p.length; const memo = Array.from({ length: n }, () => Array(n).fill(-1)); function recursive(i, j) { if (i === j) return 0; if (memo[i][j] !== -1) return memo[i][j]; let minCost = Number.MAX_SAFE_INTEGER; for (let k = i; k < j; k++) { let cost = recursive(i, k) + recursive(k + 1, j) + p[i - 1] * p[k] * p[j]; if (cost < minCost) minCost = cost; } memo[i][j] = minCost; return minCost; } return recursive(1, n - 1); } const matrix = [10, 20, 25, 60]; const result = matrixChain(matrix); console.log("Minimum number of multiplications:", result); OutputMinimum number of multiplications: 20000 Comment More infoAdvertise with us Next Article Matrix Chain Multiplication by using JavaScript V vishalkumar2204 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-DSA JavaScript-Program Similar Reads Largest Square Formed in a Matrix using JavaScript Given a matrix, our task is to find the largest square formed in a matrix using JavaScript. The largest square is a square that contains the same number of rows and columns and no more squares can be possible to form in that matrix that contains more rows and columns than this square. The below meth 3 min read JavaScript Program for Multiplication of Two Numbers In this article, we are going to learn how we can multiply two numbers using JavaScript. Multiplying the two numbers in JavaScript involves performing arithmetic addition on numeric values, resulting in their sum. JavaScript supports numeric data types. In JavaScript, both floating-point and integer 4 min read JavaScript Program to Find Missing Number in Matrix You are presented with a two-dimensional matrix, matrix[][]where each row contains an ordered sequence of positive integers. The task is to find the missing number in the matrix. NOTE: Each row is a sequence of unique positive integers, arranged in ascending order. Approach 1: Using Summation of Fir 4 min read Rotate Image by 90 Degree using JavaScript In this article, we are going to learn how we can rotate images by 90 degrees clockwise using JavaScript. In simple words, we are given a matrix, and we have to rotate it by 90 degrees clockwise. To understand the question clearly, below is the image illustration explaining how the matrix looks when 3 min read JavaScript Program to check if matrix is lower triangular Given a square matrix and the task is to check the matrix is in lower triangular form or not. A square matrix is called lower triangular if all the entries above the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 0, 0, 0}, {1, 4, 0, 0}, {4, 6, 2, 0}, {0, 4, 7, 6}};Output : Matrix is in l 2 min read JavaScript Program to Find the Normal and Trace of a Matrix JavaScript provides us with ways to find a square matrix's normal and trace. In linear algebra, the total number of entries on the principal diagonal is indicated by the trace, while the normal represents the size of the matrix overall. It is critical to understand these concepts in a variety of com 2 min read JavaScript Program for Subtraction of Matrices Subtraction of two matrices is a basic mathematical operation that is used to find the difference between the two matrices. In this article, we will see how we can perform the subtraction of input matrices using JavaScript. Example: Table of ContentUsing Loop in JavaScriptUsing the map method in Jav 5 min read Addition of Two Fractions using JavaScript Fraction numbers in math are representations of parts of a whole, consisting of a numerator (the part being considered) and a denominator (the total number of equal parts). Add two fractions a/b and c/d and print the answer in simplest form. Examples: Input: 1/2 + 3/2Output: 2/1Input: 1/3 + 3/9Outpu 3 min read JavaScript Program to Find the Rank of a Matrix Given a matrix of size M x N, your task is to find the rank of a matrix using JavaScript.Example:Input: mat[][] = [[10, 20, 10], [20, 40, 20], [30, 50, 0]]Output: Rank is 2Explanation: Ist and 2nd rows are linearly dependent. But Ist and 3rd or 2nd and 3rd are independent. Input: mat[][] = [[10, 20, 5 min read JavaScript Program to Find the Determinant of a Matrix The determinant is a term that is used to represent the matrices in terms of a real number. It can be defined only for the matrices that have the same number of rows and columns. Determinants can be used to find out the inverse of a matrix. There are several ways to find the determinant of the matri 3 min read Like