Data structure, data structure and Algorithm

Source: Internet
Author: User
Tags array definition

Data structure, data structure and Algorithm
Array definition sequence expression and implementation of Arrays

Because the memory structure of the computer is one-dimensional, one-dimensional memory is used to represent multi-dimensional arrays, and array elements must be arranged in a certain order into a column sequence, then the linear sequence is stored in the memory.
Arrays are usually represented by sequential storage.

Matrix compression storage

Storage of Matrices-two-dimensional arrays
Consider: how to improve storage efficiency if there are many elements with the same or zero values in the matrix order?
Special Matrix-the distribution of elements with the same values or zero elements in the matrix is regular.
Sparse Matrix-a matrix contains a large number of zero elements, but few non-zero elements.
Compressed Storage: Only one storage space is allocated for multiple identical non-zero elements, and no space is allocated for zero elements.

Symmetric matrix compression and storage
If I have j, then aij is in the lower Triangle Matrix. Aij before the I-1 line (from 1st to the I-1 line) a total of 1 + 2 +... + I-1 = I (I-1)/2 elements, on line I, aij just had J-1 elements (ai0, ai1, ai2 ,..., Aij-1), so there are: k = I * (I-1)/2 + J-1 when I'm j if I <j, then aij is in the upper triangle matrix. Because aij = aji, as long as I and j in the above correspondence relationship are exchanged, k = j * (J-1)/2 + I-1 can be obtained. When I <j According to the subscript correspondence, any element in the matrix, aij, can be uniquely identified in the one-dimensional array sa position k; otherwise, for all k = ,..., N (n + 1)/2-1 can determine the position of the elements in the sa [k] in the matrix (I, j ).
Triangular matrix compression and storage

Duplicate element c in the Triangle Matrix can share a bucket, and the remaining elements exactly have n (n + 1)/2. Therefore, the triangle matrix can be compressed and stored in the one-dimensional array sa [n (n + 1)/2 + 1], where c is stored in the 1st position of the array (or in the last position ).
When the upper Triangle Matrix Element aij is saved in the array sa, what is the correspondence between the lower mark value k and (I, j?
When the lower Triangle Matrix Element aij is saved in the array sa, what is the correspondence between the lower mark value k and (I, j?

Storage of sparse Matrices

Solution: while storing a non-zero element, write down its row and column location (I, j ).
The triplet (I, j, aij) uniquely identifies A non-zero element of matrix. Therefore, the sparse matrix can be uniquely identified by the triplet representing a non-zero element and the number of its columns.
For example
(, 12), (, 9), (,-3), (, 4), (, 24), (, 2 ), (5.6, 18), (,-7), (,-6), and column information (, 9) can describe the sparse matrix shown in.
Note: In the row and column information (, 9), 7: rows; 8: columns; 9: Number of non-zero elements

Triple sequence table

To represent a triple table in a sequential storage structure, a compression and storage method of the sparse matrix-ternary ordered table is obtained.

(1) Triple node definition # define MAX_SIZE 1000 typedef int elemtype; typedef struct {int row;/* row subscript */int col;/* column subscript */elemtype value; /* element value */} Triple;
(2) triples sequence table defines typedef struct {int rn;/* number of rows */int cn;/* Number of columns */int tn; /* Number of non-0 elements */Triple data [MAX_SIZE];} TMatrix; TMatrix a; // defines a sparse matrix represented by a Triple sequence table
Transpose a Matrix

If sparse matrix A is set, data is compressed by row first and stored in triple Table. in data, if. the content of I and j in data to obtain the triple table B. data, B. data is a sparse matrix B stored in column-first order. To obtain B stored in row-first order. data, you must rearrange the triple table B. the sequence of elements in data.
Because column A is a row of Column B, the data is transposed in the column order of column A. data, and the triple Table B. data of the transpose matrix B must be preferentially stored by row.

Transpose matrix algorithm
The following algorithm is used to calculate the transpose matrix based on Method 1: void TransMatrix (TMatrix a, TMatrix * B) {int I, j, col; B-> rn = a.cn; b-> cn =. rn; B-> tn =. tn;/* set the number of rows, columns, and non-zero elements of the triple table B-> data */if (B-> tn = 0) printf ("The Matrix A = 0 \ n"); else {j = 0; // The subscript of The triple array in B for (col = 1; col <= a.cn; col ++)/* scan the non-zero element of column col of a cyclically, and obtain the non-zero element of row col of Column B */for (I = 0; I <. tn; I ++)/* number of cycles of non-0 elements */if (. data [I]. col = col) {B-> data [j]. row =. data [I]. col; B-> data [j]. col =. data [I]. row; B-> data [j]. value =. data [I]. value; j ++ ;}}}
Fast transpose Algorithm
The fast transpose algorithm is as follows: void FastTransMatrix (TMatrix a, TMatrix B) {int p, q, col, k; int num [N], copt [N]; // N indicates the matrix column number B. rn = a.cn; B .cn =. rn; B. tn =. tn;/* Set triple table B. the number of rows, columns, and non-zero elements of data */if (B. tn = 0) printf ("The Matrix A = 0 \ n"); else {for (col = 1; col <= a.cn; ++ col) num [col] = 0;/* vector num [] initialized to 0 */for (k = 0; k <. tn; ++ k) ++ num [. data [k]. col];/* calculate the number of non-0 elements in each column in the original matrix */for (cpot [1] = 0, col = 2; col <= a.cn; ++ col) cpot [Col] = cpot [col-1] + num [col-1];/* calculate the first non-0 RMB in the col column in B. serial number */for (p = 0; p <. tn; ++ p) {col =. data [p]. col; q = cpot [col]; // The Position of the element in the col row of matrix B. data [q]. row =. data [p]. col; B. data [q]. col =. data [p]. row; B. data [q]. value =. data [p]. value; ++ cpot [col];/* is crucial !! When a new element is added to this column, the position is changed to the position of the next non-zero element */}}}
Cross Linked List (chain storage)
For a sparse matrix, when The number and position of non-zero elements change greatly during the operation, it is easier to use a chain storage structure to represent a linear table than a triple. The nodes with non-zero elements in the matrix contain rows, columns, values, and row pointers (pointing to the next non-zero element of the same row), column pointer (pointing to the next non-0 yuan in the same column ). Second, the cross-linked list also has a header node.

The non-zero element of the same row in the sparse matrix is linked by the right pointer field into a row linked list, and the down pointer field links into a column linked list.
Each non-zero element is a node in a row-linked list and a node in a column-linked list. All non-zero elements constitute a cross-linked list, which is called a cross-linked list.

In addition, two one-dimensional arrays rhead and chead are used to store the head pointer of the row linked list and the head pointer of the column linked list respectively.
Node Description: typedef struct OLnode {int row, col;/* row number and column number */elemtype value;/* element value */struct OLnode * down, * right ;} OLNode, * OLink;/* Non-0 Element Node */typedef struct {int rn;/* Number of matrix rows */int cn;/* Number of matrix columns */int tn; /* Total Number of non-0 elements */OLink * rhead; // base address of the row-linked list header pointer vector OLink * chead; // column-linked list header pointer vector base address} CrossList
Generalized table

Generalized table is a linear table that is widely used in the field of artificial intelligence.
The linear table in Chapter 2nd is defined as n (n Limit 0) elements a1, a2 ,..., An has a finite sequence. All elements in this sequence have the same data type and can only be atomic items (Atom ). The so-called Atomic term can be a number or a structure, which cannot be further divided. If the restriction on elements is relaxed and they have their own structure, the concept of generalized tables is introduced.
Generalized table (Lists, also called list): a finite sequence composed of n (n Limit 0) elements: LS = (a1, a2 ,..., An) Where ai is an atomic term or a generalized table. LS is the name of a generalized table, and n is its length. If ai is a generalized table, it is called a subtable of LS.
Habits: Atomic items use lower-case letters, and child tables use upper-case letters.
If the generalized table LS is not empty:
◆ A1 (the first element in the table) is called the header;
◆ The child table composed of other elements is called the end of the table; (a2, a3 ,..., An)
◆ The number of elements (including atoms and sub-tables) contained in a generalized table is called the table length.
◆ The maximum number of layers in brackets in a generalized table is called the depth (degree) of the table)

The element of a generalized table can be an atomic item or a sub-table, and the element of a sub-table can be a sub-table ,.... A generalized table is a multi-level structure. Figure 5-12 of generalized table D in Table 5-2 is shown.
A generalized table can be shared by other generalized tables or other generalized tables. When a generalized table shares other generalized tables, it does not need to list the values of the subtables. Instead, it is referenced by the table name. For example, D = (A, B, C)
A generalized table itself can be a recursive table. E = (a, E)
According to the definition of the header and end of the table, the header of any non-empty generalized table can be an atom or a subtable, and the end of the table must be a generalized table.

Storage Structure of generalized tables

Because data elements in a generalized table have different structures, they are usually represented by a chain storage structure. Each data element is represented by a node. Therefore, a generalized table has two types of nodes:
◆ One type is a table node, which is used to represent generalized table items. It consists of a flag domain, a header pointer field, and a table tail pointer field;
◆ The other type is an atomic node used to represent an atomic entry. It consists of a flag domain and an atomic value range. As shown in Figure 5-13.
As long as the Generalized table is not empty, it is composed of the header and the end of the table. That is, a definite header and the end of the table uniquely determine a generalized table.

The corresponding data structure is defined as follows: typedef struct GLNode {int tag;/* flag field, 1: Table node; 0: Atomic node */union {Atomtype atom; /* value field of the atomic node */struct {struct GLNode * hp, * tp;} ptr;/* The two members of ptr and atom share */} Gdata;} GLNode; /* generalized table node type */

Related Article

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.