行逻辑链接,带行链接信息。程序有空指针BUG,至今未解决。还是C/C++适合描述算法数据结构。以后复杂的算法还是改用C/C++吧。
有BUG的代码,总有一天会换成没有BUG的。
package 行逻辑链接的顺序表实现稀疏矩阵的相乘;
public class Triple<T> {
int row,col;
T v;
public Triple(){}
public Triple(int row,int col, T v){
this.row = row;
this.col = col;
this.v = v;
}
}
package 行逻辑链接的顺序表实现稀疏矩阵的相乘;
public class Mat {
final int MAXSIZE = 10;
int mu,nu,tu;
int rpos[] = new int[MAXSIZE + 1];//各行第一个非零元的位置表
Triple<Integer> data[] = new Triple[MAXSIZE + 1];//Java不支持泛型数组
public Mat(int mu,int nu,int tu){
this.mu = mu;
this.nu = nu;
this.tu = tu;
for(int i=1; i<=MAXSIZE; i++)
data[i] = new Triple();
}
//三元组矩阵的输出
public void display(){
int i,j,k,m,n,count = 0;
for(i=1; i<=mu; i++){
for(j=1; j<=nu; j++){
for(k=1; k<=tu; k++){
if(i==data[k].row && j==data[k].col){
System.out.print(data[k].v + " ");
count = -1;
break;
}
}
if(count != -1)
System.out.print("0 ");
count = 0;
}
System.out.println();
}
}
}
package 行逻辑链接的顺序表实现稀疏矩阵的相乘;
import java.util.*;
public class MultMat {
public static void main(String[] args) {
int i,j,k,l;
Scanner scan = new Scanner(System.in);
System.out.println("请输入矩阵M1的行数,列数,非零元的个数:");
int mu1,nu1,tu1;
mu1 = scan.nextInt();
nu1 = scan.nextInt();
tu1 = scan.nextInt();
Mat M1 = new Mat(mu1,nu1,tu1);
//输入矩阵M1
System.out.println("请输入矩阵M1的三元组:");
for(i=1; i<=tu1; i++){
M1.data[i].row = scan.nextInt();
M1.data[i].col = scan.nextInt();
M1.data[i].v = scan.nextInt();
}
System.out.println("输入的矩阵M1为:");
M1.display();
System.out.println("请输入矩阵M2的行数,列数,非零元的个数:");
int mu2,nu2,tu2;
mu2 = scan.nextInt();
nu2= scan.nextInt();
tu2 = scan.nextInt();
Mat M2 = new Mat(mu2,nu2,tu2);
//输入矩阵M2
System.out.println("请输入矩阵M2的三元组:");
for(i=1; i<=tu2; i++){
M2.data[i].row = scan.nextInt();
M2.data[i].col = scan.nextInt();
M2.data[i].v = scan.nextInt();
}
System.out.println("输入的矩阵M2为:");
M2.display();
Mat M3 = new Mat(mu1,nu2,10);
int ctemp[] = new int[mu1 + 1];
int arow,brow=0,tp,t;//M1,M2的行数计数器
/*M1.rpos[1] = 1;
M1.rpos[2] = 3;
M1.rpos[3] = 4;
M2.rpos[1] = 1;
M2.rpos[2] = 2;
M2.rpos[3] = 3;
M2.rpos[4] = 5;*/
int num1[] = new int[mu1+10];
int col1,row1,t1;
for(col1=1; col1<=mu1; col1++){
num1[col1] = 0;
}
for(t1=1; t1<=mu1; t1++){
num1[M1.data[t1].row]++;
}
for(i=1; i<mu1; i++){
M1.rpos[i] = M1.rpos[i - 1] + num1[i - 1];
}
int num2[] = new int[mu2+10];
int col2,row2,t2;
for(col2=1; col2<=mu2; col2++){
num2[col2] = 0;
}
for(t2=1; t2<=mu1; t2++){
num2[M2.data[t2].row]++;
}
for(i=1; i<mu1; i++){
M2.rpos[i] = M2.rpos[i - 1] + num2[i - 1];
}
int p,q,ccol;
for(arow=1; arow<=mu1; arow++){
ctemp[arow] = 0;//当前各行元素累加器清零
M3.rpos[arow] = M3.tu + 1;
if(arow < mu1){
tp = M1.rpos[arow + 1];
}
else{
tp = mu1 + 1;
}
for(p=M1.rpos[arow]; p<tp; p++){
brow = M1.data[p].col;
//System.out.println(p + " " + brow);
if(brow < mu2){
t = M2.rpos[brow + 1];
}
else{
t = tu2 + 1;
}
for(q=M2.rpos[brow]; q<t; q++){
ccol = M2.data[q].col;
ctemp[ccol] += M1.data[p].v * M2.data[q].v;
}
}
for(ccol=1; ccol<=M3.nu; ccol++){
if(ctemp[ccol]!=0){
M3.data[M3.tu].row = arow;
M3.data[M3.tu].col = ccol;
M3.data[M3.tu].v = ctemp[ccol];
}
}
}
System.out.println("相乘后的矩阵为:");
M3.display();
}
}