02-Sparse Arrays and Queues

Keywords: Java

introduce

Sparse arrays can be used to save an array when most of the elements in an array are 0 or are worth arrays.

Sparse arrays are processed by:

1) There are several rows and columns in the record array, and how many different values are there?

2) Record rows, columns and values of elements with different values in a small array, thereby reducing the size of the program.

Application examples of sparse arrays

1) Use sparse arrays to preserve two-dimensional arrays (chessboard, map, etc.) similar to the previous ones.

2) Store sparse arrays and restore the original two-dimensional arrays

3) Overall thinking analysis

Two-dimensional Array to Sparse Array

1. Traversing two-dimensional arrays, sum the number of valid data

2. You can create a sparse array int[sum+1][3] based on sum

3. Store valid data from two-dimensional arrays into sparse arrays

The Idea of Converting Sparse Array to Primitive Two-Dimensional Array

1. First read the first line of the sparse array and create the original two-dimensional array int[11][11]

2. Read the data of the last few rows of the sparse array and assign it to the original two-dimensional array

Sparse Array Code Implementation

package com.datastack.datastack.sparsearr;
/**
 * Sparse array
 * @author ahpan
 *
 */
public class SparseArr {
	public static void main(String[] args) {
		//Create a two-dimensional array and print it
		int arr[][] = new int[11][11];
		arr[1][2] = 1;
		arr[2][3] = 2;
		System.out.println("Two-dimensional array");
		for(int[] row : arr){
			for(int data : row){
				System.out.printf("%d\t",data);
			}
			System.out.println();
		}
		
		//2-D Array to Sparse Array
		//1. Traverse two-dimensional arrays, get the number of non-zero, and define sparse arrays
		int sum = 0;
		for(int i=0;i<11;i++){
			for(int j=0;j<11;j++){
				if(arr[i][j] != 0){
					sum++;
				}
			}
		}
		//2. Create sparse arrays
		int[][] sparseArr = new int[sum+1][3];
		//3, assign values to the first row of a sparse array
		sparseArr[0][0] = 11;
		sparseArr[0][1] = 11;
		sparseArr[0][2] = sum;
		//4, assign values to other rows of a sparse array
		int count = 0;//Define non-zero quantities
		for(int i=0;i<11;i++){
			for(int j=0;j<11;j++){
				if(arr[i][j] != 0){
					count++;
					sparseArr[count][0] = i;
					sparseArr[count][1] = j;
					sparseArr[count][2] = arr[i][j];
				}
			}
		}
		//Print sparse arrays
		System.out.println("Sparse array");
		for(int[] row : sparseArr){
			for(int data : row){
				System.out.printf("%d\t",data);
			}
			System.out.println();
		}
		
		//Sparse arrays to two-dimensional arrays, defining two-dimensional arrays
		int arr2[][] = new int[sparseArr[0][0]][sparseArr[0][0]];
		//Assignment, traversing sparse arrays from the second line
		for(int i=1;i<sparseArr.length;i++){
			arr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
		}
		System.out.println("Two-dimensional array");
		for(int[] row : arr2){
			for(int data : row){
				System.out.printf("%d\t",data);
			}
			System.out.println();
		}
	}
}


Posted by nrerup on Mon, 30 Sep 2019 15:29:54 -0700