Data Structure Learning (sparse matrix implementation, triple)

Source: Internet
Author: User

Use triplet to create, reverse, and multiply a sparse matrix.

Multiplication does not use an additional vector. Sorry for any errors. Performance may not be well described in the book.

The Code is as follows: (this code has only passed my simple test and may have problems. Believe in your abilities and dare to question them. You are welcome to provide better, faster, and more concise code or methods and identify errors. Use gcc4.6.3 for compiling in ubuntu12.04. If any error occurs in vc, please understand .)

[Cpp]
/*
* Created by Reage at 2012 November 29
* Description: sparse matrix implementation, including multiplication and Reversal
*
* Blog: http://blog.csdn.net/rentiansheng
*/
# Include <stdio. h>
# Include <stdlib. h>
 
# Define LINE print_split (20)
 
Typedef struct triple {
Int I, j; // row and column
Int value;
} Triple;
 
Typedef struct matrix {
Triple * head;
Int row, column, count; // number of rows, columns, and sparse matrix elements in the Matrix
} Matrix;
 
/*
* Description: determines whether an element exists at a certain position.
* Parameter:
* X: line coordinate
* Y: column Coordinate
* Count: number of elements to be searched. If it is 0, it indicates all. Otherwise, it indicates the value of count.
* Result: 1 is returned. 0 is returned If no result is found.
*/
Int find _ item (matrix * m, int x, int y, int count ){
Triple * tp = m-> head;
If (! Count | count> m-> count) count = m-> count;
Int I;
For (I = 0; I <count-1; I ++ ){
If (tp-> I = x & tp-> j = y) return 1;
Tp ++;
}
Return 0;
}
 
/*
* Initialize the sparse matrix.
*/
Void init_matrix (matrix * m, int row, int column, int count ){
// If (m-> head) free (m-> head); // if the matrix already exists, release the original data space
M-> head = (triple *) malloc (count * sizeof (triple ));
If (! M-> head ){
Printf ("Allocate memory failed! Application will exit .");
Exit (0 );
}
M-> row = row;
M-> column = column;
M-> count = count;
 
}
 
/*
* The input sparse matrix may not be sorted in the specified order.
* First, the size of rows increases from small to large, and the size of rows increases from small to large.
*/
Void sort_matrix (matrix * m ){
Int I, j, count = m-> count;
Int loc;
Triple * tp = m-> head;
Triple tmp;
For (I = 0; I <count-1; I ++ ){
Loc = I;
For (j = I + 1; j <count; j ++ ){
If (tp [loc]. I> tp [j]. I) {// sort by row
Loc = j;
}
Else if (tp [loc]. I = tp [j]. I & tp [loc]. j> tp [j]. j ){
Loc = j;
}
}
Memcpy (& tmp, & tp [loc], sizeof (triple ));
Memcpy (& tp [loc], & tp [I], sizeof (triple ));
Memcpy (& tp [I], & tmp, sizeof (triple ));
}
}
 
/*
* Create a sparse matrix using Array Storage
*/
Void creat_matrix (matrix * m ){
Int row, column, count; // number of rows, columns, and non-zero elements in the sparse matrix
Int I;
Int x, y;
Triple * use_triple;
Int value;
Printf ("please enter sparse matrix row, column: \ n ");
Scanf ("% d", & row, & column );
Printf ("please enter a number as the number of non-zero elements of the sparse matrix: \ n ");
Scanf ("% d", & count );
Init_matrix (m, row, column, count );
Use_triple = m-> head;
For (I = 0; I <count; I ++ ){
Repeat: printf ("please enter % d elements x and y coordinate values:", I + 1 );
Printf ("and a number as element value :");
Scanf ("% d", & x, & y, & value );
If (find_item (m, x, y, I + 1) {printf ("The position has been there exists an elemetn. \ n"); goto repeat ;}
Use_triple-> I = x;
Use_triple-> j = y;
Use_triple-> value = value;
Use_triple ++;
}
Sort_matrix (m );
}
 
/*
* Acts as a delimiter and outputs The count '-' symbol.
*/
Void print_split (int count ){
Int I;
For (I = 0; I <count; I ++)
Printf ("-");
Printf ("\ n ");
}
 
/*
* Data in the sparse matrix
*/
Void print_matrix (matrix m ){
Int len = m. count;
Int I;
Printf ("\ n ");
Triple * sm = m. head;
LINE;
For (I = 0; I <len; I ++ ){
Printf ("% 4d | % 4d | % d \ n", sm-> I, sm-> j, sm-> value );
Sm ++;
LINE;
}
}
 
/*
* Description: reverse the sparse matrix. If you want to reverse the matrix, sort it.
* Paramtere:
* Sm: Source Sparse Matrix
* Dm: Target Sparse Matrix
*/
Void transpose_matrix1 (matrix * sm, matrix * dm ){
Int I, j, count = sm-> count;
Triple * dt, * st = sm-> head;
Init_matrix (dm, sm-> row, sm-> column, sm-> count); // initialize the target sparse matrix.
Dt = dm-> head
; For (I = 0; I <count; I ++ ){
Dt-> I = st-> j;
Dt-> j = st-> I;
Dt-> value = st-> value;
Sm ++;
Dt ++;
}
Sort_matrix (dm );

}
 
/*
* Description: sparse matrix reversed,
* Paramtere:
* Sm: Source Sparse Matrix
* Dm: Target Sparse Matrix
*/
Void transpose_matrix (matrix * sm, matrix * dm ){
Int I, j;
Triple * dt, * st;
Init_matrix (dm, sm-> row, sm-> column, sm-> count); // initialize the target sparse matrix.
Dt = dm-> head;
For (I = 0; I <sm-> column; I ++ ){
St = sm-> head;
For (j = 0; j <sm-> count; j ++ ){
If (st-> j = I ){
Dt-> I = I;
Dt-> j = st-> I;
Dt-> value = st-> value;
Dt ++;
}
St ++;
}
}
 
}
 
/*
* Description: insert an element to the sparse matrix and add one element to the number of elements.
* Parameter:
* I, j, and value: the row, column, and value of the element to be inserted.
*/
Void insert_matrix (matrix * m, int I, int j, int value ){
M-> count ++;
If (1 = m-> count)
M-> head = (triple *) malloc (sizeof (triple ));
Else
M-> head = (triple *) realloc (m-> head, m-> count * sizeof (triple ));
(M-> head) [m-> count-1]. I = I;
(M-> head) [m-> count-1]. j = j;
(M-> head) [m-> count-1]. value = value;
}
 
 
/*
* Description: Multiplication of Matrices
* Pararmeter:
* M1, m2: multiplier and multiplier
* Result: score result
*/
Void multi_matrix (matrix * m1, matrix * m2, matrix * result ){
Int tmp; // Save the temporary calculation result.
Int I, j; // I indicates the number of columns computed to the m2 matrix.
Int use = 0; // indicates that m1 is calculating the nth non-zero position of the current row.
Triple * tp = m1-> head; // The first address of a non-zero element in m1
Triple * tp2 = m2-> head; // The first address of a non-zero element in m2
Init_matrix (result, 0, 0, 0); // initialize the Matrix
If (m1-> column! = M2-> row) return; // operations that cannot be performed are not satisfied with the group Operation Conditions
If (m1-> column> m1-> count | m2-> row> m2-> count ){
// The score relationship with a matrix can be known. One row in m1 and one column in m2 perform operations.
// If the non-zero number in m1 does not satisfy one row, the non-zero number in m2 does not satisfy one column. There is no need to perform operations
Return;
}
/*
* 1. First, find a non-zero element tp [usue] in m1 of the row,
* 2. Check whether the m2 [tp [use]. j] [I] element is saved. If so, go to step 1. Otherwise, go to step 2.
* 3. Add the calculation results to tmp.
* 4. Search for the next non-zero element in the same row. If you want to transfer to 2nd, do not go to step 2.
* 5. Clear the temporary data, enter the next row, and go to step 2.
*/
While (tp <& tp [m1-> count]) {// determine whether there are uncomputed elements in m1. If not, the calculation ends.
For (I = 0; I <m2-> column; I ++) {// used to control the number of the column in m2 for Calculation
Use = 0; // calculate the first non-zero element in the row.
Tmp = 0; // Calculation Result
For (j = 0; j <m2-> count; j ++) {// traverses all elements in m2 and returns the target Element
If (tp2 [j]. j = I & tp2 [j]. I = tp [use]. j ){
Tmp + = tp2 [j]. value * tp [use]. value;
If (tp [use]. j = m1-> column ){
Break;
}
If (tp >=& (m1-> head) [m1-> count]) | tp [use]. I! = Tp [use + 1]. I) break;
Use ++;
}
}
If (tmp> 0) insert_matrix (result, I, j, tmp );
}
// Go to the next row
While (tp-> I = tp [1]. I ){
Tp ++;
If (tp >=& (m1-> head) [m1-> count]) return;
}
Tp ++;
}
}
 
Void destroy_matrix (matrix * m ){
M-> row = 0;
M-> column = 0;
M-> count = 0;
Free (m-> head );

 

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.