Given n matrices {a1, a2,..., an}, where Ai and ai+1 are multiplicative, i=1,2,..., n-1. How to determine the calculation order of the multiplication of the matrix, making it necessary to calculate the number of times of the matrix multiplication in this order.
For example:
A1={30x35}; A2={35x15}; A3={15x5}; A4={5x10}; A5={10x20}; A6={20x25};
The results are as follows: ((a1(a2a3)) ((a4a5) A6) The smallest multiplier is 15125.
The original problem is n matrix multiplication, the original problem is decomposed into sub-problem, that is, when n equals three-in-one ....
When n==1 , a single matrix does not need to be computed. The minimum multiply is 0
n==2 , based on the results of n==1, the Traverse calculates the minimum multiply of each adjacent two matrices.
n==3 , based on the results of N==1 and n==2 , the Traverse calculates the minimum multiply of each adjacent three matrices.
......
When n==n , according to N==1, 2 、...... The result of N-1 , at this time has been found each adjacent one, 2, 3 ... n-1 matrix of the smallest multiplication , thus finding the minimum multiply times n==n
Whenever n increases by 1 o'clock, the obtained substructure is used to solve the optimal value at this time.
The mathematical description is as follows:
The dimensionality of the set matrix AI is Pi xpi+1.
Set a[i:j] for matrix Aiai+1.... The product of aJ , that is, the continuous product from AI to AJ, wherein 0 <= i <= J <= n-1
Set M[I][j] to calculate the minimum multiplication of a[i:j], so the optimal value of the original problem is m[0][n-1].
When i==j , a single matrix is not calculated. m[i][i]=0, i=0,1,....n-1
When i < J , the optimal substructure is used to calculate m[i][j]. That is to find the Disconnect position K(I <= K < J), making m[i][k]+m[k+1][j]+pi*pk+1*pj+1 minimum.
The python implementation of the algorithm:
1 #CODING=GBK2 #matrix multiplication Problem3 __author__='Ice'4 5 6 #row_num The number of rows per matrix7 classMatrix:8 def __init__(Self, row_num=0, col_num=0, matrix=None):9 ifMatrix! =None:TenSelf.row_num =len (Matrix) OneSelf.col_num =Len (matrix[0]) A Else: -Self.row_num =row_num -Self.col_num =Col_num theSelf.matrix =Matrix - - - defMatrix_chain (matrixs): +Matrix_num =Len (matrixs) -Count = [[0 forJinchRange (Matrix_num)] forIinchrange (matrix_num)] +flag = [[0 forJinchRange (Matrix_num)] forIinchrange (matrix_num)] A forIntervalinchRange (1, Matrix_num + 1): at forIinchRange (Matrix_num-interval): -j = i +interval -COUNT[I][J] = Count[i][i] + count[i + 1][j] + matrixs[i].row_num * matrixs[i + 1].row_num *Matrixs[j].col_num -FLAG[I][J] =I - forKinchRange (i + 1, j): -temp = Count[i][k] + count[k + 1][j] + matrixs[i].row_num * matrixs[k + 1].row_num *Matrixs[j].col_num in ifTemp <Count[i][j]: -COUNT[I][J] =Temp toFLAG[I][J] =k +Traceback (0, Matrix_num-1, Flag) - returnCount[0][matrix_num-1] the * $ defTraceback (i, J, flag):Panax Notoginseng ifi = =J: - return the ifJ-i > 1: + Print(Str (i + 1) +'~'+ STR (j + 1), end=': ') A Print(Str (i + 1) +":"+ STR (FLAG[I][J] + 1), end=',') the Print(Str (FLAG[I][J] + 2) +":"+ STR (j + 1)) + Traceback (i, flag[i][j], flag) -Traceback (Flag[i][j] + 1, J, Flag) $ $ -Matrixs = [Matrix (+), Matrix (5), Matrix (5, 20), Matrix (Ten), Matrix (25))] -result =Matrix_chain (Matrixs) the Print(Result) - Wuyi #1~6:1:3,4:6 the #1~3:1:1,2:3 - #4~6:4:5,6:6 Wu #15125
Matrix multiplication of dynamic programming