Skip to content

Commit c771cf8

Browse files
committed
matrix chain bottom up failed
1 parent 5ed70fa commit c771cf8

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

src/dymanicProgramming/MatrixChainMultiplication.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class MatrixChainMultiplication {
77

88
private static Integer[][] solMat;
99

10-
private static int matrixChainOrder(int[] ints, int start, int end) {
10+
private static int matrixChainOrderTopDown(int[] ints, int start, int end) {
1111
if (solMat[start][end] != null) {
1212
return solMat[start][end];
1313
}
@@ -17,9 +17,9 @@ private static int matrixChainOrder(int[] ints, int start, int end) {
1717
} else {
1818
int min = Integer.MAX_VALUE;
1919
for (int temp = start; temp < end; temp++) {
20-
int prev = matrixChainOrder(ints, start, temp);
21-
int next = matrixChainOrder(ints, temp + 1, end);
22-
int current = ints[start-1] * ints[temp] * ints[end];
20+
int prev = matrixChainOrderTopDown(ints, start, temp);
21+
int next = matrixChainOrderTopDown(ints, temp + 1, end);
22+
int current = ints[start - 1] * ints[temp] * ints[end];
2323
int count = prev + next + current;
2424
min = Math.min(min, count);
2525
}
@@ -29,9 +29,35 @@ private static int matrixChainOrder(int[] ints, int start, int end) {
2929
return sol;
3030
}
3131

32+
33+
private static int bottomUp(Integer[][] ints, int[] arr) {
34+
for (int k = 1; k < ints.length; k++) {
35+
ints[k][k] = 0;
36+
}
37+
for (int end = 2; end < ints[0].length; end++) {
38+
int startTemp = 1;
39+
for (int endTemp = end; endTemp < ints[0].length; endTemp++) {
40+
int min = Integer.MAX_VALUE;
41+
for (int k = startTemp; k < endTemp; k++) {
42+
int current = arr[startTemp - 1] * arr[k] * arr[end];
43+
int count = ints[startTemp][k] + ints[k + 1][endTemp] + current;
44+
min = Math.min(min, count);
45+
}
46+
ints[startTemp][end] = min;
47+
startTemp++;
48+
}
49+
}
50+
return ints[1][arr.length - 1];
51+
}
52+
3253
public static void main(String[] strings) {
33-
int arr[] = {1, 2, 3, 4, 5};
54+
int arr[] = {1, 2, 3, 4};
3455
solMat = new Integer[arr.length][arr.length];
35-
System.out.println("Minimum number of multiplications is " + matrixChainOrder(arr, 1, arr.length - 1));
56+
/*
57+
System.out.println("Minimum number of multiplications is " + matrixChainOrderTopDown(arr, 1, arr.length - 1));
58+
*/
59+
60+
System.out.println("Minimum number of multiplications is " + bottomUp(solMat,arr));
61+
3662
}
3763
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package dymanicProgramming.prereq;
2+
3+
/**
4+
* Created by dheeraj on 7/18/2016.
5+
* needed for filling matrix chain
6+
*/
7+
public class DiagonalTraversalOfMatrix {
8+
9+
private static void printDiagonal(int[][] ints) {
10+
int row = 1;
11+
int col = 2;
12+
while (col < ints[0].length) {
13+
int rowTemp = row;
14+
int colTemp = col;
15+
while (colTemp < ints[0].length) {
16+
System.out.print(ints[rowTemp][colTemp] + " ");
17+
rowTemp++;
18+
colTemp++;
19+
}
20+
row++;
21+
col = row + 1;
22+
}
23+
}
24+
25+
private static void printDiagonalOpt(int[][] ints) {
26+
for (int col =2;col < ints[0].length;col++) {
27+
int rowTemp = 1;
28+
for (int colTemp = col;colTemp < ints[0].length;colTemp++) {
29+
System.out.print(ints[rowTemp][colTemp] + " ");
30+
rowTemp++;
31+
}
32+
}
33+
}
34+
35+
public static void main(String[] strings) {
36+
int[][] mat = new int[][]{
37+
{1, 2, 3, 4},
38+
{5, 6, 7, 8},
39+
{9, 10, 11, 12},
40+
{13, 14, 15, 16}};
41+
printDiagonal(mat);
42+
}
43+
}

0 commit comments

Comments
 (0)