Data structures-arrays, matrices, generalized table storage

Source: Internet
Author: User

Definition of an array
    1. Definition of an array
      An array is a set of ordinal pairs that are made up of index and value values.
      In the array, each of the defined subscripts corresponds to a value, which is called an array element.
      Each order alignment is as follows: (Index,value)
Sequential representation and implementation of arrays

Because the memory structure of a computer is one-dimensional, so using one-dimensional memory to represent a multidimensional array, you must sort the array elements into a sequence of columns in some order, and then store the linear sequence in memory.
are generally used to represent arrays in sequential storage methods

    1. The order representation of one-dimensional arrays
      The storage address of the first array element a[0] is loc (a[0]), and if each array element is known to occupy a K storage unit, the storage address of the array element labeled I a[i] Loc (A[i]) is
      LOC (A[i]) =loc (a[0]) +i*k
      i=0,1,2,..., n-1.

    2. The sequential representation of two-dimensional arrays
      A two-dimensional array a[m][n] is mapped to one-dimensional storage space in the following order: row-First and column-first.
      Most languages, such as Pascal, BASIC, C, C + +, are stored in row-first order, and Fortran is stored in column-First order.

Compressed storage of matrices

Storage of matrices--two-dimensional arrays
Consider: How to improve storage efficiency if the matrix order is high and there are many elements with the same value or 0 elements?
Special matrices-elements with the same value or 0 elements have a certain regularity in the distribution of matrices
Sparse matrices--there are a large number of 0 elements in the matrix, less than 0 elements.
Compressed storage: Allocates only one storage space for multiple identical non-0 elements, and no space allocated to 0 elements.

Compressed storage of symmetric matrices
IfI≧J, the AIJ is in the lower triangular matrix. Before the AIJI-1Line (from section1Line to PageI-1Line) altogether1+2+...+I-1=I(I-1)/2Element, in the firstIOn the line, AIJ beforeJ-1Elements (i.e. AI0,AI1,AI2,..., aij-1), so there are: k=I*(I-1)/2+J-1WhenI≧JIfI<J, the AIJ is in the upper triangular matrix. Because of the Aij=aji, so as long as the exchange of the above-mentioned corresponding relationshipIAndJCan get: k=J*(J-1)/2+I-1WhenI<JAccording to the above subscript correspondence, for any element in the matrix AIJ, it is possible to uniquely determine its position k in the one-dimensional array sa; Conversely, for all k=0,1,..., N (n+1)/2-1, you can determine the SA[K]The position of the element in The matrix (I,J)。
Compression storage of triangular matrices

The repeating element C in the triangular matrix can share a storage space, the remaining elements just have n (n+1)/2, so the triangular matrix can be compressed into a one-dimensional array sa[n (n+1)/2+1], where c is stored in the 1th position of the array (also placed in the last position).
When the upper triangular matrix element aij is saved in the array SA, the corresponding relationship between the subscript value K and (I,j) is?
When the lower triangular matrix element aij is saved in the array SA, the corresponding relationship between the subscript value K and (I,j) is?

Storage of sparse matrices

WORKAROUND: While storing a non-0 element, note the location of the row and column where it is located (i, J).
Since the ternary group (I, J, AIJ) uniquely determines a non-0 yuan of matrix A. Therefore, the sparse matrix can be determined only by a ternary group representing a non-0 element and its number of rows.
For example, the following triples table
((1,2,12), (1,3,9), (3,1,-3), (3,8,4), (4,3,24), (4,6,2), (5,2,18), (6,7,-7), (7,4,-6)) and column information (7,8,9) can describe the sparse matrix shown in 5.6
Note: column information (7,8,9), 7: line; 8: columns; 9: Non-0-dollar number

Ternary Order Table

In order to represent ternary table in sequential storage structure, a compressed storage method of sparse matrix-ternary sequential table is obtained.

⑴ ternary node definition #define  max_size  typedef int  elemtype;     typedef struct  {int  row;      /* row subscript */        int  col;      /* column subscript */ elemtype value ; /* element value */} Triple;  
⑵  三元组顺序表定义    typedefstruct {   int  rn ;         /*   行数   */int  cn;         /*   列数   */int  tn ;         /*    非0元素个数   */Triple   data[MAX_SIZE] ; }TMatrix ; TMatrix  a;//定义了一个用三元组顺序表表示的稀疏矩阵
Transpose of the Matrix

The sparse matrix A is stored in the ternary table a.data by row priority, if simply exchanging the contents of I and J in A.data, getting the ternary table B.data,b.data will be a sparse matrix B stored in the order of precedence, and to get the b.data stored in the order of precedence, the ternary table must be rearranged. The order of the elements in the b.data.
Because the column of a is a row of B, therefore, by the a.data of the column sequence transpose, the resulting transpose matrix B of the ternary table B.data must be a row-first storage.

Algorithm of transpose matrix
The algorithm for finding the transpose matrix by method is as follows:void Transmatrix(TmatrixATmatrix* b) {int i, J, col;b->rn=a.cn; B->cn=a.rn; b->tn=a.tn;/* Set ternary table b->the number of rows, columns, and non-0 elements of data * /if(b->tn==0) printf (" the Matrix A=0\ n ");Else{j=0; Array subscript of ternary arrays in B for(col=1; col<=a.cn; col++)/* Each cycle scans A's Col column not $0, gets B's Col line not $0 */for (i=0; i<a.tn; i++)/* Cycle count is non-0Number of elements */if(A. Data[I].col==col]{b-> Data[j].row=a.data[I].col;B-> Data[j].col=a.data[I].row;B-> Data[j].value=a.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 is the number of matrix columnsB. RN=a. CN ; b.cn=a.rn; b.tn=a.tn ; /* Set the number of rows, columns, and non-0 elements of the b.data table */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<a.tn; ++k)++num[A. DataK. Col];           / * The number of non-0 elements per column in the original matrix * /for (cpot[1]=0, col=2 ; col<=a.cn; ++col)Cpot [col]=cpot[col-1]+num[col-1];      / * Ordinal number of the first non-$ 0 in the col column in B.data * /for (p=0 ; p<a.tn; ++p){col = a. Data[P]. Col ; Q=cpot[col];//The element where the nth line of Matrix B is locatedB. Data[Q]. Row=a. Data[P]. Col ;B. Data[Q]. Col=a. Data[P]. Row ;B. Data[Q]. Value=a. Data[P]. Value ; ++cpot[col]; /* Critical!! When a new element is added to this column, the position becomes the next non-0 element position * /}}}
Cross-linked list (chained storage)
    对于稀疏矩阵,当非0元素的个数和位置在操作过程中变化较大时,采用链式存储结构表示比三元组的线性表更方便。   矩阵中非0元素的结点所含的域有:行、列、值、行指针(指向同一行的下一个非0元)、列指针(指向同一列的下一个非0元)。其次,十字交叉链表还有一个头结点

A non-0 element of the same row in a sparse matrix is linked by the right pointer field to a list of rows linked by a down pointer field to a list of columns.
Each non-0 element is both a node in a list of rows and a node in a list of linked columns, and all non-0 elements form a cross-linked list called a cross-linked list.

此外,用两个一维数组rhead和chead分别存储行链表的头指针和列链表的头指针。
结点的描述如下:typedef struct OLnode { int row , col ; /* 行号和列号 */ elemtype value ; /* 元素值 */struct OLnode *down , *right ;} OLNode, *OLink ; /* 非0元素结点 */typedef struct{ int rn; /* 矩阵的行数 */ int cn; /* 矩阵的列数 */int tn; /* 非0元素总数 */OLink * rhead ; //行链表头指针向量基址OLink * chead ; //列链表头指针向量基址} CrossList 
Generalized tables

Generalized table is a generalization and extension of linear table, which is widely used in artificial intelligence field.
The 2nd chapter of the linear table is defined as n (n≧0) elements A1, A2,..., An of a poor sequence, all elements of the sequence have the same data type and can only be atomic (Atom). A so-called atomic term can be a number or a structure that is not re-divided in structure. The concept of generalized tables is produced by loosening this restriction on elements and allowing them to have their own structure.
A generalized table (Lists, also known as a list): is a poor sequence of n (n≧0) elements: ls= (A1,a2,...,an), where AI is either an atomic term or a generalized table. LS is the name of a generalized table, and n is its length. If the AI is a generalized table, it is called the sub-table of LS.
Customary: The atomic term is in lowercase letters, and the child table is in uppercase letters.
If the generalized table ls is not empty:
A1 (the first element in the table) is called a table header;
A sub-table consisting of the remaining elements is called a footer; (A2,a3,...,an)
The number of elements (including atoms and sub-tables) contained in a generalized table is called the length of the table.
The maximum number of brackets in a generalized table is called the table depth (degrees)

The elements of a generalized table can be atomic or sub-tables, and the elements of a child table can be child tables, .... That is, the generalized table is a multi-level structure. The Generalized table D in table 5-2 shows a graphical representation of 5-12.
Generalized tables can be shared by other generalized tables, or other generalized tables can be shared. Generalized tables share other generalized tables without having to list the values of the child tables, but by referencing them by table names. such as: d= (A,B,C)
The generalized table itself can be a recursive table. such as: e= (a,e)
According to the definition of the table header and footer, any non-empty generalized table header can be either an atom or a child table, and the footer must be a generalized table.

The storage structure of generalized tables

Because data elements in a generalized table have different structures, they are usually represented by a chained storage structure, with each data element represented by a node. Therefore, there are two types of nodes in a generalized table:
A class is a table node, which is used to represent generalized table items, which consists of a flag field, a table Head pointer field and a footer pointer field.
The other is the atomic node, which is used to represent the atomic term, which consists of a domain of flags and a range of atoms. As shown in 5-13.
As long as the generalized table is non-empty, it consists of a table header and a footer. That is, a definite table header and footer are the only ones that determine a generalized table.

相应的数据结构定义如下:typedefstruct GLNode{  int   tag ;     /*  标志域,为1:表结点;为0 :原子结点  */union{  Atomtype atom;     /* 原子结点的值域  */struct    {  struct GLNode  *hp , *tp ;     }ptr ;   /*  ptr和atom两成员共用  */}Gdata ; } GLNode ;      /* 广义表结点类型  */

Data structures-arrays, matrices, generalized table storage

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.