private int[][] mmChainDP(double[] dimArray,int size){
  double[][] dpMatrix=new double[size][size];
  int[][] split=new int[size][size];
  for (int i=0; i < size; i++) {
    Arrays.fill(dpMatrix[i],0);
    Arrays.fill(split[i],-1);
  }
  for (int l=2; l <= size; l++) {
    for (int i=0; i < size - l + 1; i++) {
      int j=i + l - 1;
      dpMatrix[i][j]=Double.MAX_VALUE;
      for (int k=i; k <= j - 1; k++) {
        double cost=dpMatrix[i][k] + dpMatrix[k + 1][j] + (dimArray[i] * dimArray[k + 1] * dimArray[j + 1]);
        if (cost < dpMatrix[i][j]) {
          dpMatrix[i][j]=cost;
          split[i][j]=k;
        }
      }
      if (LOG.isTraceEnabled()) {
        LOG.trace("mmchainopt [i=" + (i + 1) + ",j="+ (j + 1)+ "]: costs = "+ dpMatrix[i][j]+ ", split = "+ (split[i][j] + 1));
      }
    }
  }
  return split;
}

private int [ ] [ ] mmChainDP ( double [ ] dimArray , int size ) { double [ ] [ ] dpMatrix = new double [ size ] [ size ] ; int [ ] [ ] split = new int [ size ] [ size ] ; for ( int i = 0 ; i < size ; i ++ ) { Arrays . fill ( dpMatrix [ i ] , 0 ) ; Arrays . fill ( split [ i ] , - 1 ) ; } for ( int l = 2 ; l <= size ; l ++ ) { for ( int i = 0 ; i < size - l + 1 ; i ++ ) { int j = i + l - 1 ; dpMatrix [ i ] [ j ] = Double . MAX_VALUE ; for ( int k = i ; k <= j - 1 ; k ++ ) { double cost = dpMatrix [ i ] [ k ] + dpMatrix [ k + 1 ] [ j ] + ( dimArray [ i ] * dimArray [ k + 1 ] * dimArray [ j + 1 ] ) ; if ( cost < dpMatrix [ i ] [ j ] ) { dpMatrix [ i ] [ j ] = cost ; split [ i ] [ j ] = k ; } } if ( LOG . isTraceEnabled ( ) ) { LOG . trace ( "mmchainopt [i=" + ( i + 1 ) + ",j=" + ( j + 1 ) + "]: costs = " + dpMatrix [ i ] [ j ] + ", split = " + ( split [ i ] [ j ] + 1 ) ) ; } } } return split ; }
mmchaindp ( ) : core method to perform dynamic programming on a given array of matrix dimensions . thomas h . cormen , charles e . leiserson , ronald l . rivest , clifford stein introduction to algorithms , third edition , mit press , page 395 .
