matrix_chain_order
2024-09-20 17:12:59
to calculate the min step of multiplicate some matixs
package dynamic_programming; public class matrix_chain_order { //input is a sequence p = p0,p1..pn,where p.length = n+1 (matrix n is pn-1pn)
int[] p;
int[][] cost;
public matrix_chain_order(int[] a){
p = a;
}
public int order(){
int q = 0;
int n = p.length -1;
cost = new int[n][n];
for(int i = 0;i<= n-1;i++){
cost[i][i] = 0;
}
for(int l = 2;l<n;l++){ //the chain length,like merge sort
for(int i=0;i<n-l;i++){
int j = i+l -1;
cost[i][j] =Integer.MAX_VALUE;
for(int k = i;k <=j -1;k++){
q = cost[i][k] + cost[k+1][j] + p[i-1]*p[k]*p[j];
if(q < cost[i][j]){
cost[i][j] = q; //remeber the best step of i to j
}
}
}
}
return cost[n-1][n-1];
} }
最新文章
- 继承映射关系 TPH、TPT、TPC<;EntityFramework6.0>;
- easyUI 中datagrid 返回列隐藏方法
- 关于flume配置加载
- ACM zb的生日
- [css]需警惕CSS3属性的书写顺序
- Redis设计思路学习与总结
- IIS7 / IIS7.5 URL 重写 HTTP 重定向到 HTTPS(转)
- 用PHP添加购物商品
- Js实现简单的联动,无数据库版本
- Android传感器编程带实例(转)
- CentOS7.2静默安装oracle11g
- 升级安装php7要考虑的几个方面
- 用eNSP模拟
- Elastic Stack之kibana入门
- Swift4.0 从相册中获取图片和拍照
- 线性代数导论 | Linear Algebra 课程
- java获取泛型类型
- [Laravel] 08 - Auth &; Data Migration
- https://blog.csdn.net/dayancn/article/details/54692111
- chorem浏览器无法下载