Matrix Chain Multiplication
Using Dynamic Programming
Name Anum Rehman
Subject Advance algorithm
What is the Matrix Chain Multiplication Problem?
● Problem:
Given a sequence of matrices, determine the most efficient way to multiply
them.
● Goal:
Minimize the total number of scalar multiplications.
● Important:
Matrix multiplication is associative but not commutative
→ (A×B)×C ≠ A×(B×C) in terms of cost
Problem Input Format
You are given n matrices: A , A , ..., A
₁ ₂ ₙ
Each matrix A has dimensions:
ᵢ p[i-1] × p[i]
Your job is to parenthesize the product to minimize
cost
Example:
Matrix dimensions: [10, 20, 30, 40]
Represents matrices:
● A = 10×20
₁
● A = 20×30
₂
● A = 30×40
₃
Brute Force Approach
● Try all possible ways of placing parentheses
● Number of ways grows exponentially (~Catalan
number)
● Inefficient for n > 10
Why Use Dynamic Programming?
● Overlapping subproblems: same sub-chain multiplications computed multiple
times
● Optimal substructure: optimal solution contains optimal solutions to
subproblems
● DP saves previously computed costs in a table
DP Solution - Key Idea
● Let dp[i][j] = minimum number of scalar multiplications needed
to compute the product of matrices A to A
ᵢ ⱼ
● Try all possible k to split between i and j:
dp[i][j] = min(dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j])
Example
Input: p = [10, 20, 30, 40] → Matrices: A1(10×20), A2(20×30), A3(30×40)
We fill dp[1][3] by trying:
● Split at k=1:
Cost = dp[1][1] + dp[2][3] + (10×20×40)
● Split at k=2:
Cost = dp[1][2] + dp[3][3] + (10×30×40)
● Take the minimum of the two

Matrix Chain Advance algorithm in Artificial Intelligence

  • 1.
    Matrix Chain Multiplication UsingDynamic Programming Name Anum Rehman Subject Advance algorithm
  • 2.
    What is theMatrix Chain Multiplication Problem? ● Problem: Given a sequence of matrices, determine the most efficient way to multiply them. ● Goal: Minimize the total number of scalar multiplications. ● Important: Matrix multiplication is associative but not commutative → (A×B)×C ≠ A×(B×C) in terms of cost
  • 3.
    Problem Input Format Youare given n matrices: A , A , ..., A ₁ ₂ ₙ Each matrix A has dimensions: ᵢ p[i-1] × p[i] Your job is to parenthesize the product to minimize cost
  • 4.
    Example: Matrix dimensions: [10,20, 30, 40] Represents matrices: ● A = 10×20 ₁ ● A = 20×30 ₂ ● A = 30×40 ₃
  • 5.
    Brute Force Approach ●Try all possible ways of placing parentheses ● Number of ways grows exponentially (~Catalan number) ● Inefficient for n > 10
  • 6.
    Why Use DynamicProgramming? ● Overlapping subproblems: same sub-chain multiplications computed multiple times ● Optimal substructure: optimal solution contains optimal solutions to subproblems ● DP saves previously computed costs in a table
  • 7.
    DP Solution -Key Idea ● Let dp[i][j] = minimum number of scalar multiplications needed to compute the product of matrices A to A ᵢ ⱼ ● Try all possible k to split between i and j: dp[i][j] = min(dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j])
  • 8.
    Example Input: p =[10, 20, 30, 40] → Matrices: A1(10×20), A2(20×30), A3(30×40) We fill dp[1][3] by trying: ● Split at k=1: Cost = dp[1][1] + dp[2][3] + (10×20×40) ● Split at k=2: Cost = dp[1][2] + dp[3][3] + (10×30×40) ● Take the minimum of the two