
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Matrix Chain Multiplication (A O(N^3) Solution) in C++
If a chain of matrices is given, we have to find minimum number of correct sequence of matrices to multiply.
We know that the matrix multiplication is associative, so four matrices ABCD, we can multiply A(BCD), (AB)(CD), (ABC)D, A(BC)D, in these sequences. Like these sequences, our task is to find which ordering is efficient to multiply.
In the given input there is an array say arr, which contains arr[] = {1, 2, 3, 4}. It means the matrices are of the order (1 x 2), (2 x 3), (3 x 4).
Input − The orders of the input matrices. {1, 2, 3, 4}. It means the matrices are
{(1 x 2), (2 x 3), (3 x 4)}.
Output − Minimum number of operations need multiply these three matrices. Here the result is 18.
Algorithm
matOrder(array, n) Input: List of matrices, the number of matrices in the list. Output: Minimum number of matrix multiplication. Begin define table minMul of size n x n, initially fill with all 0s for length := 2 to n, do for i:=1 to n-length, do j := i + length – 1 minMul[i, j] := ∞ for k := i to j-1, do q := minMul[i, k] + minMul[k+1, j] + array[i-1]*array[k]*array[j] if q < minMul[i, j], then minMul[i, j] := q done done done return minMul[1, n-1] End
Example
#include<iostream> using namespace std; int matOrder(int array[], int n){ int minMul[n][n]; //holds the number of scalar multiplication needed for (int i=1; i<n; i++) minMul[i][i] = 0; //for multiplication with 1 matrix, cost is 0 for (int length=2; length<n; length++){ //find the chain length starting from 2 for (int i=1; i<n-length+1; i++){ int j = i+length-1; minMul[i][j] = INT_MAX; //set to infinity for (int k=i; k<=j-1; k++){ //store cost per multiplications int q = minMul[i][k] + minMul[k+1][j] + array[i-1]*array[k]*array[j]; if (q < minMul[i][j]) minMul[i][j] = q; } } } return minMul[1][n-1]; } int main(){ int arr[] = {1, 2, 3, 4}; int size = 4; cout << "Minimum number of matrix multiplications: "<<matOrder(arr, size); }
Output
Minimum number of matrix multiplications: 18
- Related Articles
- Matrix Chain Multiplication
- C Program for Matrix Chain Multiplication
- Matrix multiplication algorithm
- Sparse Matrix Multiplication in C++
- Find Scalar Multiplication of a Matrix in Java?
- Algorithm for matrix multiplication in JavaScript
- Find Cube Pairs - (A n^(2/3) Solution) in C++
- Print n x n spiral matrix using O(1) extra space in C Program.
- Matrix Multiplication and Normalization in C program
- C++ Program to Perform Matrix Multiplication
- Python program multiplication of two matrix.
- Matrix Vector multiplication with Einstein summation convention in Python
- Addition multiplication ladder in an array in JavaScript\n
- Matrix creation of n*n in Python
- Find the value of $2^o+3^o+4^o$.

Advertisements