forked from mission-peace/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_chain_order.py
More file actions
51 lines (35 loc) · 1.39 KB
/
matrix_chain_order.py
File metadata and controls
51 lines (35 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Problem Statement
=================
Given an array p[] which represents the chain of matrices such that the ith matrix Ai is of dimension p[i-1] x p[i]. We
need to write a function matrix_chain_order() that should return the minimum number of multiplications needed to
multiply the chain.
Video
-----
* https://youtu.be/vgLJZMUfnsU
Note
----
In the code below we give matrices length as an array and each matrix takes 2 indices from the array.
For e.g. {2, 3, 4} represents two matrices (2, 3) and (3, 4) in (row, col) format.
Complexity
----------
Time Complexity: O(n^3)
Reference
---------
* http://www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/
"""
def matrix_chain_order(matrices):
matrices_length = len(matrices)
T = [[0 for _ in range(matrices_length)] for _ in range(matrices_length)]
for gap in range(2, matrices_length):
for index_i in range(0, matrices_length - gap):
index_j = index_i + gap
T[index_i][index_j] = 10000
for index_k in range(index_i + 1, index_j):
temp = T[index_i][index_k] + T[index_k][index_j] + matrices[index_i] * matrices[index_k] * matrices[index_j]
if temp < T[index_i][index_j]:
T[index_i][index_j] = temp
return T[0][-1]
if __name__ == '__main__':
matrices = [4, 2, 3, 5, 3]
assert 84 == matrix_chain_order(matrices)