Skip to content

Commit

Permalink
Merge pull request #206 from ojhankit/dp
Browse files Browse the repository at this point in the history
added tabulation method in nth_fibo problem and memoization method in…
  • Loading branch information
UTSAVS26 authored Oct 7, 2024
2 parents 75d9c24 + 060a264 commit 14f579c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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: ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

0 comments on commit 14f579c

Please sign in to comment.