2082 3

CSC325 ยท 2082 ยท 10 marks

Distinguish between dynamic programming and memorization. Parenthesize the matrices A(30 ร— 1), B(1 ร— 40), C(40 ร— 10) and A(10 ร— 15), for computing matrix multiplication using dynamic programming.

Distinguish between dynamic programming and memorization. Parenthesize the matrices A(30 ร— 1), B(1 ร— 40), C(40 ร— 10) and A(10 ร— 15), for computing matrix multiplication using dynamic programming.[10]

Design and Analysis of AlgorithmsChapter 5: Dynamic ProgrammingDP AlgorithmsNumerical: Matrix chain multiplication

Dynamic Programming vs Memoization & Matrix Chain Multiplication

Part 1: Dynamic Programming vs Memoization

FeatureDynamic ProgrammingMemoization
ApproachBottom-upTop-down
ImplementationIterative (fills a table)Recursive with a cache
Sub-problems solvedAll sub-problems computedOnly the sub-problems actually needed
OverheadNo recursion overheadRecursion call/return overhead
StorageResults stored in a table filled in orderResults stored in a lookup table on first computation
NatureA general problem-solving technique (optimal substructure + overlapping subproblems)An optimization applied to recursion by caching results

Key idea: Memoization is essentially the top-down (recursive) way of realizing dynamic programming; it "remembers" previously computed results so they are not recomputed.

Part 2: Matrix Chain Parenthesization

Given data

  • $A = 30 \times 1$
  • $B = 1 \times 40$
  • $C = 40 \times 10$
  • $D = 10 \times 15$ (the last matrix, written A(10 ร— 15) in the question)

Dimension array: $$p = [30,,1,,40,,10,,15]$$ $p_0=30,\ p_1=1,\ p_2=40,\ p_3=10,\ p_4=15,\quad n=4$

Recurrence

$$m[i][j]=\min_{i\le k<j}{m[i][k]+m[k+1][j]+p_{i-1},p_k,p_j}$$ Base case $m[i][i]=0$.

Length $l=2$

$m[1][2]=0+0+30\cdot1\cdot40=1200,\ s=1$

$m[2][3]=0+0+1\cdot40\cdot10=400,\ s=2$

$m[3][4]=0+0+40\cdot10\cdot15=6000,\ s=3$

Length $l=3$

$m[1][3]$:

  • $k=1:\ 0+400+30\cdot1\cdot10=400+300=700$
  • $k=2:\ 1200+0+30\cdot40\cdot10=1200+12000=13200$

$m[1][3]=700,\ s=1$

$m[2][4]$:

  • $k=2:\ 0+6000+1\cdot40\cdot15=6000+600=6600$
  • $k=3:\ 400+0+1\cdot10\cdot15=400+150=550$

$m[2][4]=550,\ s=3$

Length $l=4$

$m[1][4]$:

  • $k=1:\ m[1][1]+m[2][4]+p_0p_1p_4=0+550+30\cdot1\cdot15=550+450=1000$
  • $k=2:\ m[1][2]+m[3][4]+p_0p_2p_4=1200+6000+30\cdot40\cdot15=7200+18000=25200$
  • $k=3:\ m[1][3]+m[4][4]+p_0p_3p_4=700+0+30\cdot10\cdot15=700+4500=5200$

$$m[1][4]=\min(1000,25200,5200)=1000,\quad s[1][4]=1$$

Optimal parenthesization (from s-table)

  • $s[1][4]=1 \Rightarrow (A_1)(A_2A_3A_4)$
  • $s[2][4]=3 \Rightarrow (A_2A_3)(A_4)$
  • $s[2][3]=2 \Rightarrow (A_2)(A_3)$

Putting it together: $$\big(A,((B,C),D)\big)$$

Result

  • Minimum number of scalar multiplications = $1000$
  • Optimal order: $(A,((B,C),D))$

Verification of the count:

  • $B\cdot C:\ 1\cdot40\cdot10=400$, result $1\times10$
  • $(BC)\cdot D:\ 1\cdot10\cdot15=150$, result $1\times15$
  • $A\cdot(BCD):\ 30\cdot1\cdot15=450$, result $30\times15$
  • Total $=400+150+450=1000$ โœ“

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.