-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b79a92f
commit 18ad6c0
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
DynamicProgramming/MatrixChainMultiplication/MCM_DPSoln.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
45 changes: 45 additions & 0 deletions
45
DynamicProgramming/MatrixChainMultiplication/MCM_recursive.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |