Data structure review - Chapter 4: Arrays and matrices


Part One: Matrix

Matrix has been understood in detail in linear algebra. In the postgraduate entrance examination, the matrix part is often examinedarray subscript k and The relationship between matrix row i and column j.
It should be noted that the matrix subscripts i and j are usually: 1 to n 1 to n 1 to n, and the array subscript k is usually: 0 to n 2 − 1 0 to n^2-1 0arrivedn21

  • The relationship between k and i, j is: k = n ∗ ( i − 1 ) + j − 1 k=n*(i-1)+j-1 < /span>k=n(i1)+j1

Part 1 Exercises

  1. In array A, the length of each element is 3 bytes, the row subscript i ranges from 1 to 8, the column subscript j ranges from 1 to 10, and is stored continuously in the memory starting from the first address SA. The array is stored in rows, and the starting address of element A[8][5] is (C).
    A.SA+141
    B.SA+144
    C.SA+222(It can be found using the formula A 85 A_{85} A85There are 74 elements in front of , so its starting address is: S A + 74 ∗ 3 SA+74*3 SA+743
    D.SA+225
  2. Assume that the two-dimensional array A[1...m,1...n] is stored in the array B row by row, then the subscript of the two-dimensional array element A[i,j] in the one-dimensional array B is (< /span> D.i*m+i-1 C.i*(j-1) (Confusing option, "- j" must be wrong) B.n*(i-1)-j-1 (The question does not emphasize that the subscript of array B starts from 0. If combined with other options, you can only choose A) A.n*(i-1)+j). A




Part 2: Symmetric matrices

The symmetric matrix isthe data on both sides of the main diagonal are the samesquare matrix. For this For class matrices, we can usually save space and only store the data of the upper triangular part or the lower triangular part The data of .

  • At this timearray subscript k and matrix row i and column j< The relationship of a i=4> is usually satisfied (the upper triangle becomes the lower triangle after transposition): k lower triangle = S i − 1 + j − 1 k upper triangle = S j − 1 + i − 1 k_{Lower triangle}=S_{i-1}+j-1\\k_{Upper triangle}=S_{j-1}+i-1 kLower triangle=Si1+j1kupper triangle=Sj1+i1
  • In the expression before the array stores the ordinary matrix S x = x ∗ n S_x= x*n Sx=xn, n is the total number of columns of the matrix; and here because the upper/lower triangular matrix is ​​stored, so combiningthe arithmetic sequence summation formula: S x = x ∗ ( x + 1 ) 2 S_{x}=\frac{x*(x+1)}{2} Sx=2x(x+1)

It should be noted that in order to achieve the effect of transposition, the upper triangular matrix needs to be stored in columns into the array (when transposing, the columns will become row). Combining the above two formulas, the following relationship can be obtained:
Array subscript of symmetric matrix

Part 2 Exercises

  1. Suppose there is a symmetric matrix A, which adopts compressed storage method. A11 is stored in row order as the first element. Its storage address is 1. Each element occupies an address space. Then the address of A85 is (< /span> D.40 C.18 (Number of elements in front: k=8*(8-1)/ 2+5-1=32, the address is: 1+k*1=33) B.33 A.23 ). B




Part 3: Triangular Matrix

Triangular matrices are divided intolower triangular matrices andupper triangular matrices, need to be distinguished from the upper and lower triangular matrices of symmetric matrices.

  • Lower triangular matrix: the lower triangle isweight, and the upper triangleare all the same (e.g. all 0 or 1)
  • Upper triangular matrix: the upper triangle isweight, and the lower triangleare all the same (e.g. all 0 or 1)

At this time, in addition to storing the weights in the upper/lower triangle area, the array also needs to add a position to record the remaining same data (just save a copy).

  • The array subscript corresponding to the element in the lower triangular matrix: { k weight area = i ∗ ( i − 1 ) 2 + j − 1 ( i ≥ j ) k is the same Area = n ∗ ( n + 1 ) 2 ( i < j ) \begin{cases}k_{weight area}=\frac{i*(i-1)}{2}+j-1&(i \ geq j)\\\\k_{Same area}=\frac{n*(n+1)}{2}&(i < j)\end{cases} ktermination area=2i(i1)+j1kHomologous area=2n(n+1)iji<j
  • The array subscript corresponding to the element in the upper triangular matrix: { k weight area = j ∗ ( j − 1 ) 2 + i − 1 ( i ≤ j ) k is the same Area = n ∗ ( n + 1 ) 2 ( i > j ) \begin{cases}k_{weight area}=\frac{j*(j-1)}{2}+i-1& (i \ leq j)\\\\k_{Same area}=\frac{n*(n+1)}{2}&(i > j)\end{cases} ktermination area=2j(j1)+i1kHomologous area=2n(n+1)iji>j

The maximum subscript of the weight area is: k n n = n ∗ ( n − 1 ) 2 + n − 1 = n ∗ ( n + 1 ) 2 − 1 ( i = j = n ) k_{nn}=\frac{n*(n-1)}{2}+n-1=\frac{n*(n+1)}{2}-1 \quad(i =j=n) knn=2n(n1)+n1=2n(n+1)1i=j=n

Part 3 Exercises

  1. If the n-order lower triangular matrix A is compressed and stored in a one-dimensional array B[1...n(n+1)/2+1] in column-major order, then the non-triangular matrix A stored in B[k] The corresponding relationship between the subscripts i, j and k of the zero element aij (1<=i, j < =n) is (B).
    A.(j-1)(2n-j+1)/2+i-j
    B.(j-1)(2n-j+2) /2+i-j+1 (The question says that the subscript of array B starts from 1, so there is no need to subtract 1)
    C.(j- 1)(2n-j+2)/2+i-j
    D.(j-1)(2n-j+ 1)/2+i-j-1
    [Analysis] The lower triangle is compressed in column-major order and The upper triangle is compressed in row-major order< a i=12> is the same, and the reverse summation of the arithmetic sequence needs to be performed. The specific process is shown in the figure below: (The figure shows the lower triangle of this question being compressed by columns. The upper triangle is required to be compressed by rows and only needs to be transposed - interchanged. i and j positions)
    Lower triangular column compression derivation

Part 4: Tridiagonal Matrix

Tridiagonal matrix can also be called "claw" matrix or band matrix. Its characteristic is that when ∣ i − j ∣ > 1 |i-j|>1 ij>1 A i j = 0 A_{ij}=0 Aij=0
Tridiagonal matrix structure and compressed subscript k

Part 4 Exercises

  1. There is a tridiagonal matrix of order 100 M M M,其元素 m i j m_{ij} mij (1<= i, j<=100) is compressed row-first and stored in a one-dimensional array whose subscript starts from 0 N N N中。元素 m 30 , 30 m_{30,30} m30,30中中的下标识(B).
    A.86
    B.87 (mantle formula: 2*30+30-3=87) D.89
    C.88

Part 5: Sparse Matrix

When the number ofnon-zero elements in the matrixis much less thanthe number of matrix elements When a>, we call it a sparse matrix.
There are two general compression storage methods for sparse matrices: Triples and Cross linked list. The following mainly introduces triples, and the cross linked list will be introduced in the figure part.
Triplet corresponding to sparse matrix
The triplet seems to be a redundant transformation of one matrix into another matrix, but when the original matrix has a very large dimension but is a sparse matrix, the triplet can be very effective. compressed data, and the triplet itself can be further compressed into a one-element array.
It should be noted that the sparse matrix loses its random access characteristics after being compressed and stored.

Part 5 Exercises

  1. Sparse matrix—There are two general compression storage methods, namely (C).
    A. Two-dimensional array and three-dimensional array
    B. Triplet and hashing
    C. Triplet and cross Linked list
    D. Hash and cross linked list

  2. has a 100×90 sparse matrix with 10 non-0 elements. Assume that each integer occupies 2 bytes. When compressing the sparse matrix into the triple table data, we use data[ 0].i, data[0].j, data[0].v respectively store the number of rows, columns and non-zero elements of the matrix. The total number of bytes required is ( B).
    A.20
    B.66 (There is a trap in the description of the question. The total row is stored in the array subscript 0. number, total number of columns, total number of non-zero elements, a total of 6 bytes are consumed)
    C.18000
    D.33


summary

The matrix part mainly includes four types of matrices. The inspection point is that when matrix data is compressed into an array, the array subscripts corresponding to different matrix elements do not account for a high proportion of scores.
Matrix mind map

Guess you like

Origin blog.csdn.net/qq_50571974/article/details/126633835