详见:http://data.biancheng.net/view/97.html
拿图 1 中的矩阵 A 和 B 来说,其各自用三元组顺序表表示(分别用 a 和b 表示),如下图所示:
图2 矩阵 A、B的三元组顺序表
在三元组表的存储形式下,求稀疏矩阵 A 的转置矩阵 B ,实际上就是求由 a 得到 b:
- 由 a 的行数、列数以及非 0 元素数可以直接得到 b 的列数、行数和非 0 元素数。
- 由 a 中的数据得到 b 中的数据,可采用两种方法实现:
- 对 a 中的数据进行遍历,即依次扫描第 0 列、第 1 列、……、最后一列,扫描过程交换行和列的顺序,并存储到 b 中对应的位置即可。
- 要想扫描一次 a 就能得到 b,必须每次扫描到一个三元组就直接将其放到 b 中相应的位置上,因此,需要知道 a 中的元素在 b 中的存储位置,这就要预先确定矩阵 A 的每一列的第一个非 0 元素在 b 中相应的位置。为此,需要附设两个数组,num 和cpot,分别用于存储矩阵 A 中每一列的非 0 元素个数和矩阵 A 中每一列第 1 个非0 元素在 b 中的存储位置。
显然,有如下的公式成立:
- cpot[0] = 1
- cpot[col] = cpot[col - 1] + num[col -1],col 取大于等于 1 且小于 n 的数
所以,图 1 中的矩阵 A ,其 num 和 cpot 数组的值为:
全部代码:
#include <stdio.h>
#include <malloc.h>
typedef int elem;
/**
* A triple for row index, column index, and data.
*/
typedef struct Triple{
int i;
int j;
elem e;
} Triple, *TriplePtr;
/**
* A triple for row index, column index, and data.
*/
typedef struct CompressedMatrix{
int rows,columns,numElements;
Triple* elements;
} CompressedMatrix, *CompressedMatrixPtr;
/**
* Initialize a compressed matrix.
*/
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;
}// Of initCompressedMatrix
/**
* 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->elements[i].j, paraPtr->elements[i].e);
}//Of for i
}// Of printCompressedMatrix
/**
* Transpose a compressed matrix.
*/
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
tempOffsets[0] = 0;
for(i = 1; i < paraPtr->columns; i ++){
tempOffsets[i] = tempOffsets[i - 1] + tempColumnCounts[i - 1];
printf("tempOffsets[%d] = %d \r\n", i, tempOffsets[i]);
}//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
/**
* Test the compressed matrix.
*/
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);
}// Of main
/**
* The entrance.
*/
int main(){
compressedMatrixTest();
return 1;
}// Of main
运行结果
After initialization.
(0, 0): 2
(0, 2): 3
(2, 0): 5
(2, 1): 6
tempOffsets[1] = 2
tempOffsets[2] = 3
After transpose.
(0, 0): 2
(0, 2): 5
(1, 2): 6
(2, 0): 3
Press any key to continue