C++ 數(shù)據(jù)結(jié)構(gòu)之對稱矩陣及稀疏矩陣的壓縮存儲
對稱矩陣及稀疏矩陣的壓縮存儲
1.稀疏矩陣
對于那些零元素?cái)?shù)目遠(yuǎn)遠(yuǎn)多于非零元素?cái)?shù)目,并且非零元素的分布沒有規(guī)律的矩陣稱為稀疏矩陣(sparse)。
人們無法給出稀疏矩陣的確切定義,一般都只是憑個(gè)人的直覺來理解這個(gè)概念,即矩陣中非零元素的個(gè)數(shù)遠(yuǎn)遠(yuǎn)小于矩陣元素的總數(shù),并且非零元素沒有分布規(guī)律。
實(shí)現(xiàn)代碼:
//稀疏矩陣及其壓縮存儲
#pragma once
#include <vector>
#include <iostream>
using namespace std;
template<class T>
struct Triple
{
size_t _r;
size_t _c;
T _value;
Triple(size_t row = 0, size_t col = 0, const T& value = T())
:_r(row)
,_c(col)
,_value(value)
{}
};
template <class T>
class SparseMatrix
{
public:
SparseMatrix()
:_row(0)
,_col(0)
,_illegal(T())
{}
SparseMatrix(T* arr, size_t row, size_t col, const T& illegal)
:_row(row)
,_col(col)
,_illegal(illegal)
{
for(size_t i = 0; i<row; ++i)
{
for(size_t j = 0; j<col; ++j)
{
if(arr[i*col+j] != illegal)
{
Triple<T> t(i,j,arr[i*col+j]);
_matrix.push_back(t);
}
}
}
}
void Display()
{
vector<Triple<T> >::iterator iter;
iter = _matrix.begin();
for(size_t i = 0; i<_row; ++i)
{
for(size_t j = 0; j<_col; ++j)
{
if(iter!=_matrix.end()
&&iter->_r == i
&&iter->_c == j)
{
cout << iter->_value <<" ";
++iter;
}
else
{
cout << _illegal <<" ";
}
}
cout << endl;
}
cout << endl;
}
//普通轉(zhuǎn)置(行優(yōu)先存儲)
//列變行,從0列開始,將列數(shù)據(jù)一個(gè)一個(gè)放進(jìn)轉(zhuǎn)置矩陣
SparseMatrix<T> Transpose()
{
SparseMatrix<T> tm;
tm._row = _col;
tm._col = _row;
tm._illegal = _illegal;
tm._matrix.reserve(_matrix.size());
for(size_t i = 0; i<_col; ++i)
{
size_t index = 0;
while(index < _matrix.size())
{
if(_matrix[index]._c == i)
{
Triple<T> t(_matrix[index]._c, _matrix[index]._r, _matrix[index]._value);
tm._matrix.push_back(t);
}
++index;
}
}
return tm;
}
SparseMatrix<T> FastTranspose()
{
SparseMatrix<T> tm;
tm._row = _col;
tm._col = _row;
tm._illegal = _illegal;
tm._matrix.resize(_matrix.size());
int* count = new int[_col];//記錄每行的元素個(gè)數(shù)
memset(count, 0, sizeof(int)*_col);
int* start = new int[_col];//轉(zhuǎn)置矩陣中元素的位置
start[0] = 0;
size_t index = 0;
while(index < _matrix.size())
{
count[_matrix[index]._c]++;
++index;
}
for(size_t i=1; i<_col; ++i)
{
start[i] = start[i-1] + count[i-1];
}
index = 0;
while(index < _matrix.size())
{
Triple<T> t(_matrix[index]._c, _matrix[index]._r, _matrix[index]._value);
tm._matrix[start[_matrix[index]._c]++] = t; //核心代碼
++index;
}
delete[] count;
delete[] start;
return tm;
}
protected:
vector<Triple<T> > _matrix;
size_t _row;
size_t _col;
T _illegal;
};
2.對稱矩陣
實(shí)現(xiàn)代碼:
//對稱矩陣及其壓縮存儲
#pragma once
#include <iostream>
using namespace std;
template <class T>
class SymmetricMatrix
{
public:
SymmetricMatrix(T* arr, size_t n)
:_n(n)
,_matrix(new T[n*(n+1)/2])
{
size_t index = 0;
for(size_t i = 0; i<n; ++i)
{
for(size_t j=0; j<n;++j)
{
if(i >= j)
{
_matrix[index] = arr[i*n+j];
++index;
}
else
{
continue;
}
}
}
}
void Display()
{
for(size_t i =0; i < _n; ++i)
{
for(size_t j = 0; j < _n; ++j)
{
/* if(i<j)
{
swap(i,j);
cout<<_matrix[i*(i+1)/2+j]<<" ";
swap(i,j);
}
else
cout<<_matrix[i*(i+1)/2+j]<<" ";
*/
cout << Access(i,j) << " ";
}
cout << endl;
}
cout << endl;
}
T& Access(size_t row, size_t col)
{
if(row<col)
{
swap(row, col);
}
return _matrix[row*(row+1)/2+col];
}
~SymmetricMatrix()
{
if(_matrix != NULL)
{
delete[] _matrix;
_matrix = NULL;
}
}
protected:
T* _matrix;
size_t _n; //對稱矩陣的行列大小
};
以上就是C++ 數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)稀疏矩陣與對稱矩陣,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
上一篇:C語言約瑟夫環(huán)的實(shí)現(xiàn)
欄 目:C語言
下一篇:C語言模擬實(shí)現(xiàn)atoi函數(shù)的實(shí)例詳解
本文標(biāo)題:C++ 數(shù)據(jù)結(jié)構(gòu)之對稱矩陣及稀疏矩陣的壓縮存儲
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1255.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)- 解析最少換車次數(shù)的問題詳解
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10APUE筆記之:進(jìn)程環(huán)境詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁面的局部加載


