A sparse matrix that is not processed is actually a special two-dimensional array in which most of the elements are 0 or other types of illegal values, with only a few non-0 elements.
To implement compressed storage, you can store only non-0 elements of a sparse matrix. When you store a non-0 element in a sparse matrix, you must store the element's row and column numbers and the element values. We can encapsulate a ternary group class to store these elements.
Ternary group Template<class t>struct triple{size_t _row; Line size_t _col; Column T _value; Value triple<t>::triple ()//define parameterless constructor {}triple (size_t row, size_t col,t value): _row (Row), _col (col), _value (value) { }};
creates a sparse matrix . Containers make it easy to store these elements, which is equivalent to storing them in a dynamic array. It is required to store in row-first order, so that when the sparse matrix is printed, the non-0 elements are printed sequentially in the order of rows.
Template<class t>//using containers to implement sparse matrix compression storage Sparsematrix<t>::sparsematrix (const t* Array, size_t row, size_t col, c Onst t& Invalid)//Initialize: _rowmatrix (Row), _colmatrix (col), _invalid (invalid) {for (size_t i = 0; i < _rowmatrix; ++i) { for (size_t j = 0; j < _colmatrix; ++j) {if (Array[i*col + j]! = invalid) {triple<t> cur (i, J, Array[i*col + j]); _a Rray.push_back (cur);}}}
Sequence Transpose method : The sequence of the matrix is transpose, so that after the transfer of the three-tuple container sequences are stored in line priority. Time Complexity of O (_colmatrix*_array.size ())
Template<class t>//sequencing transpose sparsematrix<t> Sparsematrix<t>::transport () {assert (_array.size ()!=0); sparsematrix<t> Ret;ret._rowmatrix = _colmatrix;ret._colmatrix = _rowmatrix;ret._invalid = _invalid;ret._ Array.reserve (This->_array.size ()); for (size_t j = 0; J < _colmatrix; J + +) {size_t index = 0;while (Index < _array . Size ()) {if (_array[index]._col = = j) {triple<t> TP (_array[index]._col, _array[index]._row, _array[index]._ value); Ret._array.push_back (TP);} index++;} if (this->_array.size () = = Ret._array.size ()) {break;}} return ret;}
Quick Transpose Method : Determine in advance the position of the first element in each column of the matrix in the container, and in the case of the sparse matrix transpose, through the traversal of the original container, the element is placed directly in the appropriate position of the new container. Time Complexity of O (_colmatrix+_array.size ())
Before transpose, determine the number of non-0 elements in each column of the original matrix, and then find the correct position of each column of non-0 elements in the new container.
Set two integer arrays Rowcounts[_colmatrix], Rowstart[_colmatrix] respectively to hold the number of non-0 elements in each column of the ternary container and the correct position of the first non-0 element in each column in the new container.
Rowstart[0] = 0; Rowstart[col] = Rowstart[col-1] + rowcounts[col-1];
| |
0 |
1 |
2 |
3 |
4 |
| rowcounts[col] |
2 |
0 |
2 |
0 |
2 |
| rowstart[col] |
0< /td> |
2 |
2 |
4 |
4 |
Template<class t>sparsematrix<t> sparsematrix<t>::fasttranaport () //Quick Transpose { ASSERT (_array.size () != 0);size_t index = 0; sparsematrix<t> ret;ret._rowmatrix = _colmatrix;ret._colmatrix = _rowmatrix; Ret._invalid = _invalid;ret._array.resize (_array.size ());int *rowcounts = new Int[_colmatrix];int *rowstart = new int[_colmatrix];memset (RowCounts, 0, _ colmatrix*sizeof (int)); memset (rowstart, 0, _colmatrix*sizeof (int));for (size_t i = 0; i < _array.size (); i++) {rowcounts[_array[i]._col]++;} rowstart[0] = 0;for (size_t i = 1; i < _colmatrix; i++) { ROWSTART[I]&NBSP;=&NBSP;ROWSTART[I&NBSP;-&NBSP;1]&NBSP;+&NBSP;ROWCOUNTS[I&NBSP;-&NBSP;1];} triple<t> tp;for (Size_t i = 0; i < _array.size (); i++) { Tp._rOw = _array[i]._col;tp._col = _array[i]._row;tp._value = _array[i]._value;ret._ ARRAY[ROWSTART[_ARRAY[I]._COL]++]&NBSP;=&NBSP;TP;} Delete [] rowcounts;delete [] rowstart;return ret;}
Complete code is included:
/********************************************************************//* Compact storage for sparse matrices realization of the ternary group of sparse matrices the use of container storage triples implementation of sparse matrix sequence transpose and fast transpose print sparse matrices by:lynn-zhang*//********************************************************************/#define _CRT_SECURE_NO_WARNINGS 1#include<iostream> #include <vector> #include <assert.h> using namespace std; #define &NBSP;ROW&NBSP;6#DEFINE&NBSP;COL&NBSP;5//Ternary Group template<class t> struct triple{size_t _row;size_t _col; t _value; Triple<t>::triple () {}triple (Size_t row, size_t col,t value): _row (Row), _col (col ), _value (value) {}};template<class t>class sparSematrix{public:sparsematrix (); // declares an argument-free constructor Sparsematrix (const t* array, size_t row, size_t col, const t& invalid); //constructor Initialization sparsematrix<t> transport (); //transpose Sparsematrix<t> fasttranaport (); //Quick Transpose void Display (); //print protected:vector<triple<t>> _array; // A sparse matrix size_t _rowmatrix; // sparse matrix row number size_t _ with container compression storage colmatrix; // Sparse matrix column number t _invalid; // Define illegal values} ; template<class t> //parameterless Constructor Sparsematrix<t>::sparsematrix () {}template< The class t> //constructor initializes the Sparsematrix<t>::sparsematrix (const t* array, Size_t row, size_t col, const t& invalid) //initialization: _rowmatrix (Row), _colMatrix (COL) , _invalid (invalid) {for (size_t i = 0; i < _rowmatrix; ++i) {for (SIZE_T&NBSP;J&NBSP;=&NBSP;0;&NBSP;J&NBSP;<&NBSP;_COLMATRIX;&NBSP;++J) {if (array[i*col + j] != invalid) {triple<t> cur (i, j, array[i*col + j]); _ Array.push_back (cur);}}} template<class t> //print void sparsematrix<t>::D isplay () //Print {assert (_ Array.size ()!=0);size_t index = 0;for (size_t i = 0; i < _rowmatrix; i++) {for (size_t j = 0; j < _colmatrix; ++j) {if (Index < _array.size () && _array[index]._row == i&&_ ARRAY[INDEX]._COL&NBSP;==&NBSP;J) {cout << _array[index]._value << " "; index++;} Else{cout << ' 0 ' << ' ';}} Cout << endl;} Cout << endl<<endl;} template<class t> //Transpose Sparsematrix<t> sparsematrix<t>::transport () { ASSERT (_array.size ()!=0); sparsematrix<t> ret;ret._rowmatrix = _colmatrix;ret._colmatrix = _rowmatrix; Ret._invalid = _invalid;ret._array.reserve (This->_array.size ());for (size_t j = 0; j < _colmatrix; j++) {size_t index = 0;while (index < _array.size ()) {if (_array[index]._col == j) {TRIPLE<T>&NBSP;TP (_array[index]._ Col, _array[index]._row, _array[index]._value); Ret._array.push_back (TP);} index++;} if (This->_array.size () == ret._array.size ()) {break;}} Return ret;} Template<class t>sparsematrix<t> sparsematrix<t>::fasttranaport () //Quick Transpose { ASSERT (_array.size () ! = 0);size_t index = 0; sparsematrix<t> ret;ret._rowmatrix = _colmatrix;ret._colmatrix = _rowmatrix; Ret._invalid = _invalid;ret._array.resize (_array.size ());int *rowcounts = new Int[_colmatrix];int *rowstart = new int[_colmatrix];memset (RowCounts, 0, _ colmatrix*sizeof (int)); memset (rowstart, 0, _colmatrix*sizeof (int));for (size_t i = 0; i < _array.size (); i++) {rowcounts[_array[i]._col]++;} rowstart[0] = 0;for (size_t i = 1; i < _colmatrix; i++) { ROWSTART[I]&NBSP;=&NBSP;ROWSTART[I&NBSP;-&NBSP;1]&NBSP;+&NBSP;ROWCOUNTS[I&NBSP;-&NBSP;1];} triple<t> tp;for (Size_t i = 0; i < _array.size (); i++) { tp._row = _array[i]._col;tp._col = _array[i]._row;tp._value = _array[i]._value; Ret._array[rowstart[_array[I]._COL]++]&NBSP;=&NBSP;TP;} Delete [] rowcounts;delete [] rowstart;return ret;} Int main () {int array[row][col] ={{ 1, 0, 3, 0, 5 },{ 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0 },{ 1, 0, 3, 0, 5 },{ 0, 0, 0, 0, 0 },{ 0, 0, 0, 0, 0 } }; SPARSEMATRIX<INT>&NBSP;SM ((int*) array, row, col, 0); Sm. Display (); Sparsematrix<int> sm1;sm1 = sm. Transport ();cout << "The matrix after transpose is: " &NBSP;<<&NBSP;ENDL&NBSP;<<&NBSP;ENDL;SM1. Display (); Sparsematrix<int> sm2;sm2 = sm. Fasttranaport ();cout << "fast-transpose matrix is: " << endl << endl;sm2 . Display (); System ("pause"); return 0;}
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7F/0E/wKiom1cRDD6B4qizAAAnq-ygG7I404.png "title=" capture. PNG "alt=" Wkiom1crdd6b4qizaaanq-ygg7i404.png "/>
This article from "Yan Anyang" blog, declined reproduced!
Compressed storage and transpose of sparse matrices