1
1
"""
2
- Find the minimum number of multiplications needed to multiply chain of matrices.
3
- Reference: https://www.geeksforgeeks.org/matrix-chain-multiplication-dp-8/
2
+ | Find the minimum number of multiplications needed to multiply chain of matrices.
3
+ | Reference: https://www.geeksforgeeks.org/matrix-chain-multiplication-dp-8/
4
4
5
- The algorithm has interesting real-world applications. Example:
6
- 1. Image transformations in Computer Graphics as images are composed of matrix.
7
- 2. Solve complex polynomial equations in the field of algebra using least processing
8
- power.
9
- 3. Calculate overall impact of macroeconomic decisions as economic equations involve a
10
- number of variables.
11
- 4. Self-driving car navigation can be made more accurate as matrix multiplication can
12
- accurately determine position and orientation of obstacles in short time.
5
+ The algorithm has interesting real-world applications.
13
6
14
- Python doctests can be run with the following command:
15
- python -m doctest -v matrix_chain_multiply.py
7
+ Example:
8
+ 1. Image transformations in Computer Graphics as images are composed of matrix.
9
+ 2. Solve complex polynomial equations in the field of algebra using least processing
10
+ power.
11
+ 3. Calculate overall impact of macroeconomic decisions as economic equations involve a
12
+ number of variables.
13
+ 4. Self-driving car navigation can be made more accurate as matrix multiplication can
14
+ accurately determine position and orientation of obstacles in short time.
16
15
17
- Given a sequence arr[] that represents chain of 2D matrices such that the dimension of
18
- the ith matrix is arr[i-1]*arr[i].
19
- So suppose arr = [40, 20, 30, 10, 30] means we have 4 matrices of dimensions
20
- 40*20, 20*30, 30*10 and 10*30.
16
+ Python doctests can be run with the following command::
21
17
22
- matrix_chain_multiply() returns an integer denoting minimum number of multiplications to
23
- multiply the chain.
18
+ python -m doctest -v matrix_chain_multiply.py
19
+
20
+ Given a sequence ``arr[]`` that represents chain of 2D matrices such that the dimension
21
+ of the ``i`` th matrix is ``arr[i-1]*arr[i]``.
22
+ So suppose ``arr = [40, 20, 30, 10, 30]`` means we have ``4`` matrices of dimensions
23
+ ``40*20``, ``20*30``, ``30*10`` and ``10*30``.
24
+
25
+ ``matrix_chain_multiply()`` returns an integer denoting minimum number of
26
+ multiplications to multiply the chain.
24
27
25
28
We do not need to perform actual multiplication here.
26
29
We only need to decide the order in which to perform the multiplication.
27
30
28
31
Hints:
29
- 1. Number of multiplications (ie cost) to multiply 2 matrices
30
- of size m*p and p*n is m*p*n.
31
- 2. Cost of matrix multiplication is associative ie (M1*M2)*M3 != M1*(M2*M3)
32
- 3. Matrix multiplication is not commutative. So, M1*M2 does not mean M2*M1 can be done.
33
- 4. To determine the required order, we can try different combinations.
32
+ 1. Number of multiplications (ie cost) to multiply ``2`` matrices
33
+ of size ``m*p`` and ``p*n`` is ``m*p*n``.
34
+ 2. Cost of matrix multiplication is not associative ie ``(M1*M2)*M3 != M1*(M2*M3)``
35
+ 3. Matrix multiplication is not commutative. So, ``M1*M2`` does not mean ``M2*M1``
36
+ can be done.
37
+ 4. To determine the required order, we can try different combinations.
38
+
34
39
So, this problem has overlapping sub-problems and can be solved using recursion.
35
40
We use Dynamic Programming for optimal time complexity.
36
41
37
42
Example input:
38
- arr = [40, 20, 30, 10, 30]
39
- output: 26000
43
+ ``arr = [40, 20, 30, 10, 30]``
44
+ output:
45
+ ``26000``
40
46
"""
41
47
42
48
from collections .abc import Iterator
@@ -50,25 +56,25 @@ def matrix_chain_multiply(arr: list[int]) -> int:
50
56
Find the minimum number of multiplcations required to multiply the chain of matrices
51
57
52
58
Args:
53
- arr: The input array of integers.
59
+ ` arr` : The input array of integers.
54
60
55
61
Returns:
56
62
Minimum number of multiplications needed to multiply the chain
57
63
58
64
Examples:
59
- >>> matrix_chain_multiply([1, 2, 3, 4, 3])
60
- 30
61
- >>> matrix_chain_multiply([10])
62
- 0
63
- >>> matrix_chain_multiply([10, 20])
64
- 0
65
- >>> matrix_chain_multiply([19, 2, 19])
66
- 722
67
- >>> matrix_chain_multiply(list(range(1, 100)))
68
- 323398
69
-
70
- # >>> matrix_chain_multiply(list(range(1, 251)))
71
- # 2626798
65
+
66
+ >>> matrix_chain_multiply([1, 2, 3, 4, 3])
67
+ 30
68
+ >>> matrix_chain_multiply([10])
69
+ 0
70
+ >>> matrix_chain_multiply([10, 20])
71
+ 0
72
+ >>> matrix_chain_multiply([19, 2, 19])
73
+ 722
74
+ >>> matrix_chain_multiply(list(range(1, 100)))
75
+ 323398
76
+ >>> # matrix_chain_multiply(list(range(1, 251)))
77
+ # 2626798
72
78
"""
73
79
if len (arr ) < 2 :
74
80
return 0
@@ -93,8 +99,10 @@ def matrix_chain_multiply(arr: list[int]) -> int:
93
99
def matrix_chain_order (dims : list [int ]) -> int :
94
100
"""
95
101
Source: https://en.wikipedia.org/wiki/Matrix_chain_multiplication
102
+
96
103
The dynamic programming solution is faster than cached the recursive solution and
97
104
can handle larger inputs.
105
+
98
106
>>> matrix_chain_order([1, 2, 3, 4, 3])
99
107
30
100
108
>>> matrix_chain_order([10])
@@ -105,8 +113,7 @@ def matrix_chain_order(dims: list[int]) -> int:
105
113
722
106
114
>>> matrix_chain_order(list(range(1, 100)))
107
115
323398
108
-
109
- # >>> matrix_chain_order(list(range(1, 251))) # Max before RecursionError is raised
116
+ >>> # matrix_chain_order(list(range(1, 251))) # Max before RecursionError is raised
110
117
# 2626798
111
118
"""
112
119
0 commit comments