Data structure and algorithm -- sparse array

Posted by generic88 on Tue, 21 Dec 2021 09:18:17 +0100

Data structure and algorithm -- sparse array

(the learning content is the JAVA data structure and algorithm of Mr. Han Shunping of shangsilicon Valley)
1. What is a sparse array:
When most elements in an array are 0 or an array with the same value, you can use a sparse array to save the array.
2. Processing method of sparse array:

  • How many rows and columns are there in the record array? How many different values are there
  • The rows, columns and values of elements with different values are recorded in a small-scale array, so as to reduce the size of the program

    3. Description of 2D array - > sparse array conversion:
  • First, we need to have a known two-dimensional array, or we can create a two-dimensional array to determine the number of rows, columns and specific values.
  • Then, when the two-dimensional array is known, start to create a sparse array. The index of the array in java starts from 0. The values saved in row 0 of the sparse array are the number of rows, columns and non-zero numbers of the two-dimensional array.
  • The number of rows in a sparse array depends on the number of non-zero values in the two-dimensional array. As shown in the figure above, rows 1-8 in a sparse array determine the rows, columns and specific values of non-zero values in the two-dimensional array. For example: 22, the position in the original two-dimensional array is 0 rows and 3 columns, so the three arrays filled in the sparse array are 0, 3 and 22 respectively (in the traversal array, the default is from left to right and from top to bottom, so 22 is the first number to read out).
    4. Code display:
    1. First, I created an 11 * 11 two-dimensional array, filled in several non-zero numbers, and traversed and output the two-dimensional array.
// 1. Create a binary array
        /**
         * 0:It means there are no pieces. 1: it means white chess. 2: it means black chess
         */
        int[][] chessArr = new int[11][11];
        chessArr[0][1] = 1;
        chessArr[1][2] = 2;
        chessArr[2][3] = 1;
        chessArr[3][4] = 2;
        System.out.println("Original array:");
        for (int[] row : chessArr) {
            for (int data : row) {
                System.out.print(data + "\t");

            }
            System.out.println();
        }

2. The second is to create a sparse array

  1. Before creating a sparse array, we need to determine the dimension of the sparse array, because the number of rows and columns of the two-dimensional array has been determined, so we only need to determine the non-0 number of the two-dimensional array, and save the non-0 number by predefining a sum variable value.
int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr[i][j] != 0) {
                    sum++;
                }
            }
        }
  1. The process of creating a sparse array is actually to fill the number of rows, columns and values of non-zero numbers into the sparse array. You only need to traverse, because the number of columns of the sparse array we create is a certain three columns (used to record rows, columns and values respectively). Therefore, only the number of rows of the sparse array is changing, that is, it is equal to the number of non-zero values of the two-dimensional array.
  2. Create a sparse array and traverse:
// 2.2 creating sparse arrays
        int[][] spaseArr = new int[sum + 1][3];
        spaseArr[0][0] = 11;
        spaseArr[0][1] = 11;
        spaseArr[0][2] = sum;

        int count = 0;// Used to record the number of rows
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr[i][j] != 0) {
                    count++;
                    spaseArr[count][0] = i;
                    spaseArr[count][1] = j;
                    spaseArr[count][2] = chessArr[i][j];
                }
            }
        }

        System.out.println("The sparse array is:");
        System.out.println("row\tcolumn\tvalue");
        for (int[] row : spaseArr) {
            for (int data : row) {
                System.out.print(data + "\t");
            }
            System.out.println();
        }

4. If necessary, we can also restore the sparse array to a two-dimensional array and traverse it at the same time to observe whether it is the same as the original two-dimensional array.

int[][] newArr = new int[spaseArr[0][0]][spaseArr[0][1]];
        for (int i = 1; i < spaseArr.length; i++) {
            newArr[spaseArr[i][0]][spaseArr[i][1]] = spaseArr[i][2];
        }

        System.out.println("2D array after recovery:");
        for (int[] row : newArr) {
            for (int data : row) {
                System.out.print(data + "\t");
            }
            System.out.println();
        }

In this case, the creation of sparse array and the mutual conversion between sparse array and two-dimensional array have been basically completed. If necessary later, the array can be output / input in the form of file through IO stream.
The program results are shown in the figure below:

(slip away, bald graduate freshmen are going to continue their liver. Come on, family ~ ~)

Topics: Java Algorithm data structure