溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

C++ 數(shù)據(jù)結(jié)構(gòu)之對(duì)稱(chēng)矩陣及稀疏矩陣的壓縮存儲(chǔ)

發(fā)布時(shí)間:2020-10-13 20:02:58 來(lái)源:腳本之家 閱讀:209 作者:lqh 欄目:編程語(yǔ)言

對(duì)稱(chēng)矩陣及稀疏矩陣的壓縮存儲(chǔ)

1.稀疏矩陣

 對(duì)于那些零元素?cái)?shù)目遠(yuǎn)遠(yuǎn)多于非零元素?cái)?shù)目,并且非零元素的分布沒(méi)有規(guī)律的矩陣稱(chēng)為稀疏矩陣(sparse)。

  人們無(wú)法給出稀疏矩陣的確切定義,一般都只是憑個(gè)人的直覺(jué)來(lái)理解這個(gè)概念,即矩陣中非零元素的個(gè)數(shù)遠(yuǎn)遠(yuǎn)小于矩陣元素的總數(shù),并且非零元素沒(méi)有分布規(guī)律。

實(shí)現(xiàn)代碼:

//稀疏矩陣及其壓縮存儲(chǔ) 
#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)先存儲(chǔ)) 
 //列變行,從0列開(kāi)始,將列數(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.對(duì)稱(chēng)矩陣

實(shí)現(xiàn)代碼:

//對(duì)稱(chēng)矩陣及其壓縮存儲(chǔ) 
 
#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; //對(duì)稱(chēng)矩陣的行列大小 
}; 


 

 以上就是C++ 數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)稀疏矩陣與對(duì)稱(chēng)矩陣,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI