Data Structures and Algorithms - compressed memory matrix

[Overview]

Compressed storage matrix is ​​mainly aimed in particular and sparse matrices:

  • Special matrix: the matrix element in many of the same value and the distribution of a certain law.
  • Sparse Matrix: Matrix There are many zero elements.

The basic idea of ​​compression is stored:

  • A plurality of values ​​for the same elements allocated only one memory space.
  • Zero element does not allocate storage space. 

[Compression] matrix

1. symmetric matrix

The symmetric matrix characteristics: a [i] [j] = a [j] [i], it can be stored only in the lower triangular matrix portion.

There are lower triangular matrix n * (n + 1) / 2 elements, the elements in the rows may be stored in an array SA [n * (n + 1) / 2] in

Then for a symmetric matrix for the i-th row j-th element whose number is 1 + 2 + ... + i-1 + j, namely: i * (i-1) / 2 + j

Since the one-dimensional array index starts from 0, the index for which the one-dimensional array of k = i * (i + 1 ) / 2 + j-1

For the triangle element a [j] [i], since a [i] [j] = a [j] [i], corresponding to the access lower triangular element a [i] [j] to, i.e., : K = J * (. 1-I) / 2 +. 1-I

2. triangular matrix

Triangular matrix is ​​divided into an upper triangular matrix and the lower triangular matrix:

  • The upper triangular matrix: below the main diagonal are all constant c
  • Lower triangular matrix: All of the above the main diagonal is a constant c

1) lower triangular matrix

Storing the symmetric matrix lower triangular matrix is ​​similar, except that only the storage elements other lower outer triangle, but also a constant stored above the diagonal, because the same constant, and therefore can be a storage, so that a total storage n * ( n + 1) / 2 + 1 elements, these elements may be stored by row into an array SA [n * (n + 1) / 2 + 1] in.

Lower triangular matrix of any one of the elements A [i] [j] with the subscript k SA i in the array, the corresponding relation of j:

  • i>=j:k=i*(i-1)/2+j-1
  • i<j:k=n*(n+1)/2

2) upper triangular matrix

Storage and lower triangular upper triangular matrix similar to the matrix.

The upper triangular matrix of any one of the elements A [i] [j] with the subscript k SA i in the array, the corresponding relation of j:

  • i<=j:k=(i-1)*(2n-i+2)/2+j-i
  • i>j:k=n*(n+1)/2

3. diagonal matrix

对角矩阵,即除主对角线和它的上下方若干条对角线的元素外,所有其他元素都为零。

其压缩存储的方式有两种:一维数组压缩、二维数组压缩

1)一维数组压缩

对于 w 对角矩阵,其第 i 行第 j 列的元素序号为前 i-1 行元素个数+第 i 行元素个数,即:w+3*(i-w)+(j-i+w) = 2i+j-w

由于一维数组下标从 0 开始,那么元素 a[i][j] 在数组中的下标 k=2i+j-w-1

2)二维数组压缩 

如上图,对于对角矩阵,可将其转为二维数组存储。

对于一个 n*m 的 w 对角矩阵,可将其压缩到 m 行 w 列的二维数组中,a[i][j] 映射为 b[t][s],其映射关系为:

  • t=i
  • s=j-i+2

【稀疏矩阵】

1.三元组

将稀疏矩阵的非零元素对应的三元组所构成的集合,按行优先的顺序排列成线性表,则稀疏矩阵的压缩存储转化为三元组表的存储。

template<class T>
struct Element{
    int row,col;
    T item;
};

2.三元顺序表

采用顺序存储结构存储的三元组表即为三元顺序表,但要表示一个稀疏矩阵,还要在存储三元组表时存储矩阵的行数、列数、非零元素个数。

const int N=100;
template<class T>
struct sparseMatrix{
    Element data[N];
    int row,col,num;
};

3.十字链表

采用链式存储结构存储的三元组表即为十字链表,其具备链接存储的特点,其对矩阵运算操作较三元顺序表要方便许多。

十字链表存储稀疏矩阵的基本思想是:将每个非零元素对应的三元组存储为一个链表结点,结点由 5 个域组成,element 为数据域,存储非零元素对应的三元组,right、down 为指针域,分别指向同一行、同一列中的下一个三元组

template<class T>
class olNode{
public:
    int row,col;
    T element;
    olNode<T>* right,*down;
public:
    olNode(){right=NULL;down=NULL;};
};

 

 

 

发布了1871 篇原创文章 · 获赞 702 · 访问量 194万+

Guess you like

Origin blog.csdn.net/u011815404/article/details/89326064