数据结构——压缩矩阵的转置

压缩矩阵在实际应用中广泛使用

老师的代码

#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

我的代码

#include<iostream>
#include<cstdlib>
#include<cstdio>
#define MAXSIZE 12500
 
typedef struct{
	int i,j;
	int e;
}Triple;
 
typedef struct{
	Triple data[MAXSIZE+1];
	int mu,nu,tu;			//存储矩阵的行数、列数、非零元个数 
}TSMatrix;
 
void input(TSMatrix &sm){
	for(int i=1;i<=sm.nu*sm.mu;i++)		//【1】如果普通的转置算法写成sm.mu*sm.nu,压缩矩阵则要把判定条件改为:i<=sm.tu
		scanf("%d%d%d",&sm.data[i].i,&sm.data[i].j,&sm.data[i].e);		//【2】注意要按照自己定义的结构体格式进行输入 
}
 
void print(TSMatrix sm){
	int index=1;
	for(int i=1;i<=sm.mu;i++){
		for(int j=1;j<=sm.nu;j++){ 
			printf("%d\t",sm.data[index].e);
			index++;
		} 
		printf("\n"); 
	}
}
 
//稀疏矩阵的转置操作
//按照矩阵M的列序进行转置 
int TransposeSMatrix(TSMatrix M,TSMatrix &T){
	T.nu=M.mu;T.mu=M.nu;T.tu=M.tu;
	if(T.tu){
		int q=1;
		for(int col=1;col<=M.nu;col++)
			for(int p=1;p<=M.tu;p++)
				if(M.data[p].j==col){			
					T.data[q].i=M.data[p].j;
					T.data[q].j=M.data[p].i;
					T.data[q].e=M.data[p].e;
					++q;	
			} 
	}
	return 1;
} 
 
int main(){
	freopen("input1.txt","r",stdin);
	freopen("input2.txt","r",stdin);
//	printf("请输入矩阵的行数、列数、非零元素的个数:\n");
	printf("原矩阵为:\n");
	TSMatrix sm,ms;
	scanf("%d%d%d",&sm.mu,&sm.nu,&sm.tu);
	input(sm);
	print(sm);
	TransposeSMatrix(sm,ms);
	printf("转置完:\n");
	print(ms);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
稀疏矩阵转置是将稀疏矩阵的行列互换得到新的矩阵。 在C语言中,可以使用三元组表示法来存储稀疏矩阵。三元组表示法包括三个数组:行索引数组、列索引数组和值数组。其中,行索引数组存储非零元素所在的行号,列索引数组存储非零元素所在的列号,值数组存储非零元素的值。 稀疏矩阵转置的基本思路是遍历原始稀疏矩阵,将每个非零元素的行列互换后存储到新的稀疏矩阵中。 下面是一个示例代码实现: ```c #include<stdio.h> #define MAX_TERMS 100 typedef struct { int row; int col; int value; } Element; void transpose(Element a[], Element b[]) { int n, m, terms, i, j, currentB; n = a[0].row; m = a[0].col; terms = a[0].value; b[0].row = m; b[0].col = n; b[0].value = terms; if (terms > 0) { currentB = 1; for (j = 0; j < m; j++) { for (i = 1; i <= terms; i++) { if (a[i].col == j) { b[currentB].row = a[i].col; b[currentB].col = a[i].row; b[currentB].value = a[i].value; currentB++; } } } } } int main() { int n, m, i, j, count = 1; printf("Enter the number of rows and columns: "); scanf("%d %d", &n, &m); Element a[MAX_TERMS], b[MAX_TERMS]; a[0].row = n; a[0].col = m; printf("Enter the elements of the matrix: \n"); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { int element; scanf("%d", &element); if (element != 0) { a[count].row = i; a[count].col = j; a[count].value = element; count++; } } } transpose(a, b); printf("\nOriginal sparse matrix:\n"); for (i = 0; i <= a[0].value; i++) { printf("%d\t%d\t%d\n", a[i].row, a[i].col, a[i].value); } printf("\nTransposed sparse matrix:\n"); for (i = 0; i <= b[0].value; i++) { printf("%d\t%d\t%d\n", b[i].row, b[i].col, b[i].value); } return 0; } ``` 这段代码中,我们首先定义了一个 `Element` 结构体来表示稀疏矩阵的非零元素。然后,使用 `transpose` 函数来实现稀疏矩阵转置操作。最后,在 `main` 函数中,我们可以输入稀疏矩阵的行列数和元素,并输出原始稀疏矩阵转置后的稀疏矩阵。 希望这段代码能帮助到你!如果有任何问题,请随时向我提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值