From 93d2cdd3c4db6fd85d55c5df8129374a60cf60d1 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 15 Oct 2023 20:54:35 +0000 Subject: [PATCH 01/10] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 5c63e6316547..b0ab8a65c2b6 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -182,6 +182,7 @@ * [Permutations](data_structures/arrays/permutations.py) * [Prefix Sum](data_structures/arrays/prefix_sum.py) * [Product Sum](data_structures/arrays/product_sum.py) + * [Sparse Table](data_structures/arrays/sparse_table.py) * Binary Tree * [Avl Tree](data_structures/binary_tree/avl_tree.py) * [Basic Binary Tree](data_structures/binary_tree/basic_binary_tree.py) @@ -370,6 +371,7 @@ * [Builtin Voltage](electronics/builtin_voltage.py) * [Carrier Concentration](electronics/carrier_concentration.py) * [Charging Capacitor](electronics/charging_capacitor.py) + * [Charging Inductor](electronics/charging_inductor.py) * [Circular Convolution](electronics/circular_convolution.py) * [Coulombs Law](electronics/coulombs_law.py) * [Electric Conductivity](electronics/electric_conductivity.py) From 8862a593f4e7eb8a50c14374e875babb79df45b9 Mon Sep 17 00:00:00 2001 From: Pooja Sharma Date: Mon, 16 Oct 2023 02:26:51 +0530 Subject: [PATCH 02/10] spell changes --- .../matrix_chain_multiplication.py | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 dynamic_programming/matrix_chain_multiplication.py diff --git a/dynamic_programming/matrix_chain_multiplication.py b/dynamic_programming/matrix_chain_multiplication.py new file mode 100644 index 000000000000..bbc12ebbac2e --- /dev/null +++ b/dynamic_programming/matrix_chain_multiplication.py @@ -0,0 +1,81 @@ +""" +Find the minimum number of multiplications needed to multiply chain of matrices. +Reference: https://www.geeksforgeeks.org/matrix-chain-multiplication-dp-8/ + +Python doctests can be run with the following command: +python -m doctest -v matrix_chain_multiply.py + +Given a sequence arr[] that represents chain of 2D matrices such that +the dimension of ith matrix is arr[i-1]*arr[i]. +So suppose arr = [40, 20, 30, 10, 30] means we have 4 matrices of +dimensions 40*20, 20*30, 30*10 and 10*30. + +matrix_chain_multiply() returns an integer denoting +minimum number of multiplications to multiply the chain. + +We do not need to perform actual multiplication here. +We only need to decide the order in which to perform the multiplication. + +Hints: +1. Number of multiplications (ie cost) to multiply 2 matrices +of size m*p and p*n is m*p*n. +2. Cost of matrix multiplication is neither associative ie (M1*M2)*M3 != M1*(M2*M3) +3. Matrix multiplication is not commutative. So, M1*M2 does not mean M2*M1 can be done. +4. To determine the required order, we can try different combinations. +So, this problem has overlapping sub-problems and can be solved using recursion. +We use Dynamic Programming for optimal time complexity. + +Example input : +arr = [40, 20, 30, 10, 30] +output : 26000 +""" +import sys + + +def matrix_chain_multiply(arr: list[int]) -> int: + """ + Find the minimum number of multiplcations to multiply + chain of matrices. + + Args: + arr : The input array of integers. + + Returns: + int: Minimum number of multiplications needed to multiply the chain + + Examples: + >>> matrix_chain_multiply([1,2,3,4,3]) + 30 + >>> matrix_chain_multiply([10]) + 0 + >>> matrix_chain_multiply([10, 20]) + 0 + >>> matrix_chain_multiply([19, 2, 19]) + 722 + """ + # first edge case + if len(arr) < 2: + return 0 + # initialising 2D dp matrix + n = len(arr) + int_max = sys.maxsize + dp = [[int_max for j in range(n)] for i in range(n)] + # we want minimum cost of multiplication of matrices + # of dimension (i*k) and (k*j). This cost is arr[i-1]*arr[k]*arr[j]. + for i in range(n - 1, 0, -1): + for j in range(i, n): + if i == j: + dp[i][j] = 0 + continue + for k in range(i, j): + dp[i][j] = min( + dp[i][j], dp[i][k] + dp[k + 1][j] + arr[i - 1] * arr[k] * arr[j] + ) + + return dp[1][n - 1] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From cadeb59865fa81781602aab48f9e63a5671f70a6 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 15 Oct 2023 21:01:13 +0000 Subject: [PATCH 03/10] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index b0ab8a65c2b6..88fb199e5b6b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -341,6 +341,7 @@ * [Longest Increasing Subsequence O(Nlogn)](dynamic_programming/longest_increasing_subsequence_o(nlogn).py) * [Longest Palindromic Subsequence](dynamic_programming/longest_palindromic_subsequence.py) * [Longest Sub Array](dynamic_programming/longest_sub_array.py) + * [Matrix Chain Multiplication](dynamic_programming/matrix_chain_multiplication.py) * [Matrix Chain Order](dynamic_programming/matrix_chain_order.py) * [Max Non Adjacent Sum](dynamic_programming/max_non_adjacent_sum.py) * [Max Product Subarray](dynamic_programming/max_product_subarray.py) From 8c31e7495291d6f048ba6a7428228ec459ce0195 Mon Sep 17 00:00:00 2001 From: Pooja Sharma Date: Mon, 16 Oct 2023 03:30:42 +0530 Subject: [PATCH 04/10] real world applications --- dynamic_programming/matrix_chain_multiplication.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/dynamic_programming/matrix_chain_multiplication.py b/dynamic_programming/matrix_chain_multiplication.py index bbc12ebbac2e..173fe79c2ca7 100644 --- a/dynamic_programming/matrix_chain_multiplication.py +++ b/dynamic_programming/matrix_chain_multiplication.py @@ -2,6 +2,15 @@ Find the minimum number of multiplications needed to multiply chain of matrices. Reference: https://www.geeksforgeeks.org/matrix-chain-multiplication-dp-8/ +The algorithm has interesting real-world applications. Example: +1. Image transformations in Computer Graphics as images are composed of matrix. +2. Solve complex polynomial equations in the field of algebra using least +processing power. +3. Calculate overall impact of macroeconomic decisions as economic +equations involve number of variables. +4. Self-driving car navigation can be made more accurate as matrix multiplication +can accurately determine position and orientation of obstacles in short time. + Python doctests can be run with the following command: python -m doctest -v matrix_chain_multiply.py From f8833f145877b0a7ee302ec8ece7b746061056b3 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 15 Oct 2023 22:10:34 +0000 Subject: [PATCH 05/10] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 41b7fbc0a1c2..cef1e06b78aa 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -527,6 +527,7 @@ * [Simplex](linear_programming/simplex.py) ## Machine Learning + * [Apriori Algorithm](machine_learning/apriori_algorithm.py) * [Astar](machine_learning/astar.py) * [Data Transformations](machine_learning/data_transformations.py) * [Decision Tree](machine_learning/decision_tree.py) @@ -557,7 +558,6 @@ * [Word Frequency Functions](machine_learning/word_frequency_functions.py) * [Xgboost Classifier](machine_learning/xgboost_classifier.py) * [Xgboost Regressor](machine_learning/xgboost_regressor.py) - * [Apriori Algorithm](machine_learning/apriori_algorithm.py) ## Maths * [Abs](maths/abs.py) From 0a4dbdf1141dc65ecb59962d79547af6a917f478 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 16 Oct 2023 01:05:33 +0200 Subject: [PATCH 06/10] Update matrix_chain_multiplication.py Add a non-dp solution with benchmarks. --- .../matrix_chain_multiplication.py | 105 +++++++++++++----- 1 file changed, 77 insertions(+), 28 deletions(-) diff --git a/dynamic_programming/matrix_chain_multiplication.py b/dynamic_programming/matrix_chain_multiplication.py index 173fe79c2ca7..8b0af28a3c75 100644 --- a/dynamic_programming/matrix_chain_multiplication.py +++ b/dynamic_programming/matrix_chain_multiplication.py @@ -1,21 +1,21 @@ """ -Find the minimum number of multiplications needed to multiply chain of matrices. +Find the minimum number of multiplications needed to multiply a chain of matrices. Reference: https://www.geeksforgeeks.org/matrix-chain-multiplication-dp-8/ The algorithm has interesting real-world applications. Example: 1. Image transformations in Computer Graphics as images are composed of matrix. -2. Solve complex polynomial equations in the field of algebra using least +2. Solve complex polynomial equations in the field of algebra using the least processing power. -3. Calculate overall impact of macroeconomic decisions as economic -equations involve number of variables. +3. Calculate the overall impact of macroeconomic decisions as economic equations +involve several variables. 4. Self-driving car navigation can be made more accurate as matrix multiplication -can accurately determine position and orientation of obstacles in short time. +can accurately determine the position and orientation of obstacles in short time. Python doctests can be run with the following command: python -m doctest -v matrix_chain_multiply.py -Given a sequence arr[] that represents chain of 2D matrices such that -the dimension of ith matrix is arr[i-1]*arr[i]. +Given a sequence arr[] that represents a chain of 2D matrices such that the +dimension of ith matrix is arr[i-1]*arr[i]. So suppose arr = [40, 20, 30, 10, 30] means we have 4 matrices of dimensions 40*20, 20*30, 30*10 and 10*30. @@ -28,7 +28,7 @@ Hints: 1. Number of multiplications (ie cost) to multiply 2 matrices of size m*p and p*n is m*p*n. -2. Cost of matrix multiplication is neither associative ie (M1*M2)*M3 != M1*(M2*M3) +2. The cost of matrix multiplication is associative. ie (M1*M2)*M3 != M1*(M2*M3) 3. Matrix multiplication is not commutative. So, M1*M2 does not mean M2*M1 can be done. 4. To determine the required order, we can try different combinations. So, this problem has overlapping sub-problems and can be solved using recursion. @@ -36,41 +36,44 @@ Example input : arr = [40, 20, 30, 10, 30] -output : 26000 +output: 26000 """ -import sys +from contextlib import contextmanager +from functools import cache +from sys import maxsize def matrix_chain_multiply(arr: list[int]) -> int: """ - Find the minimum number of multiplcations to multiply - chain of matrices. + Find the minimum number of multiplications required to multiply a chain of matrices. Args: - arr : The input array of integers. + arr: The input array of integers. Returns: - int: Minimum number of multiplications needed to multiply the chain - - Examples: - >>> matrix_chain_multiply([1,2,3,4,3]) - 30 - >>> matrix_chain_multiply([10]) - 0 - >>> matrix_chain_multiply([10, 20]) - 0 - >>> matrix_chain_multiply([19, 2, 19]) - 722 + The minimum number of multiplications needed to multiply the chain + + >>> matrix_chain_multiply([1, 2, 3, 4, 3]) + 30 + >>> matrix_chain_multiply([10]) + 0 + >>> matrix_chain_multiply([10, 20]) + 0 + >>> matrix_chain_multiply([19, 2, 19]) + 722 + >>> matrix_chain_multiply(list(range(1, 100))) + 323398 + >>> matrix_chain_multiply(list(range(1, 251))) + 5208248 """ # first edge case if len(arr) < 2: return 0 # initialising 2D dp matrix n = len(arr) - int_max = sys.maxsize - dp = [[int_max for j in range(n)] for i in range(n)] - # we want minimum cost of multiplication of matrices - # of dimension (i*k) and (k*j). This cost is arr[i-1]*arr[k]*arr[j]. + dp = [[maxsize for j in range(n)] for i in range(n)] + # We want the minimum cost of multiplication of matrices of + # dimension (i*k) and (k*j). This cost is arr[i-1]*arr[k]*arr[j]. for i in range(n - 1, 0, -1): for j in range(i, n): if i == j: @@ -84,7 +87,53 @@ def matrix_chain_multiply(arr: list[int]) -> int: return dp[1][n - 1] +def matrix_chain_order(dims: list[int]) -> int: + """ + Source: https://en.wikipedia.org/wiki/Matrix_chain_multiplication + The dynamic programming solution is faster than the cached recursive solution and + can handle larger inputs. + >>> matrix_chain_order([1, 2, 3, 4, 3]) + 30 + >>> matrix_chain_order([10]) + 0 + >>> matrix_chain_order([10, 20]) + 0 + >>> matrix_chain_order([19, 2, 19]) + 722 + >>> matrix_chain_order(list(range(1, 100))) + 323398 + >>> matrix_chain_order(list(range(1, 251))) # Max before RecursionError is raised. + 5208248 + """ + + @cache + def a(i, j): + return min( + (a(i, k) + dims[i] * dims[k] * dims[j] + a(k, j) for k in range(i + 1, j)), + default=0, + ) + + return a(0, len(dims) - 1) + + +@contextmanager +def elapsed_time(msg: str) -> None: + # print(f"Starting: {msg}") + from time import perf_counter_ns + start = perf_counter_ns() + yield + print(f"Finished: {msg} in {(perf_counter_ns() - start) / 10 ** 9} seconds.") + + if __name__ == "__main__": import doctest doctest.testmod() + with elapsed_time("matrix_chain_order"): + print(f"{matrix_chain_order(range(1, 251)) = }") + with elapsed_time("matrix_chain_multiply"): + print(f"{matrix_chain_multiply(range(1, 251)) = }") + with elapsed_time("matrix_chain_order"): + print(f"{matrix_chain_order(range(1, 251)) = }") + with elapsed_time("matrix_chain_multiply"): + print(f"{matrix_chain_multiply(range(1, 251)) = }") From 42c40d76e77a7999436ad28ce1a0f9dfcd529c31 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Oct 2023 23:06:08 +0000 Subject: [PATCH 07/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/matrix_chain_multiplication.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dynamic_programming/matrix_chain_multiplication.py b/dynamic_programming/matrix_chain_multiplication.py index 8b0af28a3c75..ca3fc46fe3b2 100644 --- a/dynamic_programming/matrix_chain_multiplication.py +++ b/dynamic_programming/matrix_chain_multiplication.py @@ -120,6 +120,7 @@ def a(i, j): def elapsed_time(msg: str) -> None: # print(f"Starting: {msg}") from time import perf_counter_ns + start = perf_counter_ns() yield print(f"Finished: {msg} in {(perf_counter_ns() - start) / 10 ** 9} seconds.") From f484544486ef750d8841bb19f44ac5875399b6d8 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 16 Oct 2023 01:14:35 +0200 Subject: [PATCH 08/10] Update matrix_chain_multiplication.py --- .../matrix_chain_multiplication.py | 77 ++++++++++--------- 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/dynamic_programming/matrix_chain_multiplication.py b/dynamic_programming/matrix_chain_multiplication.py index ca3fc46fe3b2..782f4b61224e 100644 --- a/dynamic_programming/matrix_chain_multiplication.py +++ b/dynamic_programming/matrix_chain_multiplication.py @@ -1,26 +1,26 @@ """ -Find the minimum number of multiplications needed to multiply a chain of matrices. +Find the minimum number of multiplications needed to multiply chain of matrices. Reference: https://www.geeksforgeeks.org/matrix-chain-multiplication-dp-8/ The algorithm has interesting real-world applications. Example: 1. Image transformations in Computer Graphics as images are composed of matrix. -2. Solve complex polynomial equations in the field of algebra using the least +2. Solve complex polynomial equations in the field of algebra using least processing power. -3. Calculate the overall impact of macroeconomic decisions as economic equations -involve several variables. +3. Calculate overall impact of macroeconomic decisions as economic +equations involve number of variables. 4. Self-driving car navigation can be made more accurate as matrix multiplication -can accurately determine the position and orientation of obstacles in short time. +can accurately determine position and orientation of obstacles in short time. Python doctests can be run with the following command: python -m doctest -v matrix_chain_multiply.py -Given a sequence arr[] that represents a chain of 2D matrices such that the -dimension of ith matrix is arr[i-1]*arr[i]. -So suppose arr = [40, 20, 30, 10, 30] means we have 4 matrices of -dimensions 40*20, 20*30, 30*10 and 10*30. +Given a sequence arr[] that represents chain of 2D matrices such that the dimension of +th ith matrix is arr[i-1]*arr[i]. +So suppose arr = [40, 20, 30, 10, 30] means we have 4 matrices of dimensions +40*20, 20*30, 30*10 and 10*30. -matrix_chain_multiply() returns an integer denoting -minimum number of multiplications to multiply the chain. +matrix_chain_multiply() returns an integer denoting minimum number of multiplications to +multiply the chain. We do not need to perform actual multiplication here. We only need to decide the order in which to perform the multiplication. @@ -28,16 +28,17 @@ Hints: 1. Number of multiplications (ie cost) to multiply 2 matrices of size m*p and p*n is m*p*n. -2. The cost of matrix multiplication is associative. ie (M1*M2)*M3 != M1*(M2*M3) +2. Cost of matrix multiplication is associative ie (M1*M2)*M3 != M1*(M2*M3) 3. Matrix multiplication is not commutative. So, M1*M2 does not mean M2*M1 can be done. 4. To determine the required order, we can try different combinations. So, this problem has overlapping sub-problems and can be solved using recursion. We use Dynamic Programming for optimal time complexity. -Example input : +Example input: arr = [40, 20, 30, 10, 30] output: 26000 """ +from collections.abc import Iterator from contextlib import contextmanager from functools import cache from sys import maxsize @@ -45,35 +46,35 @@ def matrix_chain_multiply(arr: list[int]) -> int: """ - Find the minimum number of multiplications required to multiply a chain of matrices. + Find the minimum number of multiplcations required to multiply the chain of matrices. Args: - arr: The input array of integers. + arr : The input array of integers. Returns: - The minimum number of multiplications needed to multiply the chain - - >>> matrix_chain_multiply([1, 2, 3, 4, 3]) - 30 - >>> matrix_chain_multiply([10]) - 0 - >>> matrix_chain_multiply([10, 20]) - 0 - >>> matrix_chain_multiply([19, 2, 19]) - 722 - >>> matrix_chain_multiply(list(range(1, 100))) - 323398 - >>> matrix_chain_multiply(list(range(1, 251))) - 5208248 + Minimum number of multiplications needed to multiply the chain + + Examples: + >>> matrix_chain_multiply([1, 2, 3, 4, 3]) + 30 + >>> matrix_chain_multiply([10]) + 0 + >>> matrix_chain_multiply([10, 20]) + 0 + >>> matrix_chain_multiply([19, 2, 19]) + 722 + >>> matrix_chain_multiply(list(range(1, 100))) + 323398 + >>> matrix_chain_multiply(list(range(1, 251))) + 5208248 """ - # first edge case if len(arr) < 2: return 0 # initialising 2D dp matrix n = len(arr) dp = [[maxsize for j in range(n)] for i in range(n)] - # We want the minimum cost of multiplication of matrices of - # dimension (i*k) and (k*j). This cost is arr[i-1]*arr[k]*arr[j]. + # we want minimum cost of multiplication of matrices + # of dimension (i*k) and (k*j). This cost is arr[i-1]*arr[k]*arr[j]. for i in range(n - 1, 0, -1): for j in range(i, n): if i == j: @@ -90,7 +91,7 @@ def matrix_chain_multiply(arr: list[int]) -> int: def matrix_chain_order(dims: list[int]) -> int: """ Source: https://en.wikipedia.org/wiki/Matrix_chain_multiplication - The dynamic programming solution is faster than the cached recursive solution and + The dynamic programming solution is faster than cached the recursive solution and can handle larger inputs. >>> matrix_chain_order([1, 2, 3, 4, 3]) 30 @@ -117,7 +118,7 @@ def a(i, j): @contextmanager -def elapsed_time(msg: str) -> None: +def elapsed_time(msg: str) -> Iterator: # print(f"Starting: {msg}") from time import perf_counter_ns @@ -131,10 +132,10 @@ def elapsed_time(msg: str) -> None: doctest.testmod() with elapsed_time("matrix_chain_order"): - print(f"{matrix_chain_order(range(1, 251)) = }") + print(f"{matrix_chain_order(list(range(1, 251))) = }") with elapsed_time("matrix_chain_multiply"): - print(f"{matrix_chain_multiply(range(1, 251)) = }") + print(f"{matrix_chain_multiply(list(range(1, 251))) = }") with elapsed_time("matrix_chain_order"): - print(f"{matrix_chain_order(range(1, 251)) = }") + print(f"{matrix_chain_order(list(range(1, 251))) = }") with elapsed_time("matrix_chain_multiply"): - print(f"{matrix_chain_multiply(range(1, 251)) = }") + print(f"{matrix_chain_multiply(list(range(1, 251))) = }") From b4ceef6e559561ee7b80f3db5ce8d9301861796c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 16 Oct 2023 01:20:17 +0200 Subject: [PATCH 09/10] Update matrix_chain_multiplication.py --- .../matrix_chain_multiplication.py | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/dynamic_programming/matrix_chain_multiplication.py b/dynamic_programming/matrix_chain_multiplication.py index 782f4b61224e..b750277aae37 100644 --- a/dynamic_programming/matrix_chain_multiplication.py +++ b/dynamic_programming/matrix_chain_multiplication.py @@ -4,18 +4,18 @@ The algorithm has interesting real-world applications. Example: 1. Image transformations in Computer Graphics as images are composed of matrix. -2. Solve complex polynomial equations in the field of algebra using least -processing power. -3. Calculate overall impact of macroeconomic decisions as economic -equations involve number of variables. -4. Self-driving car navigation can be made more accurate as matrix multiplication -can accurately determine position and orientation of obstacles in short time. +2. Solve complex polynomial equations in the field of algebra using least processing + power. +3. Calculate overall impact of macroeconomic decisions as economic equations involve a + number of variables. +4. Self-driving car navigation can be made more accurate as matrix multiplication can + accurately determine position and orientation of obstacles in short time. Python doctests can be run with the following command: python -m doctest -v matrix_chain_multiply.py Given a sequence arr[] that represents chain of 2D matrices such that the dimension of -th ith matrix is arr[i-1]*arr[i]. +the ith matrix is arr[i-1]*arr[i]. So suppose arr = [40, 20, 30, 10, 30] means we have 4 matrices of dimensions 40*20, 20*30, 30*10 and 10*30. @@ -46,10 +46,10 @@ def matrix_chain_multiply(arr: list[int]) -> int: """ - Find the minimum number of multiplcations required to multiply the chain of matrices. + Find the minimum number of multiplcations required to multiply the chain of matrices Args: - arr : The input array of integers. + arr: The input array of integers. Returns: Minimum number of multiplications needed to multiply the chain @@ -65,8 +65,8 @@ def matrix_chain_multiply(arr: list[int]) -> int: 722 >>> matrix_chain_multiply(list(range(1, 100))) 323398 - >>> matrix_chain_multiply(list(range(1, 251))) - 5208248 + >>> matrix_chain_multiply(list(range(1, 200))) + 2626798 """ if len(arr) < 2: return 0 @@ -103,8 +103,8 @@ def matrix_chain_order(dims: list[int]) -> int: 722 >>> matrix_chain_order(list(range(1, 100))) 323398 - >>> matrix_chain_order(list(range(1, 251))) # Max before RecursionError is raised. - 5208248 + >>> matrix_chain_order(list(range(1, 200))) # Max before RecursionError is raised. + 2626798 """ @cache From dd779bf7f31e82eb4fbfc776a47cd13f3bb8ce11 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 16 Oct 2023 01:27:31 +0200 Subject: [PATCH 10/10] Update matrix_chain_multiplication.py --- dynamic_programming/matrix_chain_multiplication.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/dynamic_programming/matrix_chain_multiplication.py b/dynamic_programming/matrix_chain_multiplication.py index b750277aae37..084254a61f6c 100644 --- a/dynamic_programming/matrix_chain_multiplication.py +++ b/dynamic_programming/matrix_chain_multiplication.py @@ -65,8 +65,9 @@ def matrix_chain_multiply(arr: list[int]) -> int: 722 >>> matrix_chain_multiply(list(range(1, 100))) 323398 - >>> matrix_chain_multiply(list(range(1, 200))) - 2626798 + + # >>> matrix_chain_multiply(list(range(1, 251))) + # 2626798 """ if len(arr) < 2: return 0 @@ -103,12 +104,13 @@ def matrix_chain_order(dims: list[int]) -> int: 722 >>> matrix_chain_order(list(range(1, 100))) 323398 - >>> matrix_chain_order(list(range(1, 200))) # Max before RecursionError is raised. - 2626798 + + # >>> matrix_chain_order(list(range(1, 251))) # Max before RecursionError is raised + # 2626798 """ @cache - def a(i, j): + def a(i: int, j: int) -> int: return min( (a(i, k) + dims[i] * dims[k] * dims[j] + a(k, j) for k in range(i + 1, j)), default=0,