diff --git a/DynamicProgramming/MatrixChainMultiplication/MCM_DPSoln.cpp b/DynamicProgramming/MatrixChainMultiplication/MCM_DPSoln.cpp new file mode 100644 index 0000000..935c7ae --- /dev/null +++ b/DynamicProgramming/MatrixChainMultiplication/MCM_DPSoln.cpp @@ -0,0 +1,57 @@ +/* +Optimised Solution of Matrix Chain Multiplication Problem using the Bottom Up Dynamic Programming. +Sample Test Case : +3 +3 +10 20 30 +5 +40 20 30 10 30 +4 +10 30 5 60 +case #1 : 6000 +case #2 : 26000 +case #3 : 4500 +*/ +#include "../headers.hpp" + +int matrixChainMultiplicationCost(int *arr, int i, int j, int *dp[]) +{ + if (i >= j) + return dp[i][j] = 0; + if (dp[i][j] != -1) + return dp[i][j]; + + int ans = INT_MAX; + for (int k = i; k <= j - 1; k++) + { + int temp_ans = matrixChainMultiplicationCost(arr, i, k, dp) + matrixChainMultiplicationCost(arr, k + 1, j, dp) + (arr[i - 1] * arr[k] * arr[j]); + if (ans > temp_ans) + ans = temp_ans; + } + return dp[i][j] = ans; +} +int main() +{ + vi output; + tests(t) + { + int n; + cin >> n; + int *arr = new int[n]; + loop(i, n) cin >> arr[i]; + int **dp = new int *[n + 1]; + loop(i, n + 1) + { + dp[i] = new int[n + 1]; + } + // memset(dp, -1, sizeof(dp)); did not work on system compiler, hence manually assigned -1 to dp[][]. + loop(i, n + 1) + { + loop(j, n + 1) + dp[i][j] = -1; + } + output.pb(matrixChainMultiplicationCost(arr, 1, n - 1, dp)); + } + loop(i, output.size()) cout << "case #" << i + 1 << " : " << output[i] << endl; + return 0; +} \ No newline at end of file diff --git a/DynamicProgramming/MatrixChainMultiplication/MCM_recursive.cpp b/DynamicProgramming/MatrixChainMultiplication/MCM_recursive.cpp new file mode 100644 index 0000000..fe22f13 --- /dev/null +++ b/DynamicProgramming/MatrixChainMultiplication/MCM_recursive.cpp @@ -0,0 +1,45 @@ +/* Problem Statement : Matrix Chain Multiplication +Given a sequence of matrices, find the most efficient way to multiply these matrices together. The problem is not actually to perform the multiplications, but merely to decide in which order to perform the multiplications. +We have many options to multiply a chain of matrices because matrix multiplication is associative. In other words, no matter how we parenthesize the product, the result will be the same. +Sample Test Case : +3 +3 +10 20 30 +5 +40 20 30 10 30 +4 +10 30 5 60 +case #1 : 6000 +case #2 : 26000 +case #3 : 4500 +*/ +#include "../headers.hpp" + +int matrixChainMultiplicationCost(int *arr, int i, int j) +{ + if (i >= j) + return 0; + + int ans = INT_MAX; + for (int k = i; k <= j - 1; k++) + { + int temp_ans = matrixChainMultiplicationCost(arr, i, k) + matrixChainMultiplicationCost(arr, k + 1, j) + (arr[i - 1] * arr[k] * arr[j]); + if (ans > temp_ans) + ans = temp_ans; + } + return ans; +} +int main() +{ + vi output; + tests(t) + { + int n; + cin >> n; + int *arr = new int[n]; + loop(i, n) cin >> arr[i]; + output.pb(matrixChainMultiplicationCost(arr, 1, n - 1)); + } + loop(i, output.size()) cout << "case #" << i + 1 << " : " << output[i] << endl; + return 0; +} \ No newline at end of file diff --git a/DynamicProgramming/longestPalindromicSubsequence.cpp b/DynamicProgramming/longestPalindromicSubsequence.cpp index f1084f0..3da37dc 100644 --- a/DynamicProgramming/longestPalindromicSubsequence.cpp +++ b/DynamicProgramming/longestPalindromicSubsequence.cpp @@ -56,4 +56,5 @@ int main() output.pb(LPS(s, n)); } loop(i, output.size()) cout << "case #" << i + 1 << " : " << output[i] << endl; + return 0; } \ No newline at end of file diff --git a/DynamicProgramming/longestRepeatingSubsequence.cpp b/DynamicProgramming/longestRepeatingSubsequence.cpp new file mode 100644 index 0000000..fd622aa --- /dev/null +++ b/DynamicProgramming/longestRepeatingSubsequence.cpp @@ -0,0 +1,49 @@ +#include "headers.hpp" +/* Problem Statement: Given a string, print the longest repeating subsequence such that the two subsequences don’t have the same string character at the same position, i.e., any i’th character in the two subsequences shouldn’t have the same index in the original string. +The two subsequences are 'a'(first) and 'a' (second). Note that 'b' cannot be considered as part of subsequence as it would be at the same +index in both. + +Sample Test Case : +3 +AABEBCDD +ABCDDBC +eloeggogll +case #1 : 3 +case #2 : 2 +case #3 : 4 +*/ +int longestRepeatingSubsequence(string s1, int n) +{ + string s2 = s1; + + int dp[n + 1][n + 1]; + for (int i = 0; i <= n; i++) + dp[0][i] = dp[i][0] = 0; + + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + if (s1[i - 1] == s2[j - 1] and (i != j)) + dp[i][j] = 1 + dp[i - 1][j - 1]; + else + { + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + } + } + } + return dp[n][n]; +} +int main() +{ + vi output; + tests(t) + { + string s; + cin >> s; + int n = s.size(); + output.pb(longestRepeatingSubsequence(s, n)); + } + loop(i, output.size()) cout << "case #" << i + 1 << " : " << output[i] << endl; + return 0; +} \ No newline at end of file