Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Matrix Chain Multiplication [JAVA] #843

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
| [Linear Search](https://en.wikipedia.org/wiki/Linear_search) | [:white_check_mark:](linear_search/linear_search.c) | | [:white_check_mark:](linear_search/LinearSearch.java) | [:white_check_mark:](linear_search/linear_search.py) | [:white_check_mark:](linear_search/linear-search.go) | [:white_check_mark:](linear_search/linearSearch.js) | [:white_check_mark:](linear_search/LinearSearch.cs) |
| [Longest Common Subsequence](http://www.geeksforgeeks.org/dynamic-programming-set-4-longest-common-subsequence) | [:white_check_mark:](longest_common_subsequence/longestCommonSubsequence.c) | | [:white_check_mark:](longest_common_subsequence/LongestCommonSubsequence.java) | [:white_check_mark:](longest_common_subsequence/longest_common_subsequence.py) | [:white_check_mark:](longest_common_subsequence/longestCommonSubsequence.go) | [:white_check_mark:](longest_common_subsequence/longestCommonSubsequence.js) | |
| [Longest Palindromic Substring](http://www.geeksforgeeks.org/longest-palindrome-substring-set-1/) | | [:white_check_mark:](longest_palindromic_substring/longest_palindromic_substring.cpp) | [:white_check_mark:](longest_palindromic_substring/LongestPalindromicSubstring.java) | [:white_check_mark:](longest_palindromic_substring/longest_palindromic_substring.py) | [:white_check_mark:](longest_palindromic_substring/longest_palindromic_substring.go) | [:white_check_mark:](longest_palindromic_substring/longestPalindromicSubstring.js) | |
| [Matrix Chain Multiplication](https://www.geeksforgeeks.org/matrix-chain-multiplication-dp-8/) | | | [:white_check_mark:](matrix_chain_multiplication/MatrixChainMultiplication.java) | | | | |
| [Merge Sort](https://www.khanacademy.org/computing/computer-science/algorithms/merge-sort/a/overview-of-merge-sort) | [:white_check_mark:](merge_sort/merge_sort.c) | | [:white_check_mark:](merge_sort/MergeSort.java) | [:white_check_mark:](merge_sort/merge_sort.py) | [:white_check_mark:](merge_sort/merge_sort.go) | [:white_check_mark:](merge_sort/mergeSort.js) | [:white_check_mark:](merge_sort/MergeSort.cs) |
| [Modular Exponential](http://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/) | [:white_check_mark:](modular_exponential/modular_exponential.c) | | [:white_check_mark:](modular_exponential/ModularExponential.java) | [:white_check_mark:](modular_exponential/modular_exponential.py) | [:white_check_mark:](modular_exponential/modular_exponential.go) | [:white_check_mark:](modular_exponential/modularExponential.js) | |
| [Nth Fibonacci Number](https://en.wikipedia.org/wiki/Fibonacci_number) |[:white_check_mark:](fibonacci_number/FibonacciNumber.c) | | [:white_check_mark:](fibonacci_number/FibonacciNumber.java) | | |[:white_check_mark:](fibonacci_number/FibonacciNumber.js) | |
Expand Down
93 changes: 93 additions & 0 deletions matrix_chain_multiplication/MatrixChainMultiplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
public class MatrixChainMultiplication {

// Recursive Solution: Exponential Time Complexity

public static int recursive(int a[], int i, int j)
{
// Base Case
if(i>=j)
return 0;

int min = Integer.MAX_VALUE;
for(int k=i;k<=j-1;k++)
{
// recursive call to fetch the cost
int tempans = recursive(a, i, k) + recursive(a, k+1, j) + a[i-1]*a[k]*a[j];
if(tempans<min)
min=tempans;
}
return min;
}




// Memoized Recursive Solution (Bottom-Up): O(n^3) time complexity and O(n^2) auxilliary space

public static int memo[][] = new int [1001][1001]; // 2-D memo array to store subproblems
public static int memoized(int a[], int i, int j)
{
// Base Case
if(i>=j)
return 0;

// Checking if solution to subproblem exists
if(memo[i][j]!=0)
return memo[i][j];


memo[i][j] = Integer.MAX_VALUE;
for(int k=i;k<=j-1;k++)
{
// memoization of the optimal cost for subproblems
memo[i][j] = Math.min(memo[i][j], memoized(a, i, k)+memoized(a, k+1, j)+a[i-1]*a[k]*a[j]);
}
return memo[i][j];
}




// Solution by Tabulation (Top-down): O(n^3) time complexity and O(n^2) auxilliary space

public static int tabulated(int a[], int n)
{
int dp[][] = new int[n][n]; // dp of size nxn to store solutions of subproblems

// L is the length of the Matrix chain (Minimum L = 2)
for(int L = 2; L < n; L++)
{
for(int i = 1; i < n-L+1; i++)
{
int j = i+L-1;
if(j==n)
continue;

dp[i][j] = Integer.MAX_VALUE;

for(int k = i; k <= j-1; k++)
{
// dp[i][j] = Minimum number of scalar multiplications needed to compute the sub-matrix
dp[i][j] = Math.min(dp[i][j], dp[i][k]+dp[k+1][j]+a[i-1]*a[k]*a[j]);
}
}
}
return dp[1][n-1];
}




// Driver Function

public static void main(String args[]) {

int a[] = {2,3,1,4,3}; // Array containing n elements comprising of dimensions of n-1 matrices
int n = a.length;
// In the above array, Matrix M1:2x3, M2:3x1, M3:1x4, M4:4x3

System.out.println("Answer by Recursive Approach: "+recursive(a,1,n-1));
System.out.println("Answer by Memoized Recursive Solution (bottom-up): "+memoized(a,1,n-1));
System.out.println("Answer by Tabulation (top-down): "+tabulated(a, n));
}
}