稀疏矩陣的壓縮儲存和轉置

2021-07-11 07:23:04 字數 1432 閱讀 5172

稀疏矩陣的特點:零元多,非零元遠少於零元,儲存資料沒什麼規律

因此可以採用只儲存非零元素的方法來進行壓縮儲存(為了節省空間)。所以在進行壓縮儲存的時侯需要儲存非零元素值的同時還要儲存非零元素在矩陣中的位置,即非零元素所在的行號和列號,也就是在儲存某個元素比如aij的值的同時,還需要儲存該元素所在的行號i和它的列號j,這樣就構成了乙個三元組(row,col,value)的線性表。

也可以用鍊錶實現,我這裡用的是線性表

#include

#include

using

namespace

std;

//稀疏矩陣的壓縮儲存及轉置

template

struct triple

};template

class coefficient_matrix

//二維陣列入順序表

coefficient_matrix(int* a,int row,int col,const

int invalid )

:_rowsize (row)

,_colsize (col)

,_invalid (invalid )}}

}void display()

else

}cout

//coefficient_matrixtransport()

//// index++;

// }

// }

// return tmp;

//}//快速轉置

coefficient_matrixfasttransport()

rowstart[0] = 0;

for (size_t i = 1; i < _colsize; ++i)

index = 0;

//tmp._a.reserve(_a.size());

tmp._a.resize(_a.size());

while (index < _a.size())

return tmp;

}protected:

vector

> _a;//結構體型別順序表,三元組:位置,value

int _rowsize;

int _colsize;

t _invalid;

};int main()

, ,

//,//,//,

//,//,

// };

coefficient_matrix cm1((int*)arr,3,4,0);

cm1.display ();

coefficient_matrix cm2=cm1.fasttransport ();

cm2.display ();

return

0;}

稀疏矩陣的壓縮儲存及轉置

稀疏矩陣的壓縮儲存及轉置 include using namespace std 三元組順序表的型別定義 define itemsize 100 typedef struct int row,col int item thnode typedef struct thnode data data 0 ...

稀疏矩陣的壓縮儲存與轉置

稀疏矩陣 m n的矩陣,矩陣中有效值的個數 遠小於無效值的個數,且這些資料的分布沒有規律。1 0 3 0 5 0 0 0 0 0 0 0 0 0 0 2 0 4 0 6 0 0 0 0 0 0 0 0 0 0 稀疏矩陣的壓縮儲存 壓縮儲存值儲存極少數的有效資料。使用 三元組儲存每乙個有效資料,三元組...

稀疏矩陣的壓縮儲存與轉置

稀疏矩陣的三元組表述法 型別結構 template struct triple template class sparsematrix 實現壓縮儲存 稀疏矩陣 template sparsematrix sparsematrix template sparsematrix sparsematrix ...