CE100 Algorithms and Programming II

CE100 Algorithms and Programming II

Week-5 (Dynamic Programming)

Spring Semester, 2021-2022

Download DOC-PDF, DOC-DOCX, SLIDE, PPTX

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Quicksort Sort

Outline

  • Convex Hull (Divide & Conquer)

  • Dynamic Programming

    • Introduction

    • Divide-and-Conquer (DAC) vs Dynamic Programming (DP)

RTEU CE100 Week-5
CE100 Algorithms and Programming II
  • Fibonacci Numbers

    • Recursive Solution

    • Bottom-Up Solution

  • Optimization Problems

  • Development of a DP Algorithms

RTEU CE100 Week-5
CE100 Algorithms and Programming II
  • Matrix-Chain Multiplication

    • Matrix Multiplication and Row Columns Definitions

    • Cost of Multiplication Operations (pxqxr)

    • Counting the Number of Parenthesizations

RTEU CE100 Week-5
CE100 Algorithms and Programming II
  • The Structure of Optimal Parenthesization

    • Characterize the structure of an optimal solution

    • A Recursive Solution

      • Direct Recursion Inefficiency.
    • Computing the optimal Cost of Matrix-Chain Multiplication

    • Bottom-up Computation

RTEU CE100 Week-5
CE100 Algorithms and Programming II
  • Algorithm for Computing the Optimal Costs

    • MATRIX-CHAIN-ORDER
  • Construction and Optimal Solution

    • MATRIX-CHAIN-MULTIPLY
  • Summary

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Dynamic Programming - Introduction

  • An algorithm design paradigm like divide-and-conquer
  • Programming: A tabular method (not writing computer code)
    • Older sense of planning or scheduling, typically by filling in a table
  • Divide-and-Conquer (DAC): subproblems are independent
  • Dynamic Programming (DP): subproblems are not independent
  • Overlapping subproblems: subproblems share sub-subproblems
    • In solving problems with overlapping subproblems
      • A DAC algorithm does redundant work
        • Repeatedly solves common subproblems
      • A DP algorithm solves each problem just once
        • Saves its result in a table
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Problem 1: Fibonacci Numbers Recursive Solution

  • Reminder:

F(0)=0 and F(1)=1F(n)=F(n1)+F(n2)REC-FIBO(n){if n<2return nelsereturn REC-FIBO(n1)+REC-FIBO(n2) }\begin{align*} & F(0)=0 \text{ and } F(1)=1 \\ & F(n)=F(n-1)+F(n-2) \\[10 pt] &\text{REC-FIBO}(n) \{ \\ & \quad \text{if} \ n < 2 \\ & \qquad \text{return} \ n \\ & \quad \text{else} \\ & \qquad \text{return} \ \text{REC-FIBO}(n-1) + \text{REC-FIBO}(n-2) \ \} \end{align*}

  • Overlapping subproblems in different recursive calls. Repeated work!
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Problem 1: Fibonacci Numbers Recursive Solution

  • Recurrence:
    • exponential runtime

T(n)=T(n1)+T(n2)+1 T(n) = T(n-1) + T(n-2) + 1

  • Recursive algorithm inefficient because it recomputes the same F(i)F(i) repeatedly in different branches of the recursion tree.
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Problem 1: Fibonacci Numbers Bottom-up Computation

  • Reminder:

F(0)=0 and F(1)=1F(n)=F(n1)+F(n2)\begin{align*} & F(0)=0 \text{ and } F(1)=1 \\ & F(n)=F(n-1)+F(n-2) \end{align*}

  • Runtime Θ(n)\Theta(n)

ITER-FIBO(n)
  F[0] = 0
  F[1] = 1
  for i = 2 to n do
    F[i] = F[i-1] + F[i-2]
  return F[n]

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Optimization Problems

  • DP typically applied to optimization problems
  • In an optimization problem
    • There are many possible solutions (feasible solutions)
    • Each solution has a value
    • Want to find an optimal solution to the problem
      • A solution with the optimal value (min or max value)
    • Wrong to say the optimal solution to the problem
      • There may be several solutions with the same optimal value
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Development of a DP Algorithm

Step-1. Characterize the structure of an optimal solution
Step-2. Recursively define the value of an optimal solution
Step-3. Compute the value of an optimal solution in a bottom-up fashion
Step-4. Construct an optimal solution from the information computed in Step 3

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Problem 2: Matric Chain Multiplication

  • Input: a sequence (chain) A1,A2,,An\langle A_1,A_2, \dots , A_n\rangle of nn matrices
  • Aim: compute the product A1A2AnA_1 \cdot A_2 \cdot \dots A_n
  • A product of matrices is fully parenthesized if
    • It is either a single matrix
    • Or, the product of two fully parenthesized matrix products surrounded by a pair of parentheses.
      (Ai(Ai+1Ai+2Aj))\bigg(A_i(A_{i+1}A_{i+2} \dots A_j) \bigg)
      ((AiAi+1Ai+2Aj1)Aj)\bigg((A_iA_{i+1}A_{i+2} \dots A_{j-1})A_j \bigg)
      ((AiAi+1Ai+2Ak)(Ak+1Ak+2Aj))\bigg((A_iA_{i+1}A_{i+2} \dots A_k)(A_{k+1}A_{k+2} \dots A_j)\bigg) for ik<ji \leq k < j
  • All parenthesizations yield the same product; matrix product is associative
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Matrix-chain Multiplication: An Example Parenthesization

  • Input: A1,A2,A3,A4\langle A_1,A_2,A_3,A_4\rangle (55 distinct ways of full parenthesization)

(A1(A2(A3A4)))(A1((A2A3)A4))((A1A2)(A3A4))((A1(A2A3)A4))(((A1A2)A3)A4)\begin{align*} & \bigg(A_1\Big(A_2(A_3A_4)\Big)\bigg) \\ & \bigg(A_1\Big((A_2A_3)A_4\Big)\bigg) \\ & \bigg((A_1A_2)(A_3A_4)\bigg) \\ & \bigg(\Big(A_1(A_2A_3)A_4\Big)\bigg) \\ & \bigg(\Big((A_1A_2)A_3\Big)A_4\bigg) \end{align*}

  • The way we parenthesize a chain of matrices can have a dramatic effect on the cost of computing the product
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Matrix-chain Multiplication: Reminder


MATRIX-MULTIPLY(A, B)
  if cols[A]!=rows[B] then 
    error(“incompatible dimensions”)
  for i=1 to rows[A] do
    for j=1 to cols[B] do 
      C[i,j]=0
      for k=1 to cols[A] do 
        C[i,j]=C[i,j]+A[i,k]·B[k,j]
  return C 

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Matrix Chain Multiplication: Example

  • A1:10x100A1:10\text{x}100, A2:100x5A2:100\text{x}5, A3:5x50A3:5\text{x}50

    • Which paranthesization is better? (A1A2)A3(A1A2)A3 or A1(A2A3)A1(A2A3)?
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Matrix Chain Multiplication: Example

  • A1:10×100A1:10 \times 100, A2:100×5A2:100 \times 5, A3:5×50A3:5 \times 50

    • Which paranthesization is better? (A1A2)A3(A1A2)A3 or A1(A2A3)A1(A2A3)?
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Matrix Chain Multiplication: Example

  • A1:10×100A1:10 \times 100, A2:100×5A2:100 \times 5, A3:5×50A3:5 \times 50
    • Which paranthesization is better? (A1A2)A3(A1A2)A3 or A1(A2A3)A1(A2A3)?

In summary:

  • (A1A2)A3(A1A2)A3 = #\# of multiply-add ops: 75007500
  • A1(A2A3)A1(A2A3) = #\# of multiple-add ops: 7500075000

First parenthesization yields 10x faster computation

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Matrix-chain Multiplication Problem

  • Input: A chain A1,A2,,An\langle A_1,A_2, \dots ,A_n\rangle of nn matrices,

    • where AiA_i is a pi1×pip_{i-1} \times p_i matrix
  • Objective: Fully parenthesize the product

    • A1A2AnA_1 \cdot A_2 \dots A_n
      • such that the number of scalar mult-adds is minimized.
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Counting the Number of Parenthesizations

  • Brute force approach: exhaustively check all parenthesizations
  • P(n)P(n): #\# of parenthesizations of a sequence of n matrices
  • We can split sequence between kthk^{th} and (k+1)st(k+1)^{st} matrices for any k=1,2,,n1k=1, 2, \dots , n-1 , then parenthesize the two resulting sequences independently, i.e.,

(A1A2A3Ak)(breakpointAk+1Ak+2An)(A_1 A_2 A_3 \dots A_k \overbrace{)(}^{break-point}A_{k+1} A_{k+2} \dots A_n)

  • We obtain the recurrence

P(1)=1 and P(n)=k=1n1P(k)P(nk)P(1)=1 \text{ and } P(n)=\sum \limits_{k=1}^{n-1}P(k)P(n-k)

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Number of Parenthesizations:

  • P(1)=1P(1)=1 and P(n)=k=1n1P(k)P(nk)P(n)=\sum \limits_{k=1}^{n-1}P(k)P(n-k)

  • The recurrence generates the sequence of Catalan Numbers Solution is P(n)=C(n1)P(n)=C(n-1) where

C(n)=1n+1(2nn)=Ω(4n/n3/2)C(n)=\frac{1}{n+1} {2n \choose n} = \Omega(4^n / n^{3/2})

  • The number of solutions is exponential in nn
  • Therefore, brute force approach is a poor strategy
RTEU CE100 Week-5
CE100 Algorithms and Programming II

The Structure of Optimal Parenthesization

  • Notation: Ai..jA_{i..j}: The matrix that results from evaluation of the product: AiAi+1Ai+2AjA_i A_{i+1} A_{i+2} \dots A_j

  • Observation: Consider the last multiplication operation in any parenthesization: (A1A2Ak)(Ak+1Ak+2An)(A_1 A_2 \dots A_k) \cdot (A_{k+1} A_{k+2} \dots A_n)

    • There is a kk value (1k<n)(1 \leq k < n) such that:
      • First, the product A1kA_1 \dots k is computed
      • Then, the product Ak+1nA_{k+1 \dots n} is computed
      • Finally, the matrices A1kA_{1 \dots k} and Ak+1nA_{k+1 \dots n} are multiplied
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Step 1: Characterize the Structure of an Optimal Solution

  • An optimal parenthesization of product A1A2AnA_1 A_2 \dots A_n will be:
    (A1A2Ak)(Ak+1Ak+2An)(A_1 A_2 \dots A_k) \cdot (A_{k+1} A_{k+2} \dots A_n) for some kk value
  • The cost of this optimal parenthesization will be:
    == Cost of computing A1kA_{1 \dots k}
    ++ Cost of computing Ak+1nA_{k+1 \dots n}
    ++ Cost of multiplying A1kAk+1nA_{1 \dots k} \cdot A_{k+1 \dots n}
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Step 1: Characterize the Structure of an Optimal Solution

  • Key observation: Given optimal parenthesization
    • (A1A2A3Ak)(Ak+1Ak+2An)(A_1 A_2 A_3 \dots A_k) \cdot (A_{k+1} A_{k+2} \dots A_n)
  • Parenthesization of the subchain A1A2A3AkA_1 A_2 A_3 \dots A_k
  • Parenthesization of the subchain Ak+1Ak+2AnA_{k+1} A_{k+2} \dots A_n

should both be optimal

  • Thus, optimal solution to an instance of the problem contains optimal solutions to subproblem instances
    • i.e., optimal substructure within an optimal solution exists.
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Step 2: A Recursive Solution

  • Step 2: Define the value of an optimal solution recursively in terms of optimal solutions to the subproblems

  • Assume we are trying to determine the min cost of computing AijA_{i \dots j}

  • mi,jm_{i,j}: min #\# of scalar multiply-add opns needed to compute AijA_{i \dots j}

    • Note: The optimal cost of the original problem: m1,nm_{1,n}
  • How to compute mi,jm_{i,j} recursively?

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Step 2: A Recursive Solution

  • Base case: mi,i=0m_{i,i}=0 (single matrix, no multiplication)

  • Let the size of matrix AiA_i be (pi1×pi)(p_{i-1} \times p_i)

  • Consider an optimal parenthesization of chain

    • AiAj:(AiAk)(Ak+1Aj)A_i \dots A_j : (A_i \dots A_k) \cdot (A_{k+1} \dots A_j)
  • The optimal cost: mi,j=mi,k+mk+1,j+pi1×pk×pjm_{i,j} = m_{i,k} + m_{k+1,j} + p_{i-1} \times p_k \times p_j

  • where:

    • mi,km_{i,k}: Optimal cost of computing AikA_{i \dots k}
    • mk+1,jm_{k+1,j}: Optimal cost of computing Ak+1jA_{k+1 \dots j}
    • pi1×pk×pjp_{i-1} \times p_k \times p_j : Cost of multiplying AikA_{i \dots k} and Ak+1jA_{k+1 \dots j}
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Step 2: A Recursive Solution

  • In an optimal parenthesization: kk must be chosen to minimize mijm_{ij}
  • The recursive formulation for mijm_{ij}:

mij={0ifi=jMINik<j{mik+mk+1,j+pi1pkpj}ifi<j\begin{align*} m_{ij} = \begin{cases} 0 & if & i=j \\ \underset{i \leq k < j}{MIN} \{ m_{ik} + m_{k+1,j} + p_{i-1} p_k p_j \} & if & i<j \end{cases} \end{align*}

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Step 2: A Recursive Solution

  • The mijm_{ij} values give the costs of optimal solutions to subproblems
  • In order to keep track of how to construct an optimal solution
    • Define sijs_{ij} to be the value of kk which yields the optimal split of the subchain AijA_{i \dots j}
      • That is, sij=ks_{ij}=k such that
        • mij=mik+mk+1,j+pi1pkpjm_{ij} = m_{ik} + m_{k+1,j} +p_{i-1} p_k p_j holds
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Direct Recursion: Inefficient!

  • Recursive Matrix-Chain (RMC) Order
RMC(p,i,j)
	
  if (i == j) then 
    return 0
	
  m[i, j] = INF	
	
  for k=i to j-1 do
		
    q = RMC(p, i, k) + RMC(p, k+1, j) + p_{i-1} p_k p_j
		
    if q < m[i, j] then
      m[i, j] = q
  
  endfor

	return m[i, j] 
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Direct Recursion: Inefficient!

  • Recursion tree for RMC(p,1,4)RMC(p,1,4)

  • Nodes are labeled with ii and jj values

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Computing the Optimal Cost (Matrix-Chain Multiplication)

An important observation:

  • We have relatively few subproblems
    • one problem for each choice of ii and jj satisfying 1ijn1 \leq i \leq j \leq n
    • total n+(n1)++2+1=12n(n+1)=Θ(n2)n + (n-1) + \dots + 2 + 1 = \frac{1}{2}n(n+1) = \Theta(n2) subproblems
  • We can write a recursive algorithm based on recurrence.
  • However, a recursive algorithm may encounter each subproblem many times in different branches of the recursion tree
  • This property, overlapping subproblems, is the second important feature for applicability of dynamic programming
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Computing the Optimal Cost (Matrix-Chain Multiplication)

  • Compute the value of an optimal solution in a bottom-up fashion
    • matrix AiA_i has dimensions pi1×pip_{i-1} \times p_i for i=1,2,,ni = 1, 2, \dots , n
    • the input is a sequence p0,p1,,pn\langle p_0, p_1, \dots, p_n \rangle where length[p]=n+1length[p] = n + 1
  • Procedure uses the following auxiliary tables:
    • m[1n,1n]m[1 \dots n, 1 \dots n]: for storing the m[i,j]m[i,j] costs
    • s[1n,1n]s[1 \dots n, 1 \dots n]: records which index of kk achieved the optimal cost in computing m[i,j]m[i,j]
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Bottom-Up Computation

  • How to choose the order in which we process mijm_{ij} values?
  • Before computing mijm_{ij}, we have to make sure that the values for mikm_{ik} and mk+1,jm_{k+1,j} have been computed for all kk.

mij=MINik<j{mik+mk+1,j+pi1pkpj}m_{ij}=\underset{i \leq k < j}{MIN} \{ m_{ik} + m_{k+1,j} + p_{i-1} p_k p_j \}

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Bottom-Up Computation

mij=MINik<j{mik+mk+1,j+pi1pkpj}m_{ij}=\underset{i \leq k < j}{MIN} \{ m_{ik} + m_{k+1,j} + p_{i-1} p_k p_j \}

  • mijm_{ij} must be processed after mikm_{ik} and mj,k+1m_{j,k+1}
  • Reminder: mijm_{ij} computed only for j>ij > i
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Bottom-Up Computation

mij=MINik<j{mik+mk+1,j+pi1pkpj}m_{ij}=\underset{i \leq k < j}{MIN} \{ m_{ik} + m_{k+1,j} + p_{i-1} p_k p_j \}

  • mijm_{ij} must be processed after mikm_{ik} and mj,k+1m_{j,k+1}
  • How to set up the iterations over ii and jj to compute mijm_{ij}?
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Bottom-Up Computation

mij=MINik<j{mik+mk+1,j+pi1pkpj}m_{ij}=\underset{i \leq k < j}{MIN} \{ m_{ik} + m_{k+1,j} + p_{i-1} p_k p_j \}

  • If the entries mijm_{ij} are computed in the shown order, then mikm_{ik} and mk+1,jm_{k+1,j} values are guaranteed to be computed before mijm_{ij}.
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Bottom-Up Computation

mij=MINik<j{mik+mk+1,j+pi1pkpj}m_{ij}=\underset{i \leq k < j}{MIN} \{ m_{ik} + m_{k+1,j} + p_{i-1} p_k p_j \}

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Bottom-Up Computation

mij=MINik<j{mik+mk+1,j+pi1pkpj}m_{ij}=\underset{i \leq k < j}{MIN} \{ m_{ik} + m_{k+1,j} + p_{i-1} p_k p_j \}

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Algorithm for Computing the Optimal Costs

  • Note: l ==\ell and p_{i-1} p_k p_j =pi1pkpj=p_{i-1} p_k p_j
MATRIX-CHAIN-ORDER(p)
  n = length[p]-1
  for i=1 to n do
    m[i, i]=0
  endfor
  for l=2 to n do
    for i=1 to n n-l+1 do
      j=i+l-1
      m[i, j]=INF
      for k=i to j-1 do
        q=m[i,k]+m[k+1, j]+p_{i-1} p_k p_j
        if q < m[i,j] then
          m[i,j]=q
          s[i,j]=k
      endfor
    endfor
  endfor
  return m and s
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Algorithm for Computing the Optimal Costs

  • The algorithm first computes
    • m[i,i]0m[i, i] \leftarrow 0 for i=1,2,,ni=1,2, \dots ,n min costs for all chains of length 1
  • Then, for =2,3,,n\ell = 2,3, \dots,n computes
    • m[i,i+1]m[i, i+\ell-1] for i=1,,n+1i=1,\dots,n-\ell+1 min costs for all chains of length \ell
  • For each value of =2,3,,n\ell = 2, 3, \dots ,n,
    • m[i,i+1]m[i, i+\ell-1] depends only on table entries m[i,k]&m[k+1,i+1]m[i,k] \& m[k+1, i+\ell-1] for ik<i+1i\leq k < i+\ell-1, which are already computed
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Algorithm for Computing the Optimal Costs

compute m[i,i+1]{m[1,2],m[2,3],,m[n1,n]}(n1) values{=2for i=1 to n1 do m[i,i+1]=for k=i to i do compute m[i,i+2]{m[1,3],m[2,4],,m[n2,n]}(n2) values{=3for i=1 to n2 do m[i,i+2]=for k=i to i+1 do compute m[i,i+3]{m[1,4],m[2,5],,m[n3,n]}(n3) values{=4for i=1 to n3 do m[i,i+3]=for k=i to i+2 do \begin{align} \begin{aligned} \text{compute } m[i,i+1] \\ \underbrace{ \{ m[1,2],m[2,3], \dots ,m[n-1,n]\} }_{(n-1) \text{ values}} \end{aligned} & \begin{cases} & \ell=2 \\ & \text{for } i=1 \text{ to } n-1 \text{ do } \\ & \quad m[i,i+1]=\infty \\ & \quad \quad \text{for } k=i \text{ to } i \text{ do } \\ & \quad \quad \quad \vdots \end{cases} \\ \begin{aligned} \text{compute } m[i,i+2] \\ \underbrace{ \{ m[1,3],m[2,4], \dots ,m[n-2,n]\} }_{(n-2) \text{ values}} \end{aligned} & \begin{cases} & \ell=3 \\ & \text{for } i=1 \text{ to } n-2 \text{ do } \\ & \quad m[i,i+2]=\infty \\ & \quad \quad \text{for } k=i \text{ to } i+1 \text{ do } \\ & \quad \quad \quad \vdots \end{cases} \\ \begin{aligned} \text{compute } m[i,i+3] \\ \underbrace{ \{ m[1,4],m[2,5], \dots ,m[n-3,n]\} }_{(n-3) \text{ values}} \end{aligned} & \begin{cases} & \ell=4 \\ & \text{for } i=1 \text{ to } n-3 \text{ do } \\ & \quad m[i,i+3]=\infty \\ & \quad \quad \text{for } k=i \text{ to } i+2 \text{ do } \\ & \quad \quad \quad \vdots \end{cases} \end{align}

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Table access pattern in computing m[i,j]m[i, j]s for =ji+1\ell=j-i+1

for ki to j1 doqm[i,k]+m[k+1,j]+pi1pkpj\begin{align*} & \text{for} \ k \leftarrow i \ \text{to} \ j-1 \ \text{do} \\ & \quad q \leftarrow m[i,k]+m[k+1,j]+p_{i-1}p_kp_j \end{align*}

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Table access pattern in computing m[i,j]m[i, j]s for =ji+1\ell=j-i+1

((Ai)mult.(Ai+1Ai+2Aj))\begin{align*} & \bigg( (A_i) \overset{mult.}{ \vdots } (A_{i+1}A_{i+2} \dots A_j) \bigg) \end{align*}

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Table access pattern in computing m[i,j]m[i, j]s for =ji+1\ell=j-i+1

((AiAi+1)mult.(Ai+2Aj))\begin{align*} \bigg( (A_iA_{i+1}) \overset{mult.}{ \vdots } (A_{i+2} \dots A_j) \bigg) \end{align*}

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Table access pattern in computing m[i,j]m[i, j]s for =ji+1\ell=j-i+1

((AiAi+1Ai+2)mult.(Ai+3Aj))\begin{align*} \bigg( (A_iA_{i+1}A_{i+2}) \overset{mult.}{ \vdots } (A_{i+3} \dots A_j) \bigg) \end{align*}

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Table access pattern in computing m[i,j]m[i, j]s for =ji+1\ell=j-i+1

((AiAi+1Aj1)mult.(Aj))\begin{align*} \bigg( (A_iA_{i+1} \dots A_{j-1}) \overset{mult.}{ \vdots } (A_j) \bigg) \end{align*}

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Table access pattern Example

  • Compute m25m_{25}
  • Choose the kk value that leads to min cost

mij=MINik<j{mik+mk+1,j+pi1pkpj}A1:(30×35)A2:(35×15)A3:(15×5)A4:(5×10)A5:(10×20)A6:(20×25)((A2)(k=2)(A3A4A5))cost=m22+m35+p1p2p5=0+2500+35×15×20=13000m_{ij}=\underset{i \leq k < j}{MIN} \{ m_{ik} + m_{k+1,j} + p_{i-1} p_k p_j \} \\[10pt] \begin{align*} \begin{aligned} A_1 &: (30 \times 35) \\ A_2 &: (35 \times 15) \\ A_3 &: (15 \times 5) \\ A_4 &: (5 \times 10) \\ A_5 &: (10 \times 20) \\ A_6 &: (20 \times 25) \end{aligned} \begin{aligned} & ((A_2) \overbrace{\vdots}^{ (k=2) } (A_3 A_4 A_5)) \\[10 pt] \quad cost &= m_{22} + m_{35} + p_1p_2p_5 \\ &= 0 + 2500 + 35 \times 15 \times 20 \\ &= 13000 \end{aligned} \end{align*}

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Table access pattern Example

  • Compute m25m_{25}
  • Choose the kk value that leads to min cost

mij=MINik<j{mik+mk+1,j+pi1pkpj}A1:(30×35)A2:(35×15)A3:(15×5)A4:(5×10)A5:(10×20)A6:(20×25)((A2A3)(k=3)(A4A5))cost=m23+m45+p1p3p5=2625+1000+35×5×20=7125m_{ij}=\underset{i \leq k < j}{MIN} \{ m_{ik} + m_{k+1,j} + p_{i-1} p_k p_j \} \\[10pt] \begin{align*} \begin{aligned} A_1 &: (30 \times 35) \\ A_2 &: (35 \times 15) \\ A_3 &: (15 \times 5) \\ A_4 &: (5 \times 10) \\ A_5 &: (10 \times 20) \\ A_6 &: (20 \times 25) \end{aligned} \begin{aligned} & ((A_2 A_3) \overbrace{\vdots}^{ (k=3) } (A_4 A_5)) \\[10 pt] \quad cost &= m_{23} + m_{45} + p_1p_3p_5 \\ &= 2625 + 1000 + 35 \times 5 \times 20 \\ &= 7125 \end{aligned} \end{align*}

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Table access pattern Example

  • Compute m25m_{25}
  • Choose the kk value that leads to min cost

mij=MINik<j{mik+mk+1,j+pi1pkpj}A1:(30×35)A2:(35×15)A3:(15×5)A4:(5×10)A5:(10×20)A6:(20×25)((A2A3A4)(k=4)(A5))cost=m24+m55+p1p4p5=4375+0+35×10×20=11375m_{ij}=\underset{i \leq k < j}{MIN} \{ m_{ik} + m_{k+1,j} + p_{i-1} p_k p_j \} \\[10pt] \begin{align*} \begin{aligned} A_1 &: (30 \times 35) \\ A_2 &: (35 \times 15) \\ A_3 &: (15 \times 5) \\ A_4 &: (5 \times 10) \\ A_5 &: (10 \times 20) \\ A_6 &: (20 \times 25) \end{aligned} \begin{aligned} & ((A_2 A_3 A_4)\overbrace{\vdots}^{ (k=4) }(A_5)) \\[10 pt] \quad cost &= m_{24} + m_{55} + p_1p_4p_5 \\ &= 4375 + 0 + 35 \times 10 \times 20 \\ &= 11375 \end{aligned} \end{align*}

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Table access pattern Example

  • Compute m25m_{25}
  • Choose the kk value that leads to min cost

mij=MINik<j{mik+mk+1,j+pi1pkpj}A1:(30×35)A2:(35×15)A3:(15×5)A4:(5×10)A5:(10×20)A6:(20×25)((A2)(k=2)(A3A4A5))m22+m35+p1p2p5=13000((A2A3)(k=3)(A4A5))m23+m45+p1p3p5=7125selectedmin((A2A3A4)(k=4)(A5))m24+m55+p1p4p5=11375m25=7125s25=3m_{ij}=\underset{i \leq k < j}{MIN} \{ m_{ik} + m_{k+1,j} + p_{i-1} p_k p_j \} \\[10pt] \begin{align*} \begin{aligned} A_1 &: (30 \times 35) \\ A_2 &: (35 \times 15) \\ A_3 &: (15 \times 5) \\ A_4 &: (5 \times 10) \\ A_5 &: (10 \times 20) \\ A_6 &: (20 \times 25) \end{aligned} \quad \begin{aligned} & ((A_2)\overbrace{\vdots}^{ (k=2) } (A_3 A_4 A_5)) \rightarrow m_{22} + m_{35} + p_1p_2p_5 = 13000 \\ & ((A_2 A_3) \overbrace{\vdots}^{ (k=3) } (A_4 A_5)) \rightarrow m_{23} + m_{45} + p_1p_3p_5 = \overbrace{ \boldsymbol{7125}}^{selected} \Leftarrow \text{min} \\ & ((A_2 A_3 A_4)\overbrace{\vdots}^{ (k=4) }(A_5)) \rightarrow m_{24} + m_{55} + p_1p_4p_5 = 11375 \\[20 pt] & m_{25} = 7125 \\ & s_{25} = 3 \end{aligned} \end{align*}

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Constructing an Optimal Solution

  • MATRIX-CHAIN-ORDER determines the optimal #\# of scalar mults/adds
    • needed to compute a matrix-chain product
    • it does not directly show how to multiply the matrices
  • That is,
    • it determines the cost of the optimal solution(s)
    • it does not show how to obtain an optimal solution
  • Each entry s[i,j]s[i, j] records the value of kk such that optimal parenthesization of AiAjA_i \dots A_j splits the product between AkA_k & Ak+1A_{k+1}
  • We know that the final matrix multiplication in computing A1nA_{1 \dots n} optimally is A1s[1,n]×As[1,n]+1,nA_{1 \dots s[1,n]} \times A_{s[1,n]+1,n}
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Example: Constructing an Optimal Solution

  • Reminder: sijs_{ij} is the optimal top-level split of AiAjA_i \dots A_j

  • What is the optimal top-level split for:

    • A1A2A3A4A5A6A_1 A_2 A_3 A_4 A_5 A_6
    • s16=3s_{16}=3
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Example: Constructing an Optimal Solution

  • Reminder: sijs_{ij} is the optimal top-level split of AiAjA_i \dots A_j

  • (A1A2A3)(k=4)(A4A5A6)(A_1 A_2 A_3) \overbrace{\vdots}^{ (k=4) } (A_4 A_5 A_6)

    • What is the optimal split for A1A3A_1 \dots A_3 ? ( s13=1s_{13}=1 )
    • What is the optimal split for A4A6A_4 \dots A_6 ? ( s46=5s_{46}=5 )
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Example: Constructing an Optimal Solution

  • Reminder: sijs_{ij} is the optimal top-level split of AiAjA_i \dots A_j

  • ((A1)(k=1)(A2A3))((A4A5)(k=5)(A6))\Big((A_1) \overbrace{\vdots}^{ (k=1) } (A_2 A_3) \Big) \Big( (A_4 A_5) \overbrace{\vdots}^{ (k=5) } (A_6) \Big)

    • What is the optimal split for A1A3A_1 \dots A_3 ? ( s13=1s_{13}=1 )
    • What is the optimal split for A4A6A_4 \dots A_6 ? ( s46=5s_{46}=5 )
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Example: Constructing an Optimal Solution

  • Reminder: sijs_{ij} is the optimal top-level split of AiAjA_i \dots A_j

  • ((A1)(A2A3))((A4A5)(A6))\Big((A_1) (A_2 A_3) \Big) \Big( (A_4 A_5) (A_6) \Big)

    • What is the optimal split for A2A3A_2 A_3 ? ( s23=2s_{23}=2 )
    • What is the optimal split for A4A5A_4 A_5 ? ( s45=4s_{45}=4 )
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Example: Constructing an Optimal Solution

  • Reminder: sijs_{ij} is the optimal top-level split of AiAjA_i \dots A_j

  • ((A1)((A2)(k=2)(A3)))(((A4)(k=4)(A5))(A6))\bigg(\Big(A_1\Big)\Big((A_2)\overbrace{\vdots}^{ (k=2) }(A_3)\Big) \bigg) \bigg( \Big((A_4)\overbrace{\vdots}^{ (k=4) }(A_5)\Big) \Big(A_6\Big) \bigg)

    • What is the optimal split for A2A3A_2 A_3 ? ( s23=2s_{23}=2 )
    • What is the optimal split for A4A5A_4 A_5 ? ( s45=4s_{45}=4 )
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Constructing an Optimal Solution

  • Earlier optimal matrix multiplications can be computed recursively
  • Given:
    • the chain of matrices A=A1,A2,AnA = \langle A_1, A_2, \dots A_n \rangle
      the s table computed by MATRIX-CHAIN-ORDER\text{MATRIX-CHAIN-ORDER}
    • The following recursive procedure computes the matrix-chain product AijA_{i \dots j}

MATRIX-CHAIN-MULTIPLY(A,s,i,j)if j>i thenXMATRIX-CHAIN-MULTIPLY(A,s,i,s[i,j])YMATRIX-CHAIN-MULTIPLY(A,s,s[i,j]+1,j)return MATRIX-MULTIPLY(X,Y)elsereturnAi\begin{align*} & \text{MATRIX-CHAIN-MULTIPLY}(A, s, i, j) \\ & \quad \text{if} \ j > i \ \text{then} \\ & \qquad X \longleftarrow \text{MATRIX-CHAIN-MULTIPLY}(A, s, i, s[i, j]) \\ & \qquad Y \longleftarrow \text{MATRIX-CHAIN-MULTIPLY}(A, s, s[i, j]+1, j) \\ & \qquad \text{return} \ \text{MATRIX-MULTIPLY}(X, Y) \\ & \quad \text{else} \\ & \qquad return A_i \end{align*}

  • Invocation: MATRIX-CHAIN-MULTIPLY(A,s,1,n)\text{MATRIX-CHAIN-MULTIPLY}(A, s, 1, n)
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Example: Recursive Construction of an Optimal Solution

center

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Example: Recursive Construction of an Optimal Solution

center

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Example: Recursive Construction of an Optimal Solution

center

RTEU CE100 Week-5
CE100 Algorithms and Programming II

Table reference pattern for m[i,j]m[i, j] (1ijn)(1 \leq i \leq j \leq n)

  • m[i,j]m[i, j] is referenced for the computation of
    • m[i,r] for j<rn (nj)m[i, r] \ \text{for} \ j < r \leq n \ (n - j ) times
    • m[r,j] for 1r<i (i1)m[r, j] \ \text{for} \ 1 \leq r < i \ (i - 1 ) times
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Table reference pattern for m[i,j]m[i, j] (1ijn)(1 \leq i \leq j \leq n)

  • R(i,j)R(i, j) = #\# of times that m[i,j]m[i, j] is referenced in computing other entries

R(i,j)=(nj)+(i1)=(n1)(ji)\begin{align*} R(i, j) &= (n - j) + (i-1) \\ &=(n-1) - (j-i) \end{align*}

  • The total #\# of references for the entire table is: i=1nj=inR(i,j)=n3n3\sum \limits_{i=1}^{n}\sum \limits_{j=i}^{n}R(i,j)= \frac{n^3-n}{3}
RTEU CE100 Week-5
CE100 Algorithms and Programming II

Summary

  • Identification of the optimal substructure property

  • Recursive formulation to compute the cost of the optimal solution

  • Bottom-up computation of the table entries

  • Constructing the optimal solution by backtracing the table entries

RTEU CE100 Week-5
CE100 Algorithms and Programming II

References

RTEU CE100 Week-5
CE100 Algorithms and Programming II

EndOfWeek5CourseModule-End-Of-Week-5-Course-Module-

RTEU CE100 Week-5