当前位置:网站首页>稀疏矩阵转置--C语言
稀疏矩阵转置--C语言
2022-08-08 19:09:00 【Bwy_1004】
一、特殊矩阵的存储—稀疏矩阵
1.采用矩阵的常规存储方式实现矩阵转置
//稀疏矩阵转置 二维数组
void TransMatrix(int source[m][n],int dest[n][m]){
//source表示被转置的矩阵 dest表示转置后的矩阵
int i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
dest[j][i]=source[i][j]; //行列对换
}
}
}
2.采用三元组表方式实现矩阵转置
重新排序会大量移动元素,从而影响算法的效率,为了避免行列互换后重新排序引起的数据移动。
1、列序递增转置法
2、一次定位快速转置法
2.1 列序递增转置法
//列序递增转置法 三元组
void TransposeTSMatrix(TSMatrix A,TSMatrix *B){
int i,j,k;
B->m=A->n;
B->n=A->m;
B->len=A->len;
if(B->len>0){
j=1; //辅助计数器
for(k=1;k<=A->n;k++){
for(i=1;i<=A->len;i++){
if(A->data[i]->col==k){
B->data[j]->row=A->data[i]->col;
B->data[j]->col=A->data[i]->row;
B->data[j]->e=A->data[i]->e;
j++;
}
}
}
}
}
存储空间减少,但时间复杂度并未降低。原因是要多次扫描稀疏矩阵三元组。
所以可否降低时间复杂度?
降低扫描三元组的次数(去掉双重循环)。
能否扫描一遍就找到位置?----一次定位快速转置法
2.2 一次定位快速转置法
num[]数组计算方式?
position[]数组计算方式?
那么一次定位快速转置的具体做法??
……
……
//一次定位快速转置法 三元组
void FastTransposeTSMatrix(TSMatrix A,TSMatrix *B){
int col,t,q,p;
int num[MAXSIZE],position[MAXSIZE];
B->m=A->n;
B->n=A->m;
B->len=A->len;
if(B->len>0){
for(col=1;col<=A->n;col++){
num[col]=0;
}
for(t=1;t<A.len;t++){
num[A->data[t]->col]++;
}
position[1]=1;
for(col=2;col<=A.n;col++){
position[col]=num[col-1]+position[col-1];
}
for(p=1;p<A.len;p++){
col=A->data[p]->col;
q=position[col];
B->data[q]->row=A->data[p]->col;
B->data[q]->col=A->data[p]->row;
B->data[q]->e=A->data[p]->e;
position[col]++;
}
}
}
算法的时间耗费主要是4个并列的单循环上
在时间性能上优于递增转置法,但在空间耗费上增加了两个辅助向量空间
边栏推荐
猜你喜欢
BP神经网络
软考中级网络工程师全面学习笔记第2版(5万字)+配套视频及课件
在Unity URP中实现Forward+
Why Manufacturing Companies Should Deploy Digital Factory Systems
Performance optimization | CPU power management from the perspective of ping delay
wps表格怎么设置公式自动计算?wps表格设置公式自动计算的方法
Generate captchas tools
Fortinet new cloud native protection products launched amazon cloud platform of science and technology
Architecture Design Fundamentals
黑猫带你学Makefile第5篇:Makefile中函数的使用
随机推荐
Fortinet new cloud native protection products launched amazon cloud platform of science and technology
shell的各种三角形
What are the benefits of knowledge sharing for businesses?
文档管理系统对于企业来说有哪些作用?
计算机网络面试常问知识
黑猫带你学Makefile第1篇:什么是Makefile
经验分享|低成本快节奏搭建企业知识管理系统的方法
nyoj 712 探 寻 宝 藏(双线dp 第六届河南省程序设计大赛)
Monaco-Editor 多人协作 编辑器
证券开户选哪个券商平台比较好,哪个更安全
瑞芯微rk1126 平台部分jpeg图片解码程序挂掉的问题
hdu1495 非常可乐 (广搜)
黑猫带你学Makefile第10篇:如何将未被编译的代码/自己写的驱动编译进uboot
数据泵导出数据报39006是什么原因
企业进行知识共享的好处有哪些?
5 IPOs, Internet home improvement is not as simple as Tubatu thinks
Monaco-Editor Multiplayer Collaboration Editor
C language elementary - structure
互联网技术从业者怎么解决系统高并发?
阿里云数据库PolarDB开源人才培养计划发布!万元好礼等你来拿!