Data Structure and Algorithms

Posted by PHPcadet on Thu, 27 Jan 2022 19:02:41 +0100

Data Structure and Algorithms

1. Data structure

1. Data structure includes linear structure and non-linear structure.

Linear structure:
(1) Sequential storage (array, storage elements are continuous) and chain storage (chain list, storage elements are not necessarily continuous)
(2) Arrays, Queues, Chain Lists and Stacks
Nonlinear structure: 2-D data group, multi-dimensional array, generalized table, tree structure, graph structure

2. Sparse Array

Use a sparse array to save an array when most of its elements are zero or the same value

(1) how many rows and columns of the record array, and how many different values
(2) Record rows, columns and values of elements with different values to another array

Two-dimensional array conversion sparse array ideas:

1. Traverse through the original two-dimensional array to get the effective number of data sum, so as to get the sparse array size
2. A sparse array can be created from the sum, a [sum+1] [3]
3. Store valid data of a two-dimensional array into a sparse array in rows, columns, and values
4. io Stream Stores Sparse Array

Ideas for switching sparse arrays to two-dimensional arrays:

1. Read the first row of the sparse array to get the size of the original two-dimensional array, such as a [6] [7] above
2. Read the data in the last few rows of the sparse array and assign it to the original two-dimensional array.

Application examples:

1. Use sparse arrays to hold two-dimensional arrays similar to the previous one. For example: checkerboard storage, maps, and so on.
2. By storing the sparse array on disk, the original two-dimensional array can be restored.

Code implementation:

Checkerboard storage, redisk (using IO streams to store sparse arrays):
import java.io.File;
import java.io.IOException;

/**
 * @author Purple Wind
 * @date 2022 January 27, 2008 18:58
 */
public class SparseArrayTest {
    public static void main(String[] args) throws IOException {
//        Create an original two-dimensional array 9*9
//        Checkerboard:
//        0----->No chess
//        Odd Number---->Black Chess
//        Non-zero even number--->White chess pieces
        int chessArr1[][] = new int[9][9];
        chessArr1[0][1] = 1;
        chessArr1[1][2] = 2;
        chessArr1[2][3] = 3;
        chessArr1[3][4] = 4;
        chessArr1[4][5] = 5;
        chessArr1[5][6] = 6;
        chessArr1[6][7] = 7;
        chessArr1[7][8] = 8;

//        Output original two-dimensional array
        System.out.println("Output original two-dimensional array");
        for (int[] row : chessArr1) {
            for (int data : row)
                System.out.printf("%d\t", data);
            System.out.println();
        }
//        2-D Array to Sparse Array
//        1. Traverse first to get the number of non-zero data
        int sum = 0;
        for (int[] row : chessArr1)
            for (int data : row) {
                if (data != 0)
                    sum++;
            }
        System.out.println("Number of valid original two-dimensional arrays" + sum);
//         2. Create a corresponding sparse array
        int sparseArr[][] = new int[sum + 1][3];
//          3. Assigning to sparse arrays
        sparseArr[0][0] = chessArr1.length;
        sparseArr[0][1] = chessArr1[0].length;
        sparseArr[0][2] = sum;
//        4. Traverse two-dimensional arrays, assign non-zero data to sparse arrays
        int count = 0;
        for (int i = 0; i < chessArr1.length; i++) {
            for (int j = 0; j < chessArr1.length; j++)
                if (chessArr1[i][j] != 0) {
                    count++;
                    System.out.println(count);
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr1[i][j];
                }
        }
//           5. Save Sparse Array
        File file = new File("C:\\Users\\Administrator\\Desktop\\git\\suanfa\\sparsearray\\src\\chess.txt");
        Writer writer = new Writer();
        File file1 = writer.save(sparseArr, file);
//         6. Read files to get sparse arrays
        Reader reader = new Reader();
        int[][] sparseArray = reader.getSparseArray(file1);

//           7. Read file to get sparse array: output sparse array
        System.out.println("Read file to get sparse array");
        for (int[] row : sparseArray) {
            for (int data : row)
                System.out.printf("%d\t", data);
            System.out.println();
        }

//        8. Restore the original two-dimensional array
//        1. To know the size of the original array, traverse the sparse array
//        2. Create a two-dimensional array
        int chessArr2[][] = new int[sparseArray[0][0]][sparseArray[0][1]];
//         3. Traverse sparse arrays, assign two-dimensional arrays to recover
        for (int i = 1; i < sparseArray.length; i++)
            chessArr2[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];

//         4. Two-dimensional array of output recovery
        System.out.println("Output Recovered Two-Dimensional Array");
        for (int[] row : chessArr2) {
            for (int data : row)
                System.out.printf("%d\t", data);
            System.out.println();
        }
    }
}

IO streams store sparse arrays:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author Purple Wind
 * @date 2022 January 27, 2001 18:55
 */
public class Writer {
    public File save(int[][] sparseArr, File file) {
        //        Save Sparse Array
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(file));
            //Save the compressed board in chess.txt file
            for (int[] row : sparseArr) {
                for (int data : row)
//                    Separate by spaces
                    bw.write(data + " ");
//                Line Break
                bw.write("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null)
                    bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }
}

IO stream read sparse array:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author Purple Wind
 * @date 2022 January 27, 2001 18:42
 */
public class Reader {
    //    Read File to Get Sparse Array Method Class
    public int[][] getSparseArray(File file) throws IOException {
//       Method 2. Read chess.txt gets the size of a sparse array
        String line;
        int value;
        int row = 0;
//        Use to read the first row to get the sparse array size
        BufferedReader br1 = new BufferedReader(new FileReader(file));
        BufferedReader br2 = new BufferedReader(new FileReader(file));
//            Get the original two-dimensional array size and valid data first
//            Read the first line
        String[] s = br1.readLine().split(" ");
        value = Integer.parseInt(s[2]);
//            Create Sparse Array
        int[][] sparseArr2 = new int[value + 1][3];
        br1.close();
//           Read one line at a time
        while ((line = br2.readLine()) != null) {
//              Split by space
            String[] temp = line.split(" ");
//               ergodic
            for (int i = 0; i < temp.length; i++)
//                   Assign to sparse array
                sparseArr2[row][i] = Integer.parseInt(temp[i]);
            row++;
        }
        br2.close();
        return sparseArr2;
    }
}

Run result:

Output original two-dimensional array
0	1	0	0	0	0	0	0	0	
0	0	2	0	0	0	0	0	0	
0	0	0	3	0	0	0	0	0	
0	0	0	0	4	0	0	0	0	
0	0	0	0	0	5	0	0	0	
0	0	0	0	0	0	6	0	0	
0	0	0	0	0	0	0	7	0	
0	0	0	0	0	0	0	0	8	
0	0	0	0	0	0	0	0	0	
Valid number of original 2-D array 8
1
2
3
4
5
6
7
8
 Read file to get sparse array
9	9	8	
0	1	1	
1	2	2	
2	3	3	
3	4	4	
4	5	5	
5	6	6	
6	7	7	
7	8	8	
Output Recovered Two-Dimensional Array
0	1	0	0	0	0	0	0	0	
0	0	2	0	0	0	0	0	0	
0	0	0	3	0	0	0	0	0	
0	0	0	0	4	0	0	0	0	
0	0	0	0	0	5	0	0	0	
0	0	0	0	0	0	6	0	0	
0	0	0	0	0	0	0	7	0	
0	0	0	0	0	0	0	0	8	
0	0	0	0	0	0	0	0	0	

Process finished with exit code 0

Topics: Algorithm data structure linked list