Dynamic programming algorithm mnemonic


title: dynamic programming algorithm mnemonic

Never say die !

Matrix continually multiply problems

Subject description:

Given n matrix {A1, A2, ..., An}, where Ai and Ai + 1 is multiplicative, i = 1,2, ..., n-1. How to determine the order computing the matrix even calculate the product, so that even in this order compute the matrix multiplication product of the minimum number required. E.g:

A1 = {30x35}

A2 = {35x15}

A3 = {15x5}

A4 = {5x10}

A5 = {10x20}

A6 = {20x25}

The final result is: ((A1 (A2A3)) ((A4A5) A6)) multiplied times for a minimum of 15125.

Problem-solving ideas:

A property can be dynamic programming is optimal substructure property.

That calculation A: calculated optimal order sub-matrix [i j] included in the Lian A [i: k] and A [k + 1: j] is the best order.

Dynamic programming algorithm solving this problem, which can be based on a recursive formula is calculated (i.e., starting with the smallest counted) in a bottom-up fashion.

In the calculation process, the answer to save the child's problem has been resolved. Each sub-question is calculated only once, back when simply need to check to avoid a lot of double counting, polynomial time algorithm to obtain final.

We can calculate results based on the following formula. Wherein p [i-1] represents the number of rows in the i-th matrix, p [k] denotes i: number of column k of the matrix after combined the resulting, p [j] is a k + 1: obtained after together j the number of columns. This calculation method is actually part of the total calculated by multiplying the number of times when multiplying two matrices.

official:

Even by the counted number per matrix 2 times the minimum multiplier m [i] [j]:

 m[0][1] m[1][2] m[2][3] m[3][4] m[4][5]  //m[0][1]表示第一个矩阵与第二个矩阵的最小乘次数

Then calculate in turn calculates the number of multiplicative matrix 3:

m[0][2] m[1][3] m[2][4] m[3][5]

Matrix continually multiply the number 4:

m[0][3] m[1][4] m[2][5]

Matrix continually multiply the number 5:

m[0][4] m[1][5]

Matrix continually multiply the number of 6:

m[0][5]    //即最后我们要的结果

Code

Guess you like

Origin www.cnblogs.com/fofade/p/10977660.html