程序碎片- 矩阵乘法优化(dp,循环)
2009-12-21 21:01
585 查看
下班了换个eclipse写写,感觉还蛮好的。
这个循环的原理是,沿着矩阵的对角线,一层层向上计算,
发现这样的过程中,前面的条件都已经算好了。所以就不用递归了。
import
java.util.ArrayList;
public
class
MatrixMultiply {
public
MatrixMultiply() {
Matrix m1 =
new
Matrix(2, 5);
Matrix m2 =
new
Matrix(5, 100);
Matrix m3 =
new
Matrix(100, 4);
Matrix m4 =
new
Matrix(4, 50);
Matrix m5 =
new
Matrix(50, 6);
MList
.add(m1);
MList
.add(m2);
MList
.add(m3);
MList
.add(m4);
MList
.add(m5);
for
(
int
i = 0; i < 5; i++)
for
(
int
j = 0; j < 5; j++) {
if
(i >= j)
M
[i][j] = 0;
else
M
[i][j] = Integer.
MAX_VALUE
;
}
}
private
int
[][]
M
=
new
int
[5][5];
private
ArrayList<Matrix>
MList
=
new
ArrayList<Matrix>();
public
void
getResult() {
// already fill the left-below half of the matrix, now fill the left-up
// part, the specialIndex is build by analysis matrix's structure
for
(
int
specialIndex = 1; specialIndex < 5; specialIndex++) {
int
xIndex = 0;
int
yIndex = xIndex + specialIndex;
while
(yIndex < 5) {
M
[xIndex][yIndex] = getMax(xIndex, yIndex);
yIndex++;
xIndex++;
}
}
System.
out
.println(
"the optimized result is :"
+
M
[0][4]);
}
private
int
getMax(
int
xIndex,
int
yIndex) {
if
(
M
[xIndex][yIndex] != Integer.
MAX_VALUE
)
return
M
[xIndex][yIndex];
else
{
int
finalMin = Integer.
MAX_VALUE
;
int
tempMin;
for
(
int
k = xIndex; k < yIndex; k++) {
tempMin =
M
[xIndex][k] +
M
[k + 1][yIndex]
+
MList
.get(xIndex).
orderX
*
MList
.get(k).
orderY
*
MList
.get(yIndex).
orderY
;
if
(finalMin > tempMin)
finalMin = tempMin;
}
return
finalMin;
}
}
class
Matrix {
public
Matrix(
int
x,
int
y) {
orderX
= x;
orderY
= y;
}
int
orderX
;
int
orderY
;
}
}
这个循环的原理是,沿着矩阵的对角线,一层层向上计算,
发现这样的过程中,前面的条件都已经算好了。所以就不用递归了。
import
java.util.ArrayList;
public
class
MatrixMultiply {
public
MatrixMultiply() {
Matrix m1 =
new
Matrix(2, 5);
Matrix m2 =
new
Matrix(5, 100);
Matrix m3 =
new
Matrix(100, 4);
Matrix m4 =
new
Matrix(4, 50);
Matrix m5 =
new
Matrix(50, 6);
MList
.add(m1);
MList
.add(m2);
MList
.add(m3);
MList
.add(m4);
MList
.add(m5);
for
(
int
i = 0; i < 5; i++)
for
(
int
j = 0; j < 5; j++) {
if
(i >= j)
M
[i][j] = 0;
else
M
[i][j] = Integer.
MAX_VALUE
;
}
}
private
int
[][]
M
=
new
int
[5][5];
private
ArrayList<Matrix>
MList
=
new
ArrayList<Matrix>();
public
void
getResult() {
// already fill the left-below half of the matrix, now fill the left-up
// part, the specialIndex is build by analysis matrix's structure
for
(
int
specialIndex = 1; specialIndex < 5; specialIndex++) {
int
xIndex = 0;
int
yIndex = xIndex + specialIndex;
while
(yIndex < 5) {
M
[xIndex][yIndex] = getMax(xIndex, yIndex);
yIndex++;
xIndex++;
}
}
System.
out
.println(
"the optimized result is :"
+
M
[0][4]);
}
private
int
getMax(
int
xIndex,
int
yIndex) {
if
(
M
[xIndex][yIndex] != Integer.
MAX_VALUE
)
return
M
[xIndex][yIndex];
else
{
int
finalMin = Integer.
MAX_VALUE
;
int
tempMin;
for
(
int
k = xIndex; k < yIndex; k++) {
tempMin =
M
[xIndex][k] +
M
[k + 1][yIndex]
+
MList
.get(xIndex).
orderX
*
MList
.get(k).
orderY
*
MList
.get(yIndex).
orderY
;
if
(finalMin > tempMin)
finalMin = tempMin;
}
return
finalMin;
}
}
class
Matrix {
public
Matrix(
int
x,
int
y) {
orderX
= x;
orderY
= y;
}
int
orderX
;
int
orderY
;
}
}
相关文章推荐
- 程序碎片- 矩阵乘法优化(dp,递归)
- 矩阵乘法优化DP
- 【bzoj2476】战场的数目 矩阵乘法优化dp
- BZOJ1009 GT考试 (DP 矩阵乘法优化)
- 【矩阵乘法优化DP】Codeforces 717D Dexterina’s Lab
- 【Contra】 矩阵乘法优化 dp
- Poj 3734 Blocks(DP,矩阵乘法优化)
- 1009: [HNOI2008]GT考试 矩阵乘法优化DP+KMP
- 一个矩阵乘法优化期望dp的题
- fzu 1692 Key problem(循环同构矩阵o(n^2)优化乘法)
- BZOJ 1875 [SDOI 2009] HH去散步 (DP,矩阵乘法优化)
- BestCoder Round #68 (div.1) B 矩阵乘法优化DP
- BZOJ 3120 Line【矩阵乘法优化dp
- Codevs 1305 Freda的道路(矩阵乘法 DP优化)
- Python numpy 矩阵特殊加、乘法与循环优化
- 矩阵乘法优化DP
- bzoj 1898: [Zjoi2005]Swamp 沼泽鳄鱼 (矩阵乘法优化DP)
- [BZOJ]1875: [SDOI2009]HH去散步 矩阵乘法优化DP
- 程序性能优化探讨(5)——高速缓存、存储器山与矩阵乘法优化
- bzoj 4870: [Shoi2017]组合数问题 [矩阵乘法优化dp]