-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFig10_46.java
More file actions
60 lines (56 loc) · 2.1 KB
/
Fig10_46.java
File metadata and controls
60 lines (56 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class Fig10_46
{
public static final long INFINITY = Long.MAX_VALUE;
/**
* Compute optimal ordering of matrix multiplication.
* c contains the number of columns for each of the n matrices.
* c[ 0 ] is the number of rows in matrix 1.
* The minimum number of multiplications is left in m[ 1 ][ n ].
* Actual ordering is computed via another procedure using lastChange.
* m and lastChange are indexed starting at 1, instead of 0.
* Note: Entries below main diagonals of m and lastChange
* are meaningless and uninitialized.
*/
public static void optMatrix( int [ ] c, long [ ][ ] m, int [ ][ ] lastChange )
{
int n = c.length - 1;
for( int left = 1; left <= n; left++ )
m[ left ][ left ] = 0;
for( int k = 1; k < n; k++ ) // k is right - left
for( int left = 1; left <= n - k; left++ )
{
// For each position
int right = left + k;
m[ left ][ right ] = INFINITY;
for( int i = left; i < right; i++ )
{
long thisCost = m[ left ][ i ] + m[ i + 1 ][ right ]
+ c[ left - 1 ] * c[ i ] * c[ right ];
if( thisCost < m[ left ][ right ] ) // Update min
{
m[ left ][ right ] = thisCost;
lastChange[ left ][ right ] = i;
}
}
}
}
public static void main( String [ ] args )
{
int [ ] c = { 50, 10, 40, 30, 5 };
long [ ][ ] m = new long [ 5 ][ 5 ];
int lastChange[ ][ ] = new int [ 5 ][ 5 ];
optMatrix( c, m, lastChange );
for( int i = 1; i <= 4; i++ )
{
for( int j = 1; j <= 4; j++ )
System.out.print( m[ i ][ j ] + " " );
System.out.println( );
}
for( int i = 1; i <= 4; i++ )
{
for( int j = 1; j <= 4; j++ )
System.out.print( lastChange[ i ][ j ] + " " );
System.out.println( );
}
}
}