Compressed storage and transpose algorithm for "data structure" sparse matrices (c + + code)

Source: Internet
Author: User

definition of a sparse matrix

Matrix is now a lot of scientific and engineering computing problems commonly used in the mathematical object, the matrix involved in the calculation of the matrix is usually a higher order than the number of 0 elements is relatively few cases, therefore, we need a way to compress this relatively sparse matrix.

The first question, then, is how to define whether a matrix is sparse. Refer to the data structure textbook of Min, page 96th gives the definition of sparse matrix: Suppose in the matrix of MXN, t element is Non-zero, so δ=t/(m+n), called Δ is the sparse factor of matrix. It is generally assumed that δ<=0.05 is called a sparse matrix.


compressed storage of two matrices

So, how to compress and store the sparse matrix.

The concept of compressed storage, as long as the storage matrix of 0 elements of information is good, but also need to ensure that the information stored according to the unique ability to determine a matrix. Therefore, using the ternary ancestor (row, column, element value) plus the row and column values of the matrix can solve this problem. The different representation methods of ternary group can lead to different compression storage methods of sparse matrices, such as: Ternary sequence table, sequential table of logical link, and cross linked list. In this paper, only three-dimensional sequential table is discussed.

The storage of the ternary sequential table is represented as follows:

#define MAXSIZE 12500
typedef struct {
	int		i,j;	The line subscript and column subscript elemtype E of the element	;	Element value
}triple;
typedef struct{
	Triple	data[maxsize+1];	Ternary group int mu,nu,tu of non 0 elements	;		The number of rows, columns and non 0}tsmatrix of the matrix
;

the transpose algorithm of three matrices

The conversion of matrices is one of the simplest matrix operations, and then two kinds of matrix transpose algorithms are introduced in the form of sparse matrices stored as ternary groups. Start with the first simple transpose algorithm, and with pseudo code, the end of the article with a running C + + code, followed by the addition of Java code.

To obtain a matrix transpose matrix, we first analyze the difference between the initial matrix and the ternary group of the transpose Matrix, where the assumption is that the matrix is stored in the order of the main order, as shown in the following figure.


A to become B only need to go through 3 steps: (1) to exchange the values of the matrices, (2) to replace the i,j in each ternary group, and (3) to reorganize the order between the ternary groups. The first two are very easy to do, the key is how to achieve the third article.

(1) Here is the first solution, which leads to the first algorithm: according to the B.data in the order of the ternary group to find A.data in the corresponding ternary group to transpose. That is, according to the sequence of the initial matrix for the transpose operation, the text description may be ambiguous, look at the following picture can be understood.


Pseudo code is as follows:

Status Transposesmatrix (Tsmatrix M, Tsmatrix &t) {
	t.mu = m.nu; t.nu = m.mu; T.tu = m.tu;
	if (t.tu) {
		q = 1;
		for (col = 1; Col <= m.nu; ++col)
		{for
			(p = 1; p <= m.tu; ++p)
				if (m.data[p].j = = col)
				{
					T.data [Q].I = M.DATA[P].J;
					T.DATA[Q].J = m.data[p].i;
					T.DATA[Q].E = M.DATA[P].E;
					++q
				}
			
		}
		Return Ok
	}
} Transposesmatrix

(2) There is another solution, that is, according to the order of the elements in the A.data transpose, for each element in the A.data, to find the location in the B.data, the difficulty lies in how to find the A.data elements in the b.data position. In order to determine these locations, a preprocessing is done before the transpose, and the number of non 0 elements of each column in the initial matrix is first obtained, and then the position of the first non 0 element of each column in the b.data is calculated, which can be computed in advance. Therefore, you need to append num and cpot two vectors, Num[col] represents the number of 0 elements in the col column of the Matrix, Cpot[col] represents the appropriate position of the first non-0 element in the Col column in the Matrix, with the following formula:


Pseudo code is as follows:

Status Fasttransposesmatrix (Tsmatrix M, Tsmatrix &t) {
	t.mu = m.nu; t.nu = m.mu; T.tu = m.tu;
	if (t.tu) {for
		(col = 1; Col <= m.nu; ++col) Num[col] = 0;
		for (t = 1; t <= m.nu; ++t) ++NUM[M.DATA[T].J];
		CPOT[1] = 1;
		for (col = 2; col <= m.nu; ++col) Cpot[col] = cpot[col-1]+num[col-1];
		for (p = 1; p <= m.nu; ++p)
		{
			col = m.data[p].j;
			Q = Cpot[col];
			t.data[q].i = M.DATA[P].J;
			T.DATA[Q].J = m.data[p].i;
			T.DATA[Q].E = M.DATA[P].E;
			++cpot[col];
		}
	return Ok;
} Transposesmatrix

Attached C + + Source:

#include <iostream> #include <vector> using namespace std;     #define MAXSIZE 1000 typedef struct{int row, col;//element row, column int elem;

element value}triple;
	typedef struct {Triple data[maxsize+1];  int row_num, col_num, Elem_num;

The number of rows and columns of a matrix, and the number of}tsmatrix not 0;                     /************************************************************************//* Transpose algorithm 1, time complexity of O (col_num*elem_num)  * */************************************************************************/void Transposematrix (TSMatrix
	Matrix, Tsmatrix &transed_matrix) {transed_matrix.row_num = Matrix.col_num;
	Transed_matrix.col_num = Matrix.row_num;
	Transed_matrix.elem_num = Matrix.elem_num;
	int q = 0; Iterate through the matrix for (int i = 0; i < Matrix.col_num; i++) {//Find all the non-0 elements in column I, then add to Transed_matrix for (int j = 0; J < Matrix.elem_num; J + +) {//here is to be reduced because the input is started with subscript 1, when the storage is starting from 0 if ((matrix.data[j].col-1) = i) {Transed_matrix.data[q].row = MATRIX.D
				Ata[j].col; Transed_maTrix.data[q].col = Matrix.data[j].row;
				Transed_matrix.data[q].elem = Matrix.data[j].elem;
			q++; }}}//transposematrix/************************************************************************//* Transpose algorithm 2, time
Degree of Complexity O (elem_num) * */************************************************************************/
	void Fasttransposematrix (Tsmatrix matrix, Tsmatrix &transed_matrix) {transed_matrix.row_num = Matrix.col_num;
	Transed_matrix.col_num = Matrix.row_num;

	Transed_matrix.elem_num = Matrix.elem_num; Add num and cpot two variables;//num[col]: Represents the number of 0 elements in the col column of the Matrix//cpot[col]: The first non-0 element of the Col column in the matrix is in the appropriate position in Transed_matrix.data/*--------
	Initialize the two arrays---------------* * vector<int> num;
	Num.resize (matrix.col_num, 0);
	for (int i = 0; i < Matrix.elem_num; i++) {num[matrix.data[i].col-1]++;
	} vector<int> Cpot;
	Cpot.resize (matrix.col_num, 0);
	Cpot[0] = 0; for (int col = 1; col < matrix.col_num; col++) {Cpot[col] = Cpot[col-1]+num[col-1]; }/*--------------------------------------*/for (int p = 0; p < matrix.elem_num p++) {int col = matrix.data[p]
		. col-1;
		int q = Cpot[col];
		Transed_matrix.data[q].row = Matrix.data[p].col;
		Transed_matrix.data[q].col = Matrix.data[p].row;
		Transed_matrix.data[q].elem = Matrix.data[p].elem;
	++cpot[col];
	}}//fasttransposematrix//print matrix void Printmatrix (Tsmatrix matrix) {int max_row = 0, max_col = 0; Gets the maximum number of rows and maximum columns for a matrix non-0 element for (int i = 0; i < Matrix.elem_num; i++) {if (Max_row < Matrix.data[i].row) {Max_ro
		W = matrix.data[i].row;
		} if (Max_col < Matrix.data[i].col) {max_col = Matrix.data[i].col;
	}//Constructs a two-dimensional array, which is the element of a two-dimensional matrix, only for the convenience of outputting vector<vector<int>> Tmp_data;
	Initialize the two-dimensional array tmp_data.resize (Max_row) first;
	for (int i = 0; i < Max_row i++) {tmp_data[i].resize (max_col, 0);
		///Add the information in the ternary group to the two-dimensional array for (int i = 0; i < Matrix.elem_num i++) {Triple tri;
		Tri.col = MATRIX.DATA[I].COL-1; Tri.row = Matrix.data[i]. row-1;
		Tri.elem = Matrix.data[i].elem;
	Tmp_data[tri.row][tri.col] = Tri.elem; for (int i = 0; i < Max_row. i++) {for (int j = 0; J < Max_col; J +) {Cout<<tmp_data[i][j]<&lt ;"
		\ t ";
	} cout<<endl;
} cout<<endl; }//printmatrix/************************************************************************//* Author by B o 2017-4-6 * */************************************************************************/voi
	D Main () {Tsmatrix matrix;
	
	Tsmatrix Transed_matrix; In turn, the number of non 0 elements of the matrix, the number of rows in the matrix, the number of matrices, the contents of the ternary group cout<< "INP
	UT the Non-zero number of matrix: ";
	cin>>matrix.elem_num;
	cout<< "Input the number of row:";
	cin>>matrix.row_num;
	cout<< "Input the number of column:";
	cin>>matrix.col_num;

	cout<< "input the triple of matrix (subscript starting from 1):" <<endl; for (int i = 0; i < matrix.elem_num; i++)
	{cin>>matrix.data[i].row>>matrix.data[i].col>>matrix.data[i].elem; } cout<< "Input finished!"

	<<endl;
	The transpose algorithm of the matrix is 1 Transposematrix (Matrix, Transed_matrix);

	Matrix Transpose algorithm 2//Fasttransposematrix (matrices, Transed_matrix);
	cout<< "/--------raw matrix------------\ <<endl;
	Printmatrix (matrix);

	cout<< "\----------------------------/" <<endl;
	cout<< "/--------transpose matrix-----------\ \" <<endl;
	Printmatrix (Transed_matrix);
cout<< "\----------------------------/" <<endl; }//main



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.