Sparse matrix principle and matrix compression

sparse matrix

In a matrix, if the number of elements with a value of 0 is far more than the number of non-zero elements, and the distribution of non-zero elements is irregular, the matrix is ​​called a sparse matrix; on the contrary, if the number of non-zero elements is the majority , The matrix is ​​called a dense matrix. The density of the matrix is ​​defined as the total number of non-zero elements than the total number of all elements in the above matrix.
When most of the elements in an array are 0, or an array of the same value, you can use a sparse array to save the array.

Insert picture description here

Matrix compression

Since the sparse matrix has fewer non-zero elements and more zero elements, the method of storing only non-zero elements can be used for compressed storage.
Since there is no law in the distribution of non-zero elements, it is necessary to store the non-zero element value while storing the non-zero element value in the matrix, that is, the row number and column number of the non-zero element, and also While storing the value of an element such as aij, it is also necessary to store the row number i of the element and its column number j, thus forming a linear table of triples (i, j, aij).
The triples can be represented by sequential or chained representation, which results in different compression storage methods for sparse matrices.

For example:
Insert picture description here
using a sparse matrix as follows:
Insert picture description here

The processing method for sparse arrays is:

  1. The record array has a total of several rows and columns, and how many different values ​​are there
  2. Record the ranks and values ​​of elements with different values ​​in a small-scale array, thereby reducing the size of the program

Sparse matrix generation and recovery

Insert picture description here

The idea of ​​converting a two-dimensional array to a sparse array:

  1. Traverse the original two-dimensional array to get the number of valid data sum
  2. According to sum, you can create a sparse array int[sum + 1] [3]
  3. Store the valid data of the two-dimensional array into the sparse array

The idea of ​​converting sparse array to original two-dimensional array:

  1. First read the first row of the sparse array, and create the original two-dimensional array based on the data in the first row, such as int [11][11] above
  2. Just read the data in the last few rows of the sparse array and assign it to the original two-dimensional array.

Code implementation: use a two-dimensional array to store sparse matrices here

public class SparseArray {
    
    
    public static void main(String[] args) {
    
    
        //先创建一个原始的二维数组
        int[][] array1 = new int[11][11];
        array1[1][2] =1;
        array1[2][3] =2;
        //输出原始的数组
        System.out.println("原始数组如下:");
        for(int[] row : array1){
    
    
            for(int data: row){
    
    
                System.out.print(data+"\t");
            }
            System.out.println();
        }
        //将二维数组转换稀疏矩阵的思路
        //1.先遍历二维数组得到非零数据的个数
        int sum = 0;
        for (int i = 0; i <11 ; i++) {
    
    
            for (int j = 0; j <11 ; j++) {
    
    
                if (array1[i][j] != 0){
    
    
                    sum++;
                }
            }
        }
        //2.创建对应的稀疏数组
        int array2[][] =  new int[sum+1][3];
        //给稀疏数组赋值
        array2[0][0] = 11;
        array2[0][1] = 11;
        array2[0][2] = sum;
        //遍历二维数组,非零值存到稀疏数组中
        int count = 1;
        for (int i = 0; i <11 ; i++) {
    
    
            for (int j = 0; j <11 ; j++) {
    
    
                if (array1[i][j] != 0){
    
    
                    array2[count][0] = i;
                    array2[count][1] = j;
                    array2[count][2] = array1[i][j];
                    count++;
                }
            }
        }
        //打印稀疏矩阵
        System.out.println("压缩后的稀疏矩阵:");
        for(int[] row : array2){
    
    
            for(int data: row){
    
    
                System.out.print(data+"\t");
            }
            System.out.println();
        }

        //将稀疏数组恢复成原来的数组
       // 1. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,比如上面的   int [11][11]
        int[][] array3 = new int[array2[0][0]][array2[0][1]];
        //2. 在读取稀疏数组后几行的数据,并赋给原始的二维数组即可.
        for (int i = 1; i <= array2[0][2]; i++) {
    
    
             array3[array2[i][0]][array2[i][1]] = array2[i][2];
        }
        //输出恢复后的数组
        System.out.println("恢复后的数组如下:");
        for(int[] row : array3){
    
    
            for(int data: row){
    
    
                System.out.print(data+"\t");
            }
            System.out.println();
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_41784749/article/details/112997357