diff --git a/README.md b/README.md index 6671c9d6..be518119 100644 --- a/README.md +++ b/README.md @@ -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) | | diff --git a/matrix_chain_multiplication/MatrixChainMultiplication.java b/matrix_chain_multiplication/MatrixChainMultiplication.java new file mode 100644 index 00000000..3e5b12fd --- /dev/null +++ b/matrix_chain_multiplication/MatrixChainMultiplication.java @@ -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=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)); + } +}