稀疏矩阵的压缩存储及快速转置

当一个矩阵为稀疏矩阵时,有效数据的个数比无效数据要少得多,因此若将一个矩阵全部存储会浪费空间,可以只将有效数据存储起来,无效数据作为标记

代码如下:

#include <iostream>#include <vector>using namespace std;//可用一个三元组来存储有效数据的信息template<class T>struct Triple{ size_t _row; size_t _col; T _value;};//系数矩阵类 template<class T> class SparseMatrix { public:  SparseMatrix(T* arr, size_t rowsize, size_t colsize, const T& invaild);  void Display();  SparseMatrix<T>& Transpose();  SparseMatrix<T> FastTranspose(); protected:  vector<Triple<T>> _array;  size_t _rowsize;  size_t _colsize;  T _invaild; };  //可用顺序表依次存放有效数据 template<class T> SparseMatrix<T>::SparseMatrix(T* arr, size_t rowsize, size_t colsize, const T& invaild)  :_invaild(invaild)  ,_rowsize(rowsize)  ,_colsize(colsize) {  size_t rpos = 0;  for(rpos = 0; rpos < rowsize; rpos++)  {   size_t cpos = 0;   for(cpos = 0; cpos < colsize; cpos++)   {    if(arr[(rpos * colsize) + cpos] != invaild)    {     Triple<T> t;     t._row = rpos;     t._col = cpos;     t._value = arr[(rpos * colsize) + cpos];     _array.push_back(t);         }   }  } }  //打印矩阵 template <class T> void SparseMatrix<T>::Display() {  size_t rpos = 0;  size_t index = 0;  for(rpos = 0; rpos < _rowsize; rpos++)  {   size_t cpos = 0;      for(cpos = 0; cpos < _colsize; cpos++)   {    if((index < _array.size()) && (rpos == _array[index]._row) && (cpos == _array[index]._col))    {     cout<<_array[index]._value<<" ";     index++;    }    else     cout<<_invaild<<" ";   }   cout<<endl;  }  cout<<endl; }  //转置 template<class T>SparseMatrix<T>& SparseMatrix<T>::Transpose(){ size_t i = 0; size_t j = 0; for(i = 0; i < _array.size(); i++) {  for(j = i+1; j < _array.size(); j++)  {   if(_array[i]._col > _array[j]._col)    swap(_array[i],_array[j]);  }  swap(_array[i]._row,_array[i]._col); }  swap(_rowsize,_colsize); return *this;}//快速转置template<class T>SparseMatrix<T> SparseMatrix<T>::FastTranspose(){ size_t* rowcounts = new size_t[_colsize]; size_t index = 0; for(index = 0; index < _array.size(); index++) {  rowcounts[_array[index]._col]++;   //统计转置后矩阵每一行有效数据的个数 } size_t* rowstart = new size_t[_colsize]; rowstart[0] = 0; for(index = 1; index < _colsize; index++) {  rowstart[index] = rowcounts[index-1] + rowstart[index-1]; }   //统计转置后矩阵中每一行在压缩矩阵中的起始位置 vector<Triple<T>> arr; size_t count = 0; for(size_t i = 0; i < _colsize; i++) {  for(index = 0; index < _array.size(); index++)  {   if(rowcounts[i] != 0)   {    if(_array[index]._col == rowstart[i])    {     Triple<T> t;     t._col = _array[index]._row;     t._row = _array[index]._col;     t._value = _array[index]._value;     arr.push_back(t);    }   }   else    break;  } } swap(_array,arr); swap(_rowsize,_colsize); return *this;}


本文出自 “敲完代码好睡觉zzz” 博客,请务必保留此出处http://2627lounuo.blog.51cto.com/10696599/1750630

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值