压缩矩阵:指为多个值相同的元素只分配一个存储空间,对零元素不分配存储空间。在对二维数组进行基本操作时,时间复杂度达到了n的三次方,在经过压缩之后,时间复杂度可以得到有效的降低,现在对压缩的代码进行展示。
第一步,创建
typedef int elem;
/*
A triple for row index,column index,and data
*/
typedef struct Tripe {
int i;
int j;
elem e;
}Triple,*TriplePtr;
第二步,初始化操作
CompressedMatrixPtr initCompressedMatrix(int paraRows, int paraColumns, int paraElements, int** paraData) {
int i;
CompressedMatrixPtr resultPtr = (CompressedMatrixPtr)malloc(sizeof(struct CompressedMatrix));
resultPtr->rows = paraRows;
resultPtr->columns = paraColumns;
resultPtr->numElements = paraElements;
resultPtr->elements = (TriplePtr)malloc(paraElements * sizeof(struct Triple));
for (i = 0; i < paraElements; i++) {
resultPtr->elements[i].i = paraData[i][0];
resultPtr->elements[i].j = paraData[i][1];
resultPtr->elements[i].e = paraData[i][2];
}//of for i
return resultPtr;
}
第三步,打印工作
/*
print the compressed matrix.
*/
void printCompressedMatrix(CompressedMatrixPtr paraPtr) {
int i;
for (i = 0; i < paraPtr->numElements; i++) {
printf("(%d,%d):%d\r\n", paraPtr->elements[i].i, paraPtr[i].j, paraPtr->elements[i].e);
}//of for i
}//of printCompressedMatrix
第四步,压缩重要板块
CompressedMatrixPtr transposeCompressedMatrix(CompressedMatrixPtr paraPtr) {
//step 1. Allocate space.
int i, tempColumn, tempPosition;
int* tempColumnCounts = (int*)malloc(paraPtr->columns * sizeof(int));
int* tempoffsets = (int*)malloc(paraPtr->columns * sizeof(int));
for (i = 0; i < paraPtr->columns; i++) {
tempColumnCounts[i] = 0;
}//of for i
CompressedMatrixPtr resultPtr = (CompressedMatrixPtr)malloc(sizeof(struct CompressedMatrix));
resultPtr->rows = paraPtr->columns;
resultPtr->columns = paraPtr->rows;
resultPtr->numElements = paraPtr->numElements;
resultPtr->elements = (TriplePtr)malloc(paraPtr->numElements * sizeof(struct Triple));
//step 2.One scan to calculate offsets.
for (i = 0; i < paraPtr->numElements; i++) {
tempColumnCounts[paraPtr->Elements[i].j]++;
}//of for i.
//step 3. Another scan to fill data.
for (i = 0; i < paraPtr->numElements; i++) {
tempColumn = paraPtr->elements[i].j;
tempPosition = tempoffsets[tempColumn];
resultPtr->elements[tempPosition].i = paraPtr->elements[i].j;
resultPtr->elements[tempPosition].j = paraPtr->elements[i].i;
resultPtr->elements[tempPosition].e = paraPtr->elements[i].e;
tempoffsets[tempColumn]++;
}//of for i
return resultPtr;
}//of transposeCompressedMatrix
第五步,测试
void compressedMatrixTest() {
CompressedMatrixPtr 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));
}//Of for i
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];
}//Of for j
}//Of for i
tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);
printf("After initialization.\r\n");
printCompressedMatrix(tempPtr1);
tempPtr2 = transposeCompressedMatrix(tempPtr1);
printf("After transpose.\r\n");
printCompressedMatrix(tempPtr2);
}
主函数
int main() {
compressedMatrixTest();
return 1;
}// Of main
在进行压缩式定义了两个存储单元,一个存储在转置之前,第i列一共有几个元素,另一个记录填入之后的初始位置。