UVA 348 & ZOJ 1276 Optimal Array Multiplication Sequence(dp , 矩陣鏈相乘問題)


Optimal Array Multiplication Sequence
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu



Description

Download as PDF

Given two arrays A and B, we can determine the array C = AB using the standard definition of matrix multiplication:

The number of columns in the A array must be the same as the number of rows in the B array. Notationally, let's say that rows(A) andcolumns(A) are the number of rows and columns, respectively, in the A array. The number of individual multiplications required to compute the entire C array (which will have the same number of rows as A and the same number of columns as B) is then rows(A)columns(Bcolumns(A). For example, if A is a tex2html_wrap_inline67 array, and B is a tex2html_wrap_inline71 array, it will take tex2html_wrap_inline73 , or 3000 multiplications to compute the C array.

To perform multiplication of more than two arrays we have a choice of how to proceed. For example, if XY, and Z are arrays, then to compute XYZ we could either compute (XYZ or X (YZ). Suppose X is a tex2html_wrap_inline103 array, Y is a tex2html_wrap_inline67 array, and Z is a tex2html_wrap_inline111 array. Let's look at the number of multiplications required to compute the product using the two different sequences:

(XYZ

  • tex2html_wrap_inline119 multiplications to determine the product (X Y), a tex2html_wrap_inline123 array.
  • Then tex2html_wrap_inline125 multiplications to determine the final result.
  • Total multiplications: 4500.

X (YZ)

  • tex2html_wrap_inline133 multiplications to determine the product (YZ), a tex2html_wrap_inline139 array.
  • Then tex2html_wrap_inline141 multiplications to determine the final result.
  • Total multiplications: 8750.

Clearly we'll be able to compute (XYZ using fewer individual multiplications.

Given the size of each array in a sequence of arrays to be multiplied, you are to determine an optimal computational sequence. Optimality, for this problem, is relative to the number of individual multiplications required.

Input

For each array in the multiple sequences of arrays to be multiplied you will be given only the dimensions of the array. Each sequence will consist of an integer N which indicates the number of arrays to be multiplied, and then N pairs of integers, each pair giving the number of rows and columns in an array; the order in which the dimensions are given is the same as the order in which the arrays are to be multiplied. A value of zero for N indicates the end of the input. N will be no larger than 10.

Output

Assume the arrays are named tex2html_wrap_inline157 . Your output for each input case is to be a line containing a parenthesized expression clearly indicating the order in which the arrays are to be multiplied. Prefix the output for each case with the case number (they are sequentially numbered, starting with 1). Your output should strongly resemble that shown in the samples shown below. If, by chance, there are multiple correct sequences, any of these will be accepted as a valid answer.

Sample 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

Sample Output

Case 1: (A1 x (A2 x A3))
Case 2: ((A1 x A2) x A3)
Case 3: ((A1 x (A2 x A3)) x ((A4 x A5) x A6))




這是一個矩陣鏈相乘問題,是個典型的dp問題。

問題描述

兩個矩陣A和B可以相乘當且僅當A的列數等於B的行數,一個m * p的矩陣乘以一個p * n的矩陣等於一個m * n的矩陣,運算量爲mpn。

矩陣乘法不滿足分配律,但滿足結合律,A * B * C可以按順序(A * B) * C,也可以是A * (B * C)假設A B C分別是 2 *3、3 * 4、4 * 5的則用第一種算法運算量爲64,第二種運算量爲90。

要解決的問題是給你n個矩陣,算出它們相乘所用的最少運算量。

思路描述:

運用dp思想。

定義計算A[i,j](第i個矩陣乘以第j個矩陣)所需要的最少次數爲dp[i][j];則最優解就是dp[1][n];

我們可以知道當i = j時,即爲矩陣本身,那麼dp[i][j]= 0;

當i < j 時,可利用最優子結構性質計算dp[i][j],假設AiAi+1…Aj 的最優解在Ak和Ak+1之間分開,其中i <= k < j 則dp[i][j]等於計算子乘積dp[i][k]和dp[k][j]的和再加上兩個矩陣相乘。(每個矩陣的維度是pi-1 * pi)

           dp[i][j] = dp[i][k] + dp[k+1][j] + pi-1 * pk * pj

然後我們判斷k在每個斷點,求出dp[i][j] 的最小值



附上代碼:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;

#define maxn 15
int s[maxn][maxn], n, l;
char ans[150];

void TraceBack(int i, int j)
{
    if(i==j)
    {
        ans[l++] = 'A';
        if(i == 10)
        {
            ans[l++] = '1';
            ans[l++] = '0';
        }
        else
            ans[l++] = i + '0';
    }
    else
    {
        ans[l++] = '(';
        TraceBack(i,s[i][j]);
        TraceBack(s[i][j]+1,j);
        ans[l++] = ')';
    }
}

int main()
{
    int p[maxn], dp[maxn][maxn];
    int i, temp, j, k, q = 1;
    while(scanf("%d",&n) && n)
    {
        for(i = 0; i < n; i++)
            scanf("%d%d",&p[i],&temp);//p數組儲存每個矩陣的行
        if(n == 1)
        {
            printf("Case %d: ",q++);
            printf("(A1)\n");
            continue;
        }
        p[i] = temp; // 最後一個矩陣的列
        memset(dp,0,sizeof(dp));//初始化dp
        memset(s, 0, sizeof(s));
        for(i = 2; i <= n; i++) // 對角線,先從2號對角線開始填數。
        {
            for(j = 1; j <= n - i + 1; j++)//j是從第幾個矩陣開始算
            {
                int t = j + i - 1;//t當前計算的最後一個矩陣,即對角線的最後一個元素
                dp[j][t] = dp[j + 1][t] + p[j-1] * p[j] * p[t];//先判斷按順序計算的值
                s[j][t] = j;
                for(k = j + 1; k < t; k++)//k代表斷點處
                {
                    int r = dp[j][k] + dp[k+1][t] + p[j - 1] * p[k] * p[t];//計算從第k個斷點處的值,
                    if(r < dp[j][t])
                    {
                        dp[j][t] = r;
                        s[j][t]  = k;
                    }
                }
            }
        }
        l = 0;
        printf("Case %d: ",q++);
        TraceBack(1,n);
        ans[l] = '\0';
        for(i = 0; i < l; i++)
        {
            printf("%c",ans[i]);
            if(ans[i] >= '0' && ans[i] <= '9')
            {
                if(ans[i + 1] == '0')
                    continue;
                if(ans[i + 1] != ')')
                    printf(" x ");
            }
            else if(ans[i] == ')')
            {
                if(ans[i + 1] == '(' || ans[i + 1] == 'A')
                    printf(" x ");
            }
        }
        printf("\n");
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章