您的位置:首页 > 其它

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

2016-03-13 20:28 190 查看
当一个矩阵为稀疏矩阵时,有效数据的个数比无效数据要少得多,因此若将一个矩阵全部存储会浪费空间,可以只将有效数据存储起来,无效数据作为标记
代码如下:
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: