Optimal Array Multiplication Sequence

Interview

Time limit1sMemory limit128 MB

Summary
Given the dimensions of a chain of matrices, find the minimum number of scalar multiplications needed to compute their product.
Level

Medium4 of 10

Topics
Dynamic programming, Matrix, Array, Implementation
Solved
No attempts yet

Problem

Given two arrays AA and BB, we can determine the array C=A×BC = A \times B using the standard definition of matrix multiplication:

Ci,j=∑kAi,k×Bk,jC_{i,j} = \sum_{k} A_{i,k} \times B_{k,j}

For this product to be defined, the number of columns in AA must equal the number of rows in BB. Let rows(A)\text{rows}(A) and cols(A)\text{cols}(A) denote the number of rows and columns of AA, respectively. The result CC has the same number of rows as AA and the same number of columns as BB, and the number of scalar multiplications needed to compute all of CC is

rows(A)×cols(B)×cols(A).\text{rows}(A) \times \text{cols}(B) \times \text{cols}(A).

For example, if AA is a 10×2010 \times 20 array and BB is a 20×1520 \times 15 array, it takes 10×15×20=300010 \times 15 \times 20 = 3000 multiplications to compute CC.

To multiply more than two arrays we may choose how to proceed. For instance, the product X×Y×ZX \times Y \times Z can be computed as (X×Y)×Z(X \times Y) \times Z or as X×(Y×Z)X \times (Y \times Z). Suppose XX is 5×105 \times 10, YY is 10×2010 \times 20, and ZZ is 20×3520 \times 35. The two orders require different numbers of multiplications:

(X×Y)×Z(X \times Y) \times ZX×(Y×Z)X \times (Y \times Z)
X×YX \times Y: 5×20×10=10005 \times 20 \times 10 = 1000 (result is 5×205 \times 20)Y×ZY \times Z: 10×35×20=700010 \times 35 \times 20 = 7000 (result is 10×3510 \times 35)
then 5×35×20=35005 \times 35 \times 20 = 3500then 5×35×10=17505 \times 35 \times 10 = 1750
total: 45004500total: 87508750

The order clearly affects how many multiplications are required. Given the size of each array in a sequence of arrays to be multiplied, determine the minimum possible number of scalar multiplications over all valid orders of computing the product.

Input

The input consists of several test cases. Each test case begins with an integer NN, the number of arrays to be multiplied, followed by NN pairs of integers giving the number of rows and columns of each array, in the same order in which the arrays are to be multiplied. NN is no larger than 1010. A value of 00 for NN indicates the end of the input. Adjacent arrays are always dimensioned so that their product is defined.

Output

For each test case, output on a single line the minimum number of scalar multiplications needed to compute the product of all the arrays. Prefix each line with the case number in the form Case X: , numbered sequentially starting from 11.

Examples1

  1. Example 1

    Input
    3
        1 5
        5 20
        20 1
    
    3
        5 10
        10 20
        20 35
    
    6
        30 35
        35 15
        15 5
        5 10
        10 20
        20 25
    
    0
    
    Expected output
    Case 1: 105
    Case 2: 4500
    Case 3: 15125