
- 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
C++ Program to Perform Optimal Paranthesization Using Dynamic Programming
This is a C++ program to perform Optimal Paranthesization using Dynamic Programming.
Algorithm
Begin Take the length n and dimension of matrix as input. MatrixChain() to find out minimum multiplications: Arguments: a[i][j]=Minimum number of scalar multiplications needed to compute the matrix A[i]A[i+1]...A[j] = A[i..j] where dimension of A[i] is p[i-1] x p[i]. a[i][j] means cost is zero when multiplying one matrix. L is chain length. m = cost / scalar multiplications. Body of the function: for i = 1 to n-1 Initialize a[i][i] = 0 for L = 2 to n-1 for i = 1 to n - L + 1 j = i + L - 1 a[i][j] = INT_MAX for k = i to j - 1 m = a[i][k] + a[k + 1][j] + p[i - 1] * p[k] * p[j]; if (m < a[i][j]) a[i][j] = m b[i][j] = k return a[1][n - 1] End
Example
#include<limits.h> #include<iostream> using namespace std; int MatrixChain(int p[], int n) { int a[n][n]; int b[n][n]; int i, j, k, L, m; for (i = 1; i < n; i++) a[i][i] = 0; for (L = 2; L < n; L++) { for (i = 1; i <= n - L + 1; i++) { j = i + L - 1; a[i][j] = INT_MAX; for (k = i; k <= j - 1; k++) { m = a[i][k] + a[k + 1][j] + p[i - 1] * p[k] * p[j]; if (m < a[i][j]) { a[i][j] = m; b[i][j] = k; } } } } return a[1][n - 1]; } int main() { cout << "Enter the length:"; int n; cin >> n; int a[n]; cout << "Enter the dimensions: "; for (int v = 0; v < n; ++v) { cin >> a[v]; } cout << "Minimum number of multiplications is: " << MatrixChain(a,n); return 0; }
Output
Enter the length:5 Enter the dimensions: 2 3 7 6 4 Minimum number of multiplications is: 174
- Related Articles
- C++ Program to Solve Knapsack Problem Using Dynamic Programming
- C++ Program to Find Fibonacci Numbers using Dynamic Programming
- C++ Program to Find Factorial of a Number using Dynamic Programming
- How to Perform Numpy Broadcasting using Python using dynamic arrays?
- Introduction to Dynamic Programming
- Bitmasking and Dynamic Programming in C++
- Python Program to Find Longest Common Substring using Dynamic Programming with Bottom-Up Approach
- Sum over Subsets - Dynamic Programming in C++
- Dynamic Programming in JavaScript
- C# program to perform Quick sort using Recursion
- C++ Program to Perform Sorting Using B-Tree
- C++ Program for Optimal Page Replacement Algorithm
- Dynamic programming to check dynamic behavior of an array in JavaScript
- Maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming in C++ program
- C++ Program to Perform Addition Operation Using Bitwise Operators

Advertisements