From 060a2645407b2c92a8a9cb407804d6e07e33763b Mon Sep 17 00:00:00 2001 From: ojhankit Date: Mon, 7 Oct 2024 01:49:57 +0530 Subject: [PATCH] added tabulation method in nth_fibo problem and memoization method in lcs and matrix_mul --- .../Dynammic_Programming/lcs.py | 30 ++++++++++++++++++ .../matrix_multiplication.py | 31 +++++++++++++++++++ .../Dynammic_Programming/nth_fibonacci.py | 20 +++++++++++- 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Dynammic_Programming/lcs.py b/Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Dynammic_Programming/lcs.py index b4570809c9..7c17e9684d 100644 --- a/Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Dynammic_Programming/lcs.py +++ b/Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Dynammic_Programming/lcs.py @@ -18,6 +18,36 @@ def get_lcs_length(S1, S2): # The last cell contains the length of the longest common subsequence return dp[m][n] +""" memoization method """ +def lcs_memo(S1:str ,S2:str ,i:int ,j:int ,dp:list[list[int]]): + """ + Compute the length of the longest common subsequence (LCS) between two strings using memoization. + + Parameters: + S1 (str): The first string. + S2 (str): The second string. + i (int): Current index of string S1 (starts from len(S1)-1). + j (int): Current index of string S2 (starts from len(S2)-1). + dp (list[list[int]]): A memoization table initialized with -1 to store intermediate results. + + Returns: + int: Length of the longest common subsequence between S1 and S2 + """ + #base case : if either string is ended return 0 + if i<0 or j<0: + return 0; + + #if result of subproblem is already calculated then no need to calculate again + if dp[i][j] != -1: + return dp[i][j] + + if S1[i] == S2[j]: + dp[i][j] = 1 + lcs_memo(S1,S2,i-1,j-1,dp) + else: + dp[i][j] = max(lcs_memo(S1,S2,i-1,j,dp),lcs_memo(S1,S2,i,j-1,dp)) + + return dp[i][j] + if __name__ == "__main__": # Take input strings from the user S1 = input("Enter the first string: ") diff --git a/Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Dynammic_Programming/matrix_multiplication.py b/Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Dynammic_Programming/matrix_multiplication.py index 91e278bc39..7a59e8b245 100644 --- a/Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Dynammic_Programming/matrix_multiplication.py +++ b/Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Dynammic_Programming/matrix_multiplication.py @@ -22,6 +22,37 @@ def minMult(arr): # Return the minimum cost to multiply the entire chain of matrices return dp[0][n - 1] +""" memoization method """ +def minMult_memo(arr:list ,i:int ,j:int ,dp:list[list[int]]): + """ + Compute the minimum number of scalar multiplications required to multiply + a chain of matrices using memoization (Matrix Chain Multiplication problem). + + Parameters: + arr (list): List of integers where the i-th matrix has dimensions arr[i-1] x arr[i]. + i (int): Starting index of the matrix chain. + j (int): Ending index of the matrix chain. + dp (list[list[int]]): A memoization table initialized with -1 to store intermediate results. + + Returns: + int: Minimum number of scalar multiplications needed to multiply matrices from index i to j. + if i==j: + return 0 + """ + if dp[i][j] != -1: + return dp[i][j] + + ans = sys.maxsize + + for k in range(i,j): + + res = minMult_memo(arr ,i ,k ,dp) + minMult_memo(arr ,k+1 ,j ,dp) + arr[i-1]* arr[j] * arr[k] + + ans = min(ans,res) + + dp[i][j] = ans + return dp[i][j] + if __name__ == "__main__": # Input the matrix dimensions as space-separated integers arr = list(map(int, input("Enter the dimensions of matrices separated by spaces: ").split())) diff --git a/Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Dynammic_Programming/nth_fibonacci.py b/Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Dynammic_Programming/nth_fibonacci.py index d1523ea767..1b589203df 100644 --- a/Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Dynammic_Programming/nth_fibonacci.py +++ b/Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Dynammic_Programming/nth_fibonacci.py @@ -11,11 +11,29 @@ def nth_fibonacci(n, memo={}): memo[n] = nth_fibonacci(n - 1, memo) + nth_fibonacci(n - 2, memo) return memo[n] +""" tabulation method for solving nth_fibonnaci """ +def nth_fibonacci_tab(n:int) -> int: + """ + Calculate n_th fibonnaci number in O(n) + params: + n : term value + + returns: + int: nth term of fibonnaci sequence + """ + dp = [0] * (n+1) + dp[1] = 1 + for i in range(2,n+1): + + dp[i] = dp[i-1] + dp[i-2] + + return dp[n] + # Get input from the user for the Fibonacci number position n = int(input("Enter the position of the Fibonacci number to find: ")) # Calculate the nth Fibonacci number -result = nth_fibonacci(n) +result = nth_fibonacci_tab(n) # Print the result print(f"The {n}th Fibonacci number is: {result}")