From 18c497e5f0ed76c59976dd86148527f374b9c158 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 15 Oct 2023 17:31:00 +0000 Subject: [PATCH 1/3] updating DIRECTORY.md --- DIRECTORY.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6213f26b6d93..101bfd0423df 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -265,6 +265,7 @@ * [Postfix Evaluation](data_structures/stacks/postfix_evaluation.py) * [Prefix Evaluation](data_structures/stacks/prefix_evaluation.py) * [Stack](data_structures/stacks/stack.py) + * [Stack Using Two Queues](data_structures/stacks/stack_using_two_queues.py) * [Stack With Doubly Linked List](data_structures/stacks/stack_with_doubly_linked_list.py) * [Stack With Singly Linked List](data_structures/stacks/stack_with_singly_linked_list.py) * [Stock Span Problem](data_structures/stacks/stock_span_problem.py) @@ -791,7 +792,7 @@ ## Physics * [Altitude Pressure](physics/altitude_pressure.py) - * [Archimedes Principle](physics/archimedes_principle.py) + * [Archimedes Principle Of Buoyant Force](physics/archimedes_principle_of_buoyant_force.py) * [Basic Orbital Capture](physics/basic_orbital_capture.py) * [Casimir Effect](physics/casimir_effect.py) * [Centripetal Force](physics/centripetal_force.py) From 44872114b272d612103045ddebe47ad55c426121 Mon Sep 17 00:00:00 2001 From: Pooja Sharma Date: Mon, 16 Oct 2023 00:34:49 +0530 Subject: [PATCH 2/3] new algorithm matrix_chain_multiplication --- .../matrix_chain_multiplication.py | 78 +++++++++++++++++++ 1 file changed, 78 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..23b67f48067d --- /dev/null +++ b/dynamic_programming/matrix_chain_multiplication.py @@ -0,0 +1,78 @@ +""" +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 +dimesions 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 +""" + + +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) + dp = [[float("inf") 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 89064b14ddea1b4036b2745edc1b59ee14985bea Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 15 Oct 2023 19:07:06 +0000 Subject: [PATCH 3/3] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 101bfd0423df..8a662d2a0f46 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -340,6 +340,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)