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/
2+ | Find the minimum number of multiplications needed to multiply chain of matrices.
3+ | Reference: https://www.geeksforgeeks.org/matrix-chain-multiplication-dp-8/
44
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.
136
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.
1615
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::
2117
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.
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``
36+ can be done.
37+ 4. To determine the required order, we can try different combinations.
38+
3439So, this problem has overlapping sub-problems and can be solved using recursion.
3540We use Dynamic Programming for optimal time complexity.
3641
3742Example input:
38- arr = [40, 20, 30, 10, 30]
39- output: 26000
43+ ``arr = [40, 20, 30, 10, 30]``
44+ output:
45+ ``26000``
4046"""
4147
4248from collections .abc import Iterator
@@ -50,25 +56,25 @@ def matrix_chain_multiply(arr: list[int]) -> int:
5056 Find the minimum number of multiplcations required to multiply the chain of matrices
5157
5258 Args:
53- arr: The input array of integers.
59+ ` arr` : The input array of integers.
5460
5561 Returns:
5662 Minimum number of multiplications needed to multiply the chain
5763
5864 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
7278 """
7379 if len (arr ) < 2 :
7480 return 0
@@ -93,8 +99,10 @@ def matrix_chain_multiply(arr: list[int]) -> int:
9399def matrix_chain_order (dims : list [int ]) -> int :
94100 """
95101 Source: https://en.wikipedia.org/wiki/Matrix_chain_multiplication
102+
96103 The dynamic programming solution is faster than cached the recursive solution and
97104 can handle larger inputs.
105+
98106 >>> matrix_chain_order([1, 2, 3, 4, 3])
99107 30
100108 >>> matrix_chain_order([10])
@@ -105,8 +113,7 @@ def matrix_chain_order(dims: list[int]) -> int:
105113 722
106114 >>> matrix_chain_order(list(range(1, 100)))
107115 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
110117 # 2626798
111118 """
112119
0 commit comments