11""" 
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- 
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. 
13- 
14- Python doctests can be run with the following command: 
15- python -m doctest -v matrix_chain_multiply.py 
16- 
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. 
21- 
22- matrix_chain_multiply() returns an integer denoting minimum number of multiplications to 
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+ 
5+ The algorithm has interesting real-world applications. 
6+ 
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. 
15+ 
16+ Python doctests can be run with the following command:: 
17+ 
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 of 
21+ 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 multiplications to 
2326multiply the chain. 
2427
2528We do not need to perform actual multiplication here. 
2629We only need to decide the order in which to perform the multiplication. 
2730
2831Hints: 
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`` can be done. 
36+   4. To determine the required order, we can try different combinations. 
37+ 
3438So, this problem has overlapping sub-problems and can be solved using recursion. 
3539We use Dynamic Programming for optimal time complexity. 
3640
3741Example input: 
38- arr = [40, 20, 30, 10, 30] 
39- output: 26000 
42+     ``arr = [40, 20, 30, 10, 30]`` 
43+ output: 
44+     ``26000`` 
4045""" 
4146
4247from  collections .abc  import  Iterator 
@@ -50,25 +55,25 @@ def matrix_chain_multiply(arr: list[int]) -> int:
5055    Find the minimum number of multiplcations required to multiply the chain of matrices 
5156
5257    Args: 
53-         arr: The input array of integers. 
58+         ` arr` : The input array of integers. 
5459
5560    Returns: 
5661        Minimum number of multiplications needed to multiply the chain 
5762
5863    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 
64+ 
65+     >>> matrix_chain_multiply([1, 2, 3, 4, 3])  
66+     30  
67+     >>> matrix_chain_multiply([10])  
68+     0  
69+     >>> matrix_chain_multiply([10, 20])  
70+     0  
71+     >>> matrix_chain_multiply([19, 2, 19])  
72+     722  
73+     >>> matrix_chain_multiply(list(range(1, 100)))  
74+     323398 
75+     # >>> matrix_chain_multiply(list(range(1, 251))) 
76+     # 2626798 
7277    """ 
7378    if  len (arr ) <  2 :
7479        return  0 
@@ -93,8 +98,10 @@ def matrix_chain_multiply(arr: list[int]) -> int:
9398def  matrix_chain_order (dims : list [int ]) ->  int :
9499    """ 
95100    Source: https://en.wikipedia.org/wiki/Matrix_chain_multiplication 
101+ 
96102    The dynamic programming solution is faster than cached the recursive solution and 
97103    can handle larger inputs. 
104+ 
98105    >>> matrix_chain_order([1, 2, 3, 4, 3]) 
99106    30 
100107    >>> matrix_chain_order([10]) 
@@ -105,7 +112,6 @@ def matrix_chain_order(dims: list[int]) -> int:
105112    722 
106113    >>> matrix_chain_order(list(range(1, 100))) 
107114    323398 
108- 
109115    # >>> matrix_chain_order(list(range(1, 251)))  # Max before RecursionError is raised 
110116    # 2626798 
111117    """ 
0 commit comments