Matrix Chain Multiplication

May 30, 2024

Matrix Chain Multiplication Lecture Notes

Introduction

  • Topic: Matrix Chain Multiplication
  • Purpose: To clarify missing details from a previous video on the topic
  • Contents:
    • What is matrix multiplication?
    • What is matrix chain multiplication?
    • Applying dynamic programming to solve the problem
    • Solving example problems

Matrix Multiplication

  • Condition for Multiplication: Number of columns of the first matrix must equal the number of rows of the second matrix
  • Dimension of Resultant Matrix: Number of rows of the first matrix by number of columns of the second matrix
  • Multiplications Required: Calculated by dimensions (e.g., for 2x3 and 3x2 matrices, 2 * 3 * 2 = 12 multiplications)
  • **Key Points: **
    • Multiplication condition: Columns of first matrix = Rows of second matrix
    • Resultant dimension is (rows of first matrix) x (columns of second matrix)
    • Calculate multiplications by multiplying dimensions

Matrix Chain Multiplication

  • Goal: To find the most efficient way to multiply a chain of matrices
  • Example: Multiplying matrices A1, A2, and A3 in different orders gives different number of multiplications
    • Method 1: (A1 * A2) * A3
    • Method 2: A1 * (A2 * A3)
    • Associative property holds, but efforts (number of multiplications) vary
  • Dynamic Programming Approach: Find minimal multiplication cost by considering multiple ways to parenthesize multiplication
  • Dynamic Programming Formula:
    • C(i, j) = min {C(i, k) + C(k+1, j) + d(i-1) * d(k) * d(j)} for i <= k < j
  • Algorithm Steps:
    • Calculate cost for smaller values first (i.e., smaller subproblems)
    • Use a tabulated approach to store and reuse computed values

Step-by-Step Example

  1. Set Dimensions: Define dimensions for matrices involved in the problem
  2. Cost Calculation:
  • Start with smallest subproblems (individual matrices, cost = 0)
  • Move to subproblems with difference of 1, 2, etc.
  1. Fill and Use Tables:
  • Cost table to store minimum multiplication costs
  • K table to store points of optimal matrix splits for minimum cost
  1. Formula Application:
  • For each C(i, j), consider all possible k values
  • Choose k yielding the minimum cost
  1. Track Parentheses: Use K table to reconstruct optimal parenthesis structure

Example Problem Solved

  • Dimensions: e.g., 3x2, 2x4, 4x2, 2x5
  • Stepwise Calculation:
    • Difference 0, cost 0 for each individual matrix
    • Difference 1, difference 2, and so on...
    • Final result derived from optimal subproblem solutions

Complexity Analysis

  • Table Cells to Fill: N * (N-1) / 2 for N matrices
  • Time per Cell: Each cell potentially requires evaluating N splits
  • Overall Complexity: O(N^3)

Conclusion

  • Dynamic Programming: Efficient approach to solving matrix chain multiplication problem
  • Optimal Order: Derived using cost and K tables
  • Multiplication Cost: Minimized using formulated approach