|
| 1 | +/* |
| 2 | +https://www.geeksforgeeks.org/matrix-chain-multiplication-dp-8/ |
| 3 | +
|
| 4 | +NOTE - Similar to Longest Plaindromic Subsequence |
| 5 | +
|
| 6 | +
|
| 7 | +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. |
| 8 | +
|
| 9 | +example, suppose A is a 10 × 30 matrix, B is a 30 × 5 matrix, and C is a 5 × 60 matrix. Then, |
| 10 | +
|
| 11 | + (AB)C = (10×30×5) + (10×5×60) = 1500 + 3000 = 4500 operations |
| 12 | + A(BC) = (30×5×60) + (10×30×60) = 9000 + 18000 = 27000 operations. |
| 13 | + |
| 14 | +
|
| 15 | +Given an array p[] which represents the chain of matrices such that the ith matrix Ai is of dimension p[i-1] x p[i]. We need to write a function MatrixChainOrder() that should return the minimum number of multiplications needed to multiply the chain. |
| 16 | +
|
| 17 | + Input: p[] = {40, 20, 30, 10, 30} |
| 18 | + Output: 26000 |
| 19 | + There are 4 matrices of dimensions 40x20, 20x30, 30x10 and 10x30. |
| 20 | + Let the input 4 matrices be A, B, C and D. The minimum number of |
| 21 | + multiplications are obtained by putting parenthesis in following way |
| 22 | + (A(BC))D --> 20*30*10 + 40*20*10 + 40*10*30 |
| 23 | +
|
| 24 | + Input: p[] = {10, 20, 30, 40, 30} |
| 25 | + Output: 30000 |
| 26 | + There are 4 matrices of dimensions 10x20, 20x30, 30x40 and 40x30. |
| 27 | + Let the input 4 matrices be A, B, C and D. The minimum number of |
| 28 | + multiplications are obtained by putting parenthesis in following way |
| 29 | + ((AB)C)D --> 10*20*30 + 10*30*40 + 10*40*30 |
| 30 | +
|
| 31 | + Input: p[] = {10, 20, 30} |
| 32 | + Output: 6000 |
| 33 | + There are only two matrices of dimensions 10x20 and 20x30. So there |
| 34 | + is only one way to multiply the matrices, cost of which is 10*20*30 |
| 35 | +
|
| 36 | +
|
| 37 | +OPTIMAL SUBSTRUCTURE- |
| 38 | +1. In a chain of matrices of size n, we can place the first set of parenthesis in n-1 ways. |
| 39 | +2. let the chain be ABCD, then there are 3 ways to place first set of parenthesis outer side: (A)(BCD), (AB)(CD) and (ABC)(D). |
| 40 | +3. When we place a set of parenthesis, we divide the problem into subproblems of smaller size. |
| 41 | +4. Minimum number of multiplication needed to multiply a chain of size n = Minimum of all n-1 placements (these placements create subproblems of smaller size) |
| 42 | +
|
| 43 | +OVERLAPPING SUBPROBLEMS |
| 44 | +*/ |
| 45 | + |
| 46 | +//RECURSIVE CODE |
| 47 | +public int matrixChain(int dims[], int start, int end){ |
| 48 | + |
| 49 | + //BASE CONDITION - We have only one matrix, which can not be multiplied ==> cost = 0 |
| 50 | + if(start == end) return 0; |
| 51 | + |
| 52 | + int minCost = Integer.MAX_VALUE; |
| 53 | + |
| 54 | + /* |
| 55 | + The current window under consideration is [start...end] |
| 56 | + k partitions the range [start...end] into 2 ==> [start....k] and [k+1...j] |
| 57 | + These two partitions are the subproblems. |
| 58 | + Since we need to find the partition with minimum cost, we try all values of k |
| 59 | + ie. All possible partitions possible |
| 60 | + */ |
| 61 | + for(int k = start ; k < end ; k++){ |
| 62 | + int cost = matrixChain(dims, start, k) + |
| 63 | + matrixChain(dims, k+1, end) + |
| 64 | + (dims[start-1] * dims[k] * dims[end]); |
| 65 | + /* |
| 66 | + Cost of multiplication of two matrices - number of arithemetic computations involved. |
| 67 | + */ |
| 68 | + |
| 69 | + if(cost < minCost) minCost = cost; |
| 70 | + } |
| 71 | + |
| 72 | + return minCost; |
| 73 | +} |
| 74 | + |
| 75 | +//DYNAMIC PROGRAMMING - BOTTOM UP |
| 76 | +/* |
| 77 | +1. In the recursive code, (start == end) was our base condition and building from that we considered matrices in continuous window of size = 2, 3, ... n-1. |
| 78 | +2. To consider all continuous matrices of a given window size, the window is slid by one. |
| 79 | +3. Window of size=2 is a subproblem of window of size=3 and so on... |
| 80 | +3. A matrix is used to store solutions of the subproblems. |
| 81 | +4. mat[i][j] indicates the minimum cost required to multiply matrices [i...j] |
| 82 | +5. Only the values above the principal diaognal will be computed. The rest will remain unused. |
| 83 | +6. Final solution will be mat[1][n-1] |
| 84 | +*/ |
| 85 | + |
| 86 | +public int matrixChain(int dims[]){ |
| 87 | + int n = dims.length; |
| 88 | + |
| 89 | + int cost[][] = new int[n][n]; |
| 90 | + |
| 91 | + //we will use cost[][] from (1,1) because the number of input matrices is (n-1) |
| 92 | + |
| 93 | + //cost of multiplying 1 matrix is 0 |
| 94 | + //This is equivalent to the leaf nodes in the recursion tree |
| 95 | + for(int i=1;i<n;i++) cost[i][i] = 0; |
| 96 | + |
| 97 | + /* |
| 98 | + After the leaf nodes of the recursion tree are computed, the control moves to the next level above the leaf nodes. |
| 99 | + In that level the window size=2 and it keeps increasing by 1 as we move up each level in the recursion tree. |
| 100 | + So, gap start at 2 and will increase up to (n-1) |
| 101 | + */ |
| 102 | + for(int gap = 2 ; gap < n ; gap++){ |
| 103 | + //fixing the start and end of the current gap |
| 104 | + for(int start = 1 ; start < n-gap+1 ; start++){ |
| 105 | + int end = start+gap-1; |
| 106 | + |
| 107 | + cost[start][end] = Integer.MAX_VALUE; |
| 108 | + |
| 109 | + /* |
| 110 | + The current window under consideration is [start...end] |
| 111 | + k partitions the range [start...end] into 2 ==> [start....k] and [k+1...j] |
| 112 | + These two partitions are the subproblems. |
| 113 | + Since we need to find the partition with minimum cost, we try all values of k |
| 114 | + ie. All possible partitions possible |
| 115 | + */ |
| 116 | + for(int k=start ; k < end ; k++){ |
| 117 | + |
| 118 | + int currCost = cost[start][k] + cost[k+1][end] + (dims[start-1] * dims[k] * dims[end]); |
| 119 | + |
| 120 | + //since all possible partitions will be explored we must keep track of the minimum cost |
| 121 | + if(cost[start][end] > currCost) cost[start][end] = currCost; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return cost[1][n-1]; |
| 127 | + |
| 128 | +} |
| 129 | + |
| 130 | + |
0 commit comments