DP思想 之 Matrix-chain multiplication(矩阵链相乘问题)

时间:2024-05-23 22:31:30

一.矩阵链复杂度计算(根据两两相乘计算次数):

假设有A1(10*100),A2(100*5),A3(5*50)三个矩阵

((A1A2)A3) 计算顺序使用到的乘法次数为:10*100*5 + 10*5*50=7500次

(A1(A2A3)) 计算顺序使用到的乘法次数为:100*5*50 + 10*100*50=75000次

二.矩阵链相乘组合方式的计算(括号组合的方式):

假设有矩阵链 A1A2A3...An ,组合方式一共有:

DP思想 之 Matrix-chain multiplication(矩阵链相乘问题)

三.DP在矩阵链求解中的应用:

Our goal is only to determine an order for multiplying matrices that has the lowest cost. Typically, the time invested in determining this optimal order is more than paid for by the time saved later on when actually performing the matrix multiplications (such as performing only 7500 scalar multiplications instead of 75,000).

即:求出复杂度最低(相乘次数最少)的一种相乘顺序

四.求解步骤:

1.The structure of an optimal parenthesization(描述解的结构)

A(i..j) = A(i) A(i+1) ... A(j)

假设A(i..j) 最优解为子链 A(i..k) 和 A(k+1..j) 相乘, A(i..j) 为最优解的同时, A(i..k) 与 A(k+1..j) 同为最优解

2.A recursive solution(递归方式求解)

用m[i,j]表示计算矩阵A(1..n)最小复杂度,假设矩阵A(i)为一个(i-1)*i的矩阵,行列用P(i-1)和P(i)表示,则A(i..k)A(k+1..j) takes P(i-1)P(k)P(j) scalar multiplications.

m[i,j] = m[i,k]+m[k+1,j]+ Pi-1 Pk Pj

s[i,j] = k

DP思想 之 Matrix-chain multiplication(矩阵链相乘问题)