Learning data structure-Chapter 3: Stacks and queues (compressed storage of special matrices)

Compressed storage of special matrices

1.Matrix


In C language, we usually use two-dimensional arrays to store matrices.

2. Compressed storage of matrix

For example, we want to store the following student information in a matrix with 3 rows and 6 columns, but all the student information is the same, so we actually only need to store one.

In the following information, each row of information is the same. According to the above compression method, we only need three storage units to store all the information of the matrix.

  • Compressed storage: It means that 值相同only 一个storage space is allocated for multiple elements, but storage space is 零元素not allocated for multiple elements .
  • Special matrix: refers to a matrix with many identical matrix elements or zero elements, and 相同矩阵元素或零元素the distribution of these has a certain regularity.
  • Compressed storage of special matrices: find out the distribution law of matrix elements with the same value in the special matrix, and put those matrix elements with regular distribution and the same value on multiple matrix elements 压缩存储到一个存储空间.

Only special matrices can be compressed and stored.

3. Compressed storage of special matrices

3.1 Symmetric matrix

Symmetric matrix: 若对一个n阶方阵A[1...n][1....n][中的任意元素aij都有aij=aji(1 ≤ i, j ≤ n),则称其为对称矩阵。

As shown below:

In the matrix, we often turn the matrix into three areas:

  • i> j lower triangle area
  • i = j main diagonal
  • i <j upper triangle area

According to the characteristics of this matrix, we found that we only need to store: 下三角区域and 主对角线区域; our array was originally A[n][n], but now we only need B[n(n+1)/2]a storage unit, which achieves the purpose of compression.

This n(n+1)/2 is obtained by summing an arithmetic sequence, with one element in the first row, two elements in the second row, and three elements in the third row...

Specific storage order:

Now think: What is the subscript of aij in the compressed storage array?

answer:k=[i(i-1)/2] + j-1

Analysis: The total number of elements in the first i-1 row of the ij-th element is: 1, 2, 3, 4, 5...i-1, and the arithmetic sequence summation: i(i-1)/2one, in addition, iwe have jone element in the first row , So add it j, and finally we get the subscript, so subtract one 1, so the answer:k=[i(i-1)/2] + j-1

Note that we assume that the B array index here starts from 0

3.2 Triangular matrix

Triangular matrix: If the A[1...n][1...n] 上elements of the (lower) triangular area of an n-order square matrix are all the same constant, it is called the (upper) triangular matrix.

The compressed storage array B[[n/(n+1)/2]+1], where n(n+1)/2 is the same as the above symmetric matrix, +1is because we want to store the same constant C in the last storage unit of the array B.

Lower triangular matrix

Store by row first

Upper triangular matrix

Store by row first

Note that we assume that the B array index here starts from 0

3.3 Tridiagonal matrix

Tridiagonal matrix If for any element aij in an n-order square matrix A, when |ij|>1, there is aij=0 (1<=i,j<=n), then it is called a tridiagonal matrix.

In fact, in addition to the elements of the main diagonal and the elements on both sides, the value of the other elements are equal to 0, it is called tridiagonal matrices

Tridiagonal matrix compressed storage is still stored in row first

Now think: What is the subscript of aij in the compressed storage array?

Note that we assume that the B array index here starts from 0

answer:k=3(i-1)-1+ j-(i-1)+1 -1 = 2i+j-3

If k is known, find i, j?

answer:

  • i=[(k+1)/3+1]
  • j= k-2i+3

Explanation: k+1 means that the index of the array starts from 0, we find it is i, so we have to add 1, and then divide by three, representing how many elements are in the whole row of three, and then we know that there are only two elements in the first row , And then we itake one from the first row and add it to the first row. Then the inumber of elements in the first row will never be 3, so add 1 and go to the lower bound to get the row number.

Note: The [] here no longer represents parentheses, but represents an integral function, for example: [4/3] = 1, [8/3]=2

3.4 Sparse matrix

Sparse matrix: The number s of matrix elements is very large relative to the number t of non-zero elements in the matrix, that is, a matrix with s>>t is called a sparse matrix.

Much of what is said here is a vague concept. Use the following 三元组storage.


We use triples to compress and store sparse matrices. After sparse matrices are compressed and stored, they lose the characteristics of random storage.

4. Special matrix array

An array is n(n>=1)a finite sequence composed of data elements of the same type. Each data element is called an array element, and each element is constrained by n linear relationships. The sequence number of each element in n linear relationships is called the element And call the array an n-dimensional array.

Array is the promotion of linear table!

4.1 Dimensions of the array

Array is dimensional

Insert picture description here

4.2 The dimension and dimension of the array are immutable

Once the array is defined, its dimensions and dimensions 不可变, except for initialization and destruction of the array, are the only 存取元素和修改元素operations

4.3 The storage structure of the array

Using sequential storage, it is a block of continuous storage space.

Insert picture description here

Two-dimensional array storage order is
row first
Insert picture description here

LOC (a00) address represents the first element, and L is the size of each element, (n+1)each row represents from 0the ntotal n+1one element i*(n+1)*Lof i because the first irow, from 0- to the i-1total iline, so multiply i, add back on j*:Lbecause the first aij, represents the i+1first line of jelements, so add these elements to.

Rank first

Insert picture description here

Address LOC (a00) represents the first element in columns priority, first with the former 0----(j-1)address space columns: j*(n+1)*Lthen add the first jcolumn of this element of the space above, a total of 0----(i-1)elements is the ione, so:i*L

Welcome to follow the official account 理木客, insist on sharing original knowledge, more exciting waiting for you to discover, look forward to your attention
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41941875/article/details/106305097