Compressed storage of advanced (second) matrix (sparse matrix) of data structure

Source: Internet
Author: User

compressed storage of matrices (sparse matrices) Note

This article reproduces the address:

http://blog.163.com/zhoumhan_0351/blog/static/39954227201001112526244/

In order to save storage space and speed up processing, this kind of matrix needs to be compressed, and the principle of compressing storage is that the same elements are not stored repeatedly; 0 value elements are not stored.

first, related concepts ㈠ Special matrices

In the matrix, most of the values are the same, or not 0, and the distribution in the matrix has a certain regularity.

⒈ symmetric matrices

The elements in the matrix meet

Aij=aji 1≤i,j≤n

⒉ triangle Matrix

The upper (lower) triangular matrix refers to the lower (upper) triangular (excluding diagonal) elements of the matrix that are all n-order matrices of constant C or 0.

⒊ diagonal matrix (banded matrix)

All non-0 elements in the matrix are centered in the area centered on the main diagonal.

㈡ sparse matrices

The non-0 elements are very few (≤5%) and the distribution is irregular.

second, storage structure and algorithm thought 1. Symmetric matrix

Storage allocation policy: Each pair of symmetric elements is allocated only one storage unit, that is, only the lower triangle (including the diagonal) of the element, the required amount of space is: N (n+1)/2.

Storage Allocation method: Use one-dimensional array sa[n (N+1)/2] as the storage structure.

The correspondence between Sa[k] and AIJ is:

2. Triangular matrix

It is also an n-order phalanx with upper and lower triangular matrices. The lower (upper) triangular matrix is an n-order matrix of zero above the main diagonal (bottom) element. With a one-dimensional array sb[0..n (N+1)/2] as the storage structure of the n-order triangular matrix B, still using the row storage scheme, there is still a correspondence between any element in B, Bi,j and sb[k], but it is necessary to add a storage space of constant c. For example, in the lower triangular matrix, the constants are stored in N (n+1)/2 locations.

The compression storage of special matrices is essentially the arrangement of some elements in a two-dimensional matrix into a one-dimensional array according to a scheme, and different arrangement schemes correspond to different storage schemes.

3. Sparse matrix

The common three-tuple notation, the two-tuple notation with auxiliary line vectors (also the sequential table of logical lists), the cross-linked list notation, and so on.

1) Ternary representation method

The ternary representation is to store the row subscript and column subscript corresponding to the element at the same time that it stores a non-0 element. Each non-0 element in a sparse matrix is uniquely determined by a ternary group (I,J,AIJ). All non-0 elements in the matrix are stored in an array of ternary groups.


In this case, the data field represents a ternary group that is not 0 yuan, which is arranged in the main order of the row order.

Here is how to use ternary notation to realize the transpose of the Matrix.

(1) According to the order of three triples in B.data, we find the corresponding ternary group to transpose in A.data. In other words, transpose by the column of the Matrix M. In order to find all of the non-0 elements in each column of M, it is necessary to scan its ternary table a.data from the first line. Since the a.data is in the order of M to store each of the non-0 yuan, the resulting is exactly the b.data should be ordered.

third, storage structure and C language description 1. Ternary group notation

(1) According to the order of the rows in the B matrix, we find the corresponding ternary group in A.data to transpose.

(2) Quick transpose: According to the A.data in the order of the three groups transpose, and the transferred ternary group into the b.data in the appropriate position.

The proper location is determined by first calculating the number of non-0 elements in each column of the M-matrix (that is, each row of T), and then obtaining the position of the first 0-yuan in the b.data of each column of the M-matrix.

Basic idea of the algorithm:

Set two vectors:

Num[col]: The number of non-0 elements in the col column.

Cpot[col]: Nth column The first non-$ 0 in the proper position in the b.data.

During the transpose process, indicates the column's next non-$ 0 position in B.data.

1, Num[col] calculation:

Sequential scan A.data Ternary Group, the total number of columns is not 0 yuan.

2, Cpot[col] calculation:

C language implementation of the algorithm

#include "stdio.h" #include "stdlib.h" #define MAXSIZE 12500#define OK 1typedef int elemtype;typedef struct{int i,j; Elemtype e;}   Triple;typedef struct{triple Data[maxsize+1];int Mu,nu,tu; Number of rows of matrices, number of columns, and number of non-0}tsmatrix;int cpot[maxsize+1],num[maxsize+1];int Transposesmatrix (Tsmatrix m,tsmatrix &T) {T.mu= m.nu; T.nu=m.mu;   T.tu=m.tu;if (t.tu) {int q=1;                 for (int col=1;col<=m.nu;++col) for (int p=1;p<=m.tu;++p) if (M.data[p].j==col)                 {T.DATA[Q].I=M.DATA[P].J;                 T.DATA[Q].J=M.DATA[P].I;                 T.DATA[Q].E=M.DATA[P].E;                 ++q; }//if}//ifreturn OK;} Transposesmatrixint Inputm (Tsmatrix &m) {printf ("Input nu mu tu (with a space interval) of a matrix:\n"); scanf ("%d%d  D ", &m.nu,&m.mu,&m.tu); Row,colume,and tuprintf ("Please input the data of matrix:\n"); for (int c=1;c<=m.tu;c++) {scanf ("%d", &m.data[       C].I);       scanf ("%d", &AMP;M.DATA[C].J); scanf ("%d",&AMP;M.DATA[C].E); }//forreturn 1;} Inputint Printm (Tsmatrix T) {printf ("Matrix after transpose is:\n"), for (int c=1;c<=t.tu;c++) {printf ("%d%d%d\n   ", T.DATA[C].I,T.DATA[C].J,T.DATA[C].E); }//forreturn 1;} Inputint Fasttransposesmatrix (Tsmatrix m,tsmatrix &t) {t.mu=m.nu; T.nu=m.mu;  T.tu=m.tu;if (T.tu) {for (int col=1;col<=m.mu;++col) num[col]=0; for (int t=1;t<=m.tu;++t) ++NUM[M.DATA[T].J];  The number of M.DATA[T].J//not 0 Yuan Cpot[1]=1;  The number of the first non-0-dollar ordinal in B.data (T) in the Col column for (int col=2;col<=m.mu;++col) CPOT[COL]=CPOT[COL-1]+NUM[COL-1];          for (int p=1;p<=m.tu;++p) {int col=m.data[p].j;         int Q=cpot[col];         T.DATA[Q].I=M.DATA[P].J;         T.DATA[Q].J=M.DATA[P].I;         T.DATA[Q].E=M.DATA[P].E;      ++cpot[col]; }//for}//ifreturn OK;} Fasttransposesmatrixint Main () {Tsmatrix m,t;inputm (M);//transposesmatrix (m,t); Fasttransposesmatrix (m,t); Printm (T); return OK;}
American and American Pictures

Compressed storage of advanced (second) matrix (sparse matrix) of data structure

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.