Sparse array of data structure

Posted by rtpmatt on Sun, 03 Nov 2019 04:26:57 +0100

Sparse array

Basic introduction

When most of the elements in an array are 0, or an array with the same value, a sparse array can be used to hold the array.

The processing method of sparse array is as follows:

1. How many rows and columns are there in the record array? How many different values are there

2. Record the columns and values of elements with different values in a small array, so as to reduce the size of the program

Sparse array example:

 

The first row of the right array is 6 = how many rows are there in the left array, 7 = how many columns are there in the left array, 8 = how many non-zero values are there in the left array, and then the next eight rows hold the coordinates and values of the data.

practical application

In the program of Gobang, it has the function of saving, exiting and continuing.

Analysis problem: because many values of the two-dimensional array are 0 by default, a lot of meaningless data is recorded, so we can use sparse array to store at this time;

Thought analysis:

1. Use sparse array to keep the previous two-dimensional array (chessboard, map, etc.)

2. Save the sparse array to disk, and restore the original two-dimensional array number

3. Overall thinking analysis

4. Code implementation

package com.sparseArray;

public class SparseArray {

    public static void main(String[] args) {
        // Create an original 2D array 11 * 11
        // 0: It means there is no chess piece, 1 means black, 2 means blue
        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[4][5] = 2;
        // Output original 2D array
        System.out.println("Original 2D array~~");
        for (int[] row : chessArr1) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }

        // Thinking of converting 2D array to sparse array
        // 1. First traverse the two-dimensional array to get the number of non-zero data
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    sum++;
                }
            }
        }

        // 2. Create the corresponding sparse array
        int sparseArr[][] = new int[sum + 1][3];
        // Assign values to sparse arrays
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;

        // Traverse the two-dimensional array and store the non-zero value in the sparseArr in
        int count = 0; //count Used to record the number of non-zero data
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr1[i][j];
                }
            }
        }

        // Output sparse array form
        System.out.println();
        System.out.println("Get sparse array as~~~~");
        for (int i = 0; i < sparseArr.length; i++) {
            System.out.printf("%d\t%d\t%d\t\n", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
        }
        System.out.println();

        //Sparse array --> Restore to original 2D array
        /*
         *  1. First read the first row of the sparse array, and create the original two-dimensional array according to the data in the first row, such as chessar2 = int [11] [11] above
            2. Read the data of the last few rows of sparse array and assign it to the original two-dimensional array
         */

        //1. First read the first row of the sparse array, and create the original two-dimensional array according to the data in the first row

        int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];

        //2. Reading the data of the last few rows of sparse array(Start with the second line),And assign it to the original two-dimensional array

        for(int i = 1; i < sparseArr.length; i++) {
            chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }

        // Output recovered 2D array
        System.out.println();
        System.out.println("Recovered 2D array");

        for (int[] row : chessArr2) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
    }

}
Code

Code address

Sparse array can reduce data complexity and memory consumption

Topics: PHP