matrix_chain_order
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];
}
}
matrix_chain_order的更多相关文章
- 算法导论——lec 11 动态规划及应用
和分治法一样,动态规划也是通过组合子问题的解而解决整个问题的.分治法是指将问题划分为一个一个独立的子问题,递归地求解各个子问题然后合并子问题的解而得到原问题的解.与此不同,动态规划适用于子问题不是相互 ...
- Algorithm --> 矩阵链乘法
动态规划--矩阵链乘法 1.矩阵乘法 Note:只有当矩阵A的列数与矩阵B的行数相等时A×B才有意义.一个m×r的矩阵A左乘一个r×n的矩阵B,会得到一个m×n的矩阵C. #include ...
- 理解DP(持续更新)
理解DP author: thy from buaa 初见 dynamic programming(可以理解为动态刷表法 其实这里的programming并不是编程而是规划.设计表格的意思) 关于动态 ...
- (最大矩阵链乘)Matrix-chain product
Matrix-chain product. The following are some instances. a) <3, 5, 2, 1,10> b) < ...
随机推荐
- python 竖排文本
新建目录train,并将目录data和data1复制到train下 python test data/,data1/ 目录data和data1中包含很多文件,文件中内容都是以空格分隔,将所有文件内容都 ...
- Why Choose MB SD C5 with Engineer Software
MB SD C5 with engineer software performed good and now is released. Unlike the old clone C5 which us ...
- Flutter 获取控件尺寸和位置
1. 插件必须渲染好, final RenderBox box = globalKey.currentContext.findRenderObject(); final size = box.size ...
- Netbeans and Remote Host for C/C++ Developing
Netbeans and Remote Host for C/C++ Developing 很久以来,因为我不适应在 Linux 下使用 Vim, GCC, GDB 开发 C/C++ 程序,所以我一直 ...
- Java解决异常之try、catch、finally、throw、throws&log4j记录日志步骤
知识点一.多重catch引发多种类型的异常排列catch 语句的顺序:先子类后父类 发生异常时按顺序逐个匹配只执行第一个与异常类型匹配的catch语句二.异常分类异常分为运行时异常和检测异常运行时异常 ...
- libcurl返回常见错误码
转载:https://blog.csdn.net/kenkao/article/details/46875571 转载:http://www.cnblogs.com/wainiwann/p/34929 ...
- pip使用豆瓣的镜像源
豆瓣镜像地址:https://pypi.douban.com/simple/ 虽然用easy_install和pip来安装第三方库很方便 它们的原理其实就是从Python的官方源pypi.python ...
- FJUT3701 这也是一道数论题(线段树)题解
Problem Description 好久没出数据结构题,现在赶紧来做道数据结构题热热身 小q现在要去银行,他有个很厉害的bug能看到前面排队的人.假如当前有人正在办理业务,那么肯定要等待前一个人完 ...
- linux下使用maven修改hbase源码并重新编译
一.准备 maven已配置 JDK已配置 二.修改相关hbase代码 三.使用maven编译hbase-2.0.0 在hbase src根目录下,执行以下命令 mvn clean package -D ...
- robot framework测试数据语法
Robot Framework通过文件的扩展名来选择使用何种解析器. 扩展名不分大小写. 可以识别的扩展名包括: HTML: .html, .htm 和 .xhtml TSV: .tsv 纯文本: . ...