一·浅谈转置矩阵
转置矩阵一般有两种方式,一种是直接交换行和列的次序,这种算法的时间复杂度为O(n^2).有没有一种方法能够降低时间复杂度呢?有,也就是下面要说的快速转置矩阵。快速转置矩阵的核心是要定义两个数组。第一个数组用来储存压缩矩阵中每列的有效值的个数,第二个数组用来储存每列第一个有效值的次序。
不多说直接上代码。
#include <stdio.h>
#include <malloc.h>
typedef int elem;
typedef struct Triple{
int i,j; //该非零元的行下标和列下标
int e; //非零元对应的值
}Triple, *TriplePtr;
typedef struct CompressedMatrix{
Triple *element; //非零元三元组表
int row,col,numElements; //矩阵的行数,列数,非零元个数
}Matrix,*MatrixPtr;
//初始化函数
MatrixPtr initCompressedMatrix(int paraRows, int paraColumns, int paraElements, int** paraData){
int i;
MatrixPtr resultPtr=(MatrixPtr)malloc(sizeof(Matrix));
resultPtr->row=paraRows;
resultPtr->col=paraColumns;
resultPtr->numElements=paraElements;
resultPtr->element=(TriplePtr)malloc(paraElements*sizeof(Triple));
for(i=0;i<paraElements;i++){
resultPtr->element[i].i=paraData[i][0];
resultPtr->element[i].j=paraData[i][1];
resultPtr->element[i].e=paraData[i][2];
}
return resultPtr;
}
//打印压缩矩阵函数
void printMatrix(MatrixPtr paraPtr){
int i;
for(i = 0; i < paraPtr->numElements; i ++){
printf("(%d, %d): %d\r\n", paraPtr->element[i].i, paraPtr->element[i].j, paraPtr->element[i].e);
}
}
//快速转置矩阵
MatrixPtr transMatrix(MatrixPtr paraPtr){
MatrixPtr resultPtr= (MatrixPtr)malloc(sizeof(Matrix));
resultPtr->row = paraPtr->col;
resultPtr->col = paraPtr->row;
resultPtr->numElements = paraPtr->numElements;
resultPtr->element=(TriplePtr)malloc(resultPtr->numElements*sizeof(Triple));
int i, tempColumn, tempPosition;
int tempColumnCounts[100],tempOffsets[100];
for (i = 0; i < paraPtr->col;i++){
tempColumnCounts[i] = 0;
}
if(paraPtr->numElements){
int t;
for(t=0;t<paraPtr->numElements;t++){
tempColumnCounts[paraPtr->element[t].j]++; //求矩阵中每一列含有的非零元个数
}
tempOffsets[0]=0;
for (i= 1; i<paraPtr->col;i++){
tempOffsets[i] = tempOffsets[i-1] + tempColumnCounts[i - 1]; //存放每一列第一个非零元的次序
}
int q;
for (i= 0; i < paraPtr->numElements; ++i){
tempColumn =paraPtr->element[i].j;
tempPosition =tempOffsets[tempColumn];
resultPtr->element[tempPosition].i=paraPtr->element[i].j;
resultPtr->element[tempPosition].j=paraPtr->element[i].i;
resultPtr->element[tempPosition].e=paraPtr->element[i].e;
tempOffsets[tempColumn]++;
}
}
return resultPtr;
}
int main(){
MatrixPtr tempPtr1, tempPtr2;
int i, j, tempElements;
//Construct the first sample matrix.
tempElements = 4;
int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
for(i = 0; i < tempElements; i ++){
tempMatrix1[i] = (int*)malloc(3 * sizeof(int));
}
int tempMatrix2[4][3] = {{0, 0, 2}, {0, 2, 3}, {2, 0, 5}, {2, 1, 6}};
for(i = 0; i <tempElements; i ++){
for(j = 0; j<3; j ++) {
tempMatrix1[i][j] = tempMatrix2[i][j];
}
}
tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);
printf("After initialization.\r\n");
printMatrix(tempPtr1);
tempPtr2 = transMatrix(tempPtr1);
printf("After transpose.\r\n");
printMatrix(tempPtr2);
}
测试代码
After initialization.
(0, 0): 2
(0, 2): 3
(2, 0): 5
(2, 1): 6
After transpose.
(0, 0): 2
(0, 2): 5
(1, 2): 6
(2, 0): 3
这里借用书上的图来帮助大家理解
cpot就是tempOffsets数组
cpot[1]=1.
cpot[2]=num[1]+cpot[1]=1+2=3
cpot[3]=num[2]+cpot[2]=2+3=5
cpot[4]=num[3]+cpot[3]=2+5=7
cpot[5]=num[4]+cpot[4]=1+7=8
cpot[6]=num[5]+cpot[5]=0+8=8
cpot[7]=num[6]+cpot[6]=1+8=9
这个表就是这么填的。
二.总结
重点是理解这两个数组的作用,掌握了对于快速转置矩阵应该就不难了。
在初始化的时候注意不要将行和列赋值赋反了,这个问题折磨笔者太久了,一开始运行出现段错误,原本以为是转置函数写错了,最后调试的时候才发现初始化就出现了错误。