Mid From PDF Coding C# Coding Interview

Matrix Chain Multiplication?

int MatrixChainOrder(int[] dims)
{
int n = dims.Length - 1;
int[,] dp = new int[n, n];
for (int l = 2; l <= n; l++)
{

Follow on:

for (int i = 0; i < n - l + 1; i++)
{
int j = i + l - 1;
dp[i, j] = int.MaxValue;
for (int k = i; k < j; k++)
{
int cost = dp[i, k] + dp[k + 1, j] + dims[i] *

dims[k + 1] * dims[j + 1];

dp[i, j] = Math.Min(dp[i, j], cost);
}
}
}
return dp[0, n - 1];
}

Explanation:

DP calculates minimal cost to multiply chain of matrices by trying all partitions.

More from C# Programming Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details