Dynamic Programming-matrix concatenation Python Implementation Method

Source: Internet
Author: User

Dynamic Programming-matrix concatenation Python Implementation Method

This article describes the Python Implementation Method of Dynamic Planning for Matrix concatenation. We will share this with you for your reference. The details are as follows:

Given n matrices {A1, A2 ,..., An}, where Ai and Ai + 1 are multiplication, I = 1, 2 ,..., N-1. How to determine the calculation order of the matrix product, so that the number of times required to calculate the matrix product in this order is the least.

For example:

A1 = {30x35}; A2 = {35x15}; A3 = {15x5}; A4 = {5x10}; A5 = {10x20 }; a6 = {20x25 };

The result is: (A1 (A2A3) (A4A5) A6) The minimum multiplication time is 15125.

The original problem is n matrix concatenation, which breaks down the original problem into subproblems, that is, when n is equal to 1, 2, 3.
When n = 1, a single matrix does not need to be calculated. The minimum multiplication time is 0.
When n = 2, based on the result of n = 1, traverse to calculate the minimum multiplication of every two adjacent Matrices
When n = 3, according to the results of n = 1 and n = 2, the minimum multiplication times of each adjacent matrix are obtained, traverse to calculate the minimum multiplication of the adjacent three matrices
And so on ......
When n = n, according to n = 1, 2 ,...... The result of n-1. At this time, one, two, and three adjacent ones are obtained ...... Obtain the least multiplication times of n-1 matrices.

When n increases by 1, the obtained sub-structure is used to solve the optimal value at this time.

The mathematical description is as follows:

Set the dimension of matrix Ai to Pi x Pi + 1.
Let A [I: j] be the concatenation product of the matrix AiAi + 1... Aj, that is, the concatenation product from Ai to Aj, where 0 <= I <= j <= n-1
Set m [I] [j] to calculate the minimum multiplication of A [I: j]. Therefore, the optimal value of the original problem is m [0] [n-1].
When I = j, a single matrix is not required. M [I] [I] = 0, I =,... n-1
When I <j, the optimal sub-structure is used to calculate m [I] [j]. That is, find the disconnection location k (I <= k <j ), minimum m [I] [k] + m [k + 1] [j] + Pi * Pk + 1 * Pj + 1.

Python implementation of this algorithm:

# Coding = gbk # Matrix concatenation problem _ author _ = 'ice' # row_num class Matrix of the number of rows in each Matrix: def _ init _ (self, row_num = 0, col_num = 0, matrix = None): if matrix! = None: self. row_num = len (matrix) self. col_num = len (matrix [0]) else: self. row_num = row_num self. col_num = col_num self. matrix = matrixdef matrix_chain (matrixs): matrix_num = len (matrixs) count = [[0 for j in range (matrix_num)] for I in range (matrix_num)] flag = [[0 for j in range (matrix_num)] for I in range (matrix_num)] for interval in range (1, matrix_num + 1 ): for I in range (matrix_num-interv Al): 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 for k in range (I + 1, j ): temp = count [I] [k] + count [k + 1] [j] + matrixs [I]. row_num * matrixs [k + 1]. row_num * matrixs [j]. col_num if temp <count [I] [j]: count [I] [j] = temp flag [I] [j] = k traceback (0, matrix_num-1, flag) return count [0] [matrix_num-1] Def traceback (I, j, flag): if I = j: return if j-I> 1: print (str (I + 1) + '~ '+ Str (j + 1), end =': ') print (str (I + 1) + ": "+ str (flag [I] [j] + 1), end = ',') print (str (flag [I] [j] + 2) + ": "+ str (j + 1) traceback (I, flag [I] [j], flag) traceback (flag [I] [j] + 1, j, flag) matrixs = [Matrix (30, 35), Matrix (35, 15), Matrix (15, 5), Matrix (5, 10), Matrix (10, 20 ), matrix (20, 25)] result = matrix_chain (matrixs) print (result) #1 ~ 6:, #1 ~ 3:, #4 ~ 6:, #15125

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.