C + + implementation of sparse matrix compression storage, transpose, fast transpose

Source: Internet
Author: User

/* Compression storage, transpose, quick transpose for sparse matrix */#include  <iostream>using namespace std; #include  <vector>/ /Ternary Group template<class t>struct triple{size_t _row;size_t _col; t _value; Triple (Size_t row = 0, size_t col = 0, const t& value  = t ()): _row (Row), _col (col), _value (value) {}};template<class t>class sparsematrix{ Public:sparsematrix (t* a = null, size_t m = 0, size_t n =  0, const t& invalid = t ()): _rowsize (M), _colsize (N), _invalid (invalid) {for   (size_t i = 0; i < m; ++i) {for  (size_t j = 0; &NBSP;J&NBSP;&LT;&NBSP;N;&NBSP;++J) {if  (a[i*n+j] != _invalid) {Triple<t> t;t._row  = i;t._col = j;t._value = a[i*n+j];_a.push_back (t);}}} Void display () {size_t index = 0;for  (Size_t i = 0; i < _rowsize; ++i) {for  (size_t j = 0; &NBSP;J&NBSP;&LT;&NBSP;_COLSIZE;&NBSP;++J) {if  (Index < _a.size () &&  (_a[index]._ Row == i) &&  (_a[index]._col == j)) {cout<<_a[index++]._value<< "   ";} else{cout<<_invalid<< " ";}} Cout<<endl;}} Matrix transpose   Time complexity is  o (number of valid data * Number of columns of the original matrix) Sparsematrix<t> transport () {sparsematrix<t> sm; sm._colsize = _rowsize;sm._rowsize = _colsize;sm._invalid = _invalid;for  ( Size_t i = 0; i < _colsize; ++i) {Size_t index = 0;while   (Index < _a.size ()) {if  (_a[index]._col == i) {Triple<t> t;t._row  = _a[index]._col;t._col = _a[index]._row;t._value = _a[index]._value;sm._a.push _back (t);} ++index;}} RETURN&NBSP;SM;} Quick transpose   time complexity O (withNumber of valid data + number of columns of the original matrix) Sparsematrix<t> fasttransport () {sparsematrix<t> sm;sm._rowsize =  _colsize;sm._colsize = _rowsize;sm._invalid = _invalid;int* rowcounts =  new int[_colsize];int* rowstart = new int [_colsize];memset (RowCounts,  0, sizeof (int) *_colsize); memset (rowstart, 0, sizeof (int) *_colsize); Size_t index  = 0;while  (Index < _a.size ()) {++rowcounts[_a[index]._col];++index;} for  (size_t i = 1; i < _colsize; ++i) {RowStart[i] =  ROWSTART[I-1]&NBSP;+&NBSP;ROWCOUNTS[I-1];} Index = 0;sm._a.resize (_a.size ());while  (Index < sm._a.size ()) {Triple<T>  t;t._row = _a[index]._col;t._col = _a[index]._row;t._value = _a[index]._ Value;sm._a[rowstart[_a[index]._col]] = t;++rowstart[_a[index]._col];++index;} Delete[] rowcounts;delete[]&NBSP;ROWSTART;RETURN&NBSP;SM;} protected:vector<triple<t>> _a;size_t _rowsize;size_t _colsize; t _invalid;}; Void test () {int array [6][5] = {{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}}; SPARSEMATRIX&LT;INT&GT;&NBSP;SM1 ((int*) array, 6, 5, 0); Sm1. Display (); cout<<endl;//sprsematrix<int> sm2 = sm1. Transport (); Sparsematrix<int> sm2 = sm1. Fasttransport (); sm2. Display ();} Int main () {Test (); return 0;}

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/80/64/wKioL1dABvaRi4wbAABcFjMN-ho272.png "title=" Qq20160521145257.png "alt=" Wkiol1dabvari4wbaabcfjmn-ho272.png "/>

This article is from the "zgw285763054" blog, make sure to keep this source http://zgw285763054.blog.51cto.com/11591804/1775680

C + + implementation of sparse matrix compression storage, transpose, fast transpose

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.