2
2
3
3
import java .util .ArrayList ;
4
4
import java .util .Arrays ;
5
- import java .util .Scanner ;
6
5
6
+ /**
7
+ * The MatrixChainMultiplication class provides functionality to compute the
8
+ * optimal way to multiply a sequence of matrices. The optimal multiplication
9
+ * order is determined using dynamic programming, which minimizes the total
10
+ * number of scalar multiplications required.
11
+ */
7
12
public final class MatrixChainMultiplication {
8
13
private MatrixChainMultiplication () {
9
14
}
10
15
11
- private static final Scanner SCANNER = new Scanner (System .in );
12
- private static final ArrayList <Matrix > MATRICES = new ArrayList <>();
13
- private static int size ;
16
+ // Matrices to store minimum multiplication costs and split points
14
17
private static int [][] m ;
15
18
private static int [][] s ;
16
19
private static int [] p ;
17
20
18
- public static void main (String [] args ) {
19
- int count = 1 ;
20
- while (true ) {
21
- String [] mSize = input ("input size of matrix A(" + count + ") ( ex. 10 20 ) : " );
22
- int col = Integer .parseInt (mSize [0 ]);
23
- if (col == 0 ) {
24
- break ;
25
- }
26
- int row = Integer .parseInt (mSize [1 ]);
27
-
28
- Matrix matrix = new Matrix (count , col , row );
29
- MATRICES .add (matrix );
30
- count ++;
31
- }
32
- for (Matrix m : MATRICES ) {
33
- System .out .format ("A(%d) = %2d x %2d%n" , m .count (), m .col (), m .row ());
34
- }
35
-
36
- size = MATRICES .size ();
21
+ /**
22
+ * Calculates the optimal order for multiplying a given list of matrices.
23
+ *
24
+ * @param matrices an ArrayList of Matrix objects representing the matrices
25
+ * to be multiplied.
26
+ * @return a Result object containing the matrices of minimum costs and
27
+ * optimal splits.
28
+ */
29
+ public static Result calculateMatrixChainOrder (ArrayList <Matrix > matrices ) {
30
+ int size = matrices .size ();
37
31
m = new int [size + 1 ][size + 1 ];
38
32
s = new int [size + 1 ][size + 1 ];
39
33
p = new int [size + 1 ];
@@ -44,51 +38,20 @@ public static void main(String[] args) {
44
38
}
45
39
46
40
for (int i = 0 ; i < p .length ; i ++) {
47
- p [i ] = i == 0 ? MATRICES .get (i ).col () : MATRICES .get (i - 1 ).row ();
41
+ p [i ] = i == 0 ? matrices .get (i ).col () : matrices .get (i - 1 ).row ();
48
42
}
49
43
50
- matrixChainOrder ();
51
- for (int i = 0 ; i < size ; i ++) {
52
- System .out .print ("-------" );
53
- }
54
- System .out .println ();
55
- printArray (m );
56
- for (int i = 0 ; i < size ; i ++) {
57
- System .out .print ("-------" );
58
- }
59
- System .out .println ();
60
- printArray (s );
61
- for (int i = 0 ; i < size ; i ++) {
62
- System .out .print ("-------" );
63
- }
64
- System .out .println ();
65
-
66
- System .out .println ("Optimal solution : " + m [1 ][size ]);
67
- System .out .print ("Optimal parens : " );
68
- printOptimalParens (1 , size );
69
- }
70
-
71
- private static void printOptimalParens (int i , int j ) {
72
- if (i == j ) {
73
- System .out .print ("A" + i );
74
- } else {
75
- System .out .print ("(" );
76
- printOptimalParens (i , s [i ][j ]);
77
- printOptimalParens (s [i ][j ] + 1 , j );
78
- System .out .print (")" );
79
- }
80
- }
81
-
82
- private static void printArray (int [][] array ) {
83
- for (int i = 1 ; i < size + 1 ; i ++) {
84
- for (int j = 1 ; j < size + 1 ; j ++) {
85
- System .out .printf ("%7d" , array [i ][j ]);
86
- }
87
- System .out .println ();
88
- }
44
+ matrixChainOrder (size );
45
+ return new Result (m , s );
89
46
}
90
47
91
- private static void matrixChainOrder () {
48
+ /**
49
+ * A helper method that computes the minimum cost of multiplying
50
+ * the matrices using dynamic programming.
51
+ *
52
+ * @param size the number of matrices in the multiplication sequence.
53
+ */
54
+ private static void matrixChainOrder (int size ) {
92
55
for (int i = 1 ; i < size + 1 ; i ++) {
93
56
m [i ][i ] = 0 ;
94
57
}
@@ -109,33 +72,92 @@ private static void matrixChainOrder() {
109
72
}
110
73
}
111
74
112
- private static String [] input (String string ) {
113
- System .out .print (string );
114
- return (SCANNER .nextLine ().split (" " ));
115
- }
116
- }
117
-
118
- class Matrix {
75
+ /**
76
+ * The Result class holds the results of the matrix chain multiplication
77
+ * calculation, including the matrix of minimum costs and split points.
78
+ */
79
+ public static class Result {
80
+ private final int [][] m ;
81
+ private final int [][] s ;
82
+
83
+ /**
84
+ * Constructs a Result object with the specified matrices of minimum
85
+ * costs and split points.
86
+ *
87
+ * @param m the matrix of minimum multiplication costs.
88
+ * @param s the matrix of optimal split points.
89
+ */
90
+ public Result (int [][] m , int [][] s ) {
91
+ this .m = m ;
92
+ this .s = s ;
93
+ }
119
94
120
- private final int count ;
121
- private final int col ;
122
- private final int row ;
95
+ /**
96
+ * Returns the matrix of minimum multiplication costs.
97
+ *
98
+ * @return the matrix of minimum multiplication costs.
99
+ */
100
+ public int [][] getM () {
101
+ return m ;
102
+ }
123
103
124
- Matrix (int count , int col , int row ) {
125
- this .count = count ;
126
- this .col = col ;
127
- this .row = row ;
104
+ /**
105
+ * Returns the matrix of optimal split points.
106
+ *
107
+ * @return the matrix of optimal split points.
108
+ */
109
+ public int [][] getS () {
110
+ return s ;
111
+ }
128
112
}
129
113
130
- int count () {
131
- return count ;
132
- }
114
+ /**
115
+ * The Matrix class represents a matrix with its dimensions and count.
116
+ */
117
+ public static class Matrix {
118
+ private final int count ;
119
+ private final int col ;
120
+ private final int row ;
121
+
122
+ /**
123
+ * Constructs a Matrix object with the specified count, number of columns,
124
+ * and number of rows.
125
+ *
126
+ * @param count the identifier for the matrix.
127
+ * @param col the number of columns in the matrix.
128
+ * @param row the number of rows in the matrix.
129
+ */
130
+ public Matrix (int count , int col , int row ) {
131
+ this .count = count ;
132
+ this .col = col ;
133
+ this .row = row ;
134
+ }
133
135
134
- int col () {
135
- return col ;
136
- }
136
+ /**
137
+ * Returns the identifier of the matrix.
138
+ *
139
+ * @return the identifier of the matrix.
140
+ */
141
+ public int count () {
142
+ return count ;
143
+ }
137
144
138
- int row () {
139
- return row ;
145
+ /**
146
+ * Returns the number of columns in the matrix.
147
+ *
148
+ * @return the number of columns in the matrix.
149
+ */
150
+ public int col () {
151
+ return col ;
152
+ }
153
+
154
+ /**
155
+ * Returns the number of rows in the matrix.
156
+ *
157
+ * @return the number of rows in the matrix.
158
+ */
159
+ public int row () {
160
+ return row ;
161
+ }
140
162
}
141
163
}
0 commit comments