Data Structures-arrays

Source: Internet
Author: User

Differences between arrays and linear tables we should have learned the difference between arrays and linear tables before. They are the same:

(1) They are the same, they are a number of data elements of the same data type A0,a1,a2,..., a finite sequence of an-1

The difference between them:

(1) An array requires that its elements occupy a contiguous block of memory space, whereas a linear table has no such requirement
(2) The elements of a linear table are logically non-divided elements, and each element in the array can also be an array
(3) The operation of an array is primarily to store data in an array element of a subscript and to take an array element of a subscript, which differs from the INSERT and delete operations of a linear table

Definition of an array

The logical definition of an array is that an n (n>1)-dimensional array is a vector whose elements are n-1-dimensional arrays with the same upper and lower bounds.
The number of elements in an n-dimensional array
Set n-dimensional arrays as:
A[c1. D1, C2. D2, ..., CN.. DN]
The number of elements is:

-Arrays are logically generalized for linear structures
-the array is a linear structure with linear table elements, and the structure of the elements is the same
-Arrays can be considered as a set of pairs of subscripts and values
-Arrays are a logical structure

Array operations: Arrays generally do not insert and delete operations (less efficient)
There are generally only two types of operations for arrays:
(1) Given a set of subscripts, access to the corresponding data elements
(2) Given a set of subscripts, modify the value of the corresponding data element

Sequential storage of arrays

(1) One-dimensional array (n elements) of any element of the AI's memory unit address
LOC (AI) =loc (a0) +i*k
  
(2) A two-dimensional array of m rows n columns
LOC (AIJ) =loc (a00) + (I*N+J) *k
  
Note: The array elements in the C language use the method of storing the main sequence of the line, which is the order of precedence.

conventions for sequential storage by line order and column order

A storage image with "line order as the main order"


The storage location of any element A[i][j] in a two-dimensional array
LOC (I,J) = loc (0,0) + (B2XI+J) XL
Where: LOC (0,0) is called the base address or base

Set two-dimensional array a[c1. D1][c2. D2] where C1, c2, and D1, D2 are the lower and upper bounds of the subscript for the two-dimensional array A, each array element is a storage unit of L, and the first element A[C1][C2] is stored in LOC (C1,C2), then any element in the two-dimensional array a[i][j] (C1≤I≤D1, C2≤J≤D2) The storage location can be determined by the following formula:

In the C language, the subscript is zero-based, that is: a[0..d1][0..d2], the storage > location of the array element A[i][j] is:

LOC (i,j) =loc (0,0) + (i* (d2+1) +j) *l

element storage location for n-dimensional arrays

In an n-dimensional array, the location where the array elements are stored is determined:

The formula is as follows:

Representation method of three-dimensional array

LOC (I,j,k) = loc (L1,L2,L3) +
[(I-L1) * (U2-L2) * (U3-L3) + (J-L2) * (U3-L3) + (K-L3)]* L

If l1=l2=l3 = 0;

LOC (I,j,k) = loc (0,0,0) + (i * u2* U3 + J * U3 +k) *l

Compressed storage of matrices

The idea of compressing storage

When the order of the matrix is higher and some elements in the matrix have special properties, space-saving storage methods can be used (compressed storage)

Special matrices

An element with the same value or a non-0 element distribution has a certain regularity

(1) Symmetric matrix

If the elements in the N-order matrix A Meet the following properties:
Aij=aji 1≤i, J≤n
It is called a N-order symmetric matrix.

Analysis:
For a symmetric matrix, N2 elements can be compressed into n (n+1)/2 spaces
The element in the lower triangle (including the diagonal) in the main order of the row order
stored in a vector b[n (n+1)/2]:

Symmetry Matrix Features:
There is a one by one correspondence between Vector b[k] and element AIJ in the matrix:
The following press subscript from 0 to start discussion
Subscript Transformation Formula
Vector B[k] and the relationship between element AIJ in the matrix: deduced by I and J K:

(2) Triangular matrix
    什么是三角矩阵呢?

Triangular matrix:
The lower triangular matrix refers to n-order matrices with zero or constant elements in the upper triangle (excluding diagonal) of the matrix.

Note: The lower triangular matrix storage formula and the symmetric matrix store the following elements of the main diagonal are basically the same formula, just add an additional storage constant or zero storage space to

(3) Diagonal matrix


Three diagonal matrices: a total of 3n-2 non-0 elements, deposited in b[3n-2], elements in a one-dimensional array b
The corresponding relationship between subscript k and element subscript I and J in the matrix is:

K = 3 (i-1)-1 (lower left corner of main diagonal, i.e. i=j+1)
K = 3 (i-1) (on the main diagonal, i.e. i=j)
K = 3 (i-1) +1 (on the main diagonal, i.e. i=j-1)

By the above three-type, to be
K=2 (i-1) + (j-1) (1≤i,j≤n; 0≤k≤3n-1)

Sparse matrices

Less than 0 elements and irregular distribution

Sparse factor:
Assuming that the matrix of M row n columns contains T 0 elements, then
Sparse factor is δ= t/(m*n) <= 0.05

Why use sparse matrices?

The problem that arises when you represent a high-order sparse matrix in a two-dimensional array is the usual method:
(1) 0 value element takes up a lot of space
(2) Calculation of many and 0 values of the operation, in case of division, but also to determine whether the divisor is zero

The principle of problem solving:
(1) Save or Save as little as 0 value elements
(2) Minimizing operations with no practical significance
(3) easy to operate. That is, the element corresponding to the subscript value (I,J) can be found as quickly as possible to find a non-0 value element of the same row or column as quickly as possible
Note: When compressing storage, do not allocate storage space on 0 elements, only store non 0 in sparse matrix

Sequential storage structure-ternary table

Ternary Order Table

Ternary Group Type
#define  MAXSIZE  12500struct {     int   i,  j;   //该非零元的行下标和列下标     ElemType  e;   // 该非零元的值 } Triple;  

Sparse matrix Types

typedefstruct {     Triple  1];       int     mu, nu, tu; } TSMatrix;  
Chained storage structure-cross linked list
the structure of non-0-element nodes in the cross-linked list:

The structure type of the cross-linked list
typedef    struct   OLNode {    int i , j;    ElemType e;    struct OLNode *down, *right; }OLNode;*OLink;typedef   struct {   OLink   *rhead,  *chead ;  //行、列链表头指针   int     mu,   nu ,  tu  ;} CrossList;

Data Structures-arrays

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.