Data structure Java Implementation--④ array---sparse matrix ternary sequential storage

Source: Internet
Author: User


Author Information
write in front

Gossip don't want to say more, and then summarize, today is the sparse matrix ternary storage of the order structure.

Text Description
first, the noun explanation

1. Sparse matrix
Less than 0 elements in matrix array and no regularity in distribution


2, ternary group storage

an element in the matrix has three properties: line number, column number, element value, become ternary

3. Sequential structure

for each ternary group, according to line number precedence or column number precedence, convenient for the later operation of the Matrix


Second, compression and reduction


1. Compression
progressive scan matrix, you can record a non-0 element.
/blockquote>

2, restore
traversing sequentially stored data, placing each data in the appropriate position
in the matrix




Code Implementation



1. Abstract structure of ternary group

package org. stone6762.entity;/** * @Stone6762 */public class Triplenode {/** * @Fields Row:todo (the line where the element is located) */private int row;/** * @Fi  ELDs Column:todo (the column in which the element resides) */private int column;/** * @Fields value:todo (contents stored in this location) */private double value;/** * @ constructor * @param row * @param column * @param value */public triplenode (int row, int column, double value) {super (); this.row = row; This.column = Column;this.value = value;} /** * @ constructor */public Triplenode () {This (0, 0, 0);} public int GetRow () {return row;} public void Setrow (int row) {this.row = row;} public int GetColumn () {return column;} public void Setcolumn (int column) {this.column = column;} Public double GetValue () {return value;} public void SetValue (double value) {this.value = value;}
 @Overridepublic String toString () {return "[(" + Row + "," + column + ")," + Value + "]";} 


2. Sequential storage of ternary group and its reduction process

import org. stone6762.utils.arrayutils;import org. stone6762.entity.triplenode;/** * @Stone6762 */public class Sparsearray {/** * @Fields Data:todo (where data is stored) */private trip Lenode data[];/** * @Fields Rows:todo (number of rows of raw data) */private int rows;/** * @Fields Cols:todo (number of columns of raw data) */private int cols; /** * @Fields Nums:todo (number of existing data) */private int nums;public triplenode[] GetData () {return data;} public void SetData (triplenode[] data) {This.data = Data;this.nums = Data.length;} public int getRows () {return rows;} public void setrows (int rows) {this.rows = rows;} public int Getcols () {return cols;} public void Setcols (int cols) {this.cols = cols;} public int getnums () {return nums;} public void setnums (int nums) {this.nums = Nums;} Public Sparsearray () {super ();} /** * @ constructor * @param maxSize */public sparsearray (int maxSize) {data = new Triplenode[maxsize];for (int i = 0; i < data. Length i++) {Data[i] = new Triplenode ();} rows = 0;cols = 0;nums = 0;} /** * @ constructor * @param arr */public Sparsearray (doubleArr[][]) {this.rows = Arr.length;this.cols = arr[0].length;//Statistics How many non-0 elements are available for the request for the space below (int i = 0; i < arr.length; i++) {for (int j = 0; J < Arr[0].length; J + +) {if (arr[i][j]! = 0) {nums++;}}} The information for each non-0 element is saved according to the number of non-0 data above, data = new Triplenode[nums];for (int i = 0, k = 0; i < arr.length; i++) {for (int j = 0; J < Arr[0].length; J + +) {if (arr[i][j]! = 0) {Data[k] = new Triplenode (i, J, Arr[i][j]); k++;}}} /** * @Title: PrintArray * @Description: TODO (print stored sparse matrix) */public void Printarrayofrc () {System.out.println (" The ternary group storage structure of sparse matrices is: "); System.out.println ("Rows" + Rows + ", Number of columns:" + cols + ", the number of non-0 elements is:" + nums); System.out.println ("Row subscript column Subscript element value"); for (int i = 0; i < nums; i++) {System.out.println ("" + data[i].getrow () + "" + data[i].getcolumn () + "" + Data[i].getvalue ());}} /** * @Description: TODO () */public void Printarr () {System.out.println ("The ternary storage structure of the sparse matrix is:"); SYSTEM.OUT.PRINTLN (number of rows + rows +, number of columns: "+ cols +", number of non-0 elements: "+ nums);d ouble OriGarr[][] = Rebacktoarr (); Arrayutils.printmularray (Origarr);} /** * @Description: TODO (Restore sparse matrix to movie matrix) * @return */public double[][] Rebacktoarr () {double arr[][] = new Double[rows][col s];for (int i = 0; i < nums; i++) {Arr[data[i].getrow ()][data[i].getcolumn ()] = Data[i].getvalue ();} return arr;}}







Data structure Java Implementation--④ array---sparse matrix ternary sequential 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.