Sparse array of JAVA data structure

Sparse array

About sparse arrays:

  • When most of the data in an array is 0 (the same value), sparse array can be used to save the array
  • Compress storage (keep effective data) can save storage space to avoid unnecessary waste of resources

Storage mode (non chained storage):

  • Character description
  1. The first row stores the total number of original data rows, total columns, and total effective data
  2. Next, each row stores the row, column and specific value of the valid data
  • Icon
    Original array:
    0,1,0,2,0
    0,0,0,2,0
    1,1,0,0,0
    0,0,0,2,0
    Sparse array:
    5, 4, 6 - > the first row stores the total number of original data rows, total columns, and total effective data
    0, 1, 1 - > indicates that the data of 0 row and 1 column in the original array is 1
    0,3,2
    1,3,2
    1,0,1
    1,1,1
    2,4,2

Code implementation:

  • Convert 2D array to sparse array
  1. Get the number of rows and columns of the original array
  2. Traversing the original array to get the number of valid data
  3. Create and assign sparse arrays
    (1) Assign value to the first row of sparse array (total row number, total column number, total effective data number of original array)
    (2) Traverse the original array and put the valid data and its corresponding positions into the sparse array
/**
	 * 
	 * <pre>
	 * Convert array to sparse array
	 * </pre>
	 * 
	 * @param array   Primitive array
	 * @param invalid Invalid data
	 * @return Sparse array
	 */
	public static int[][] toSparseArray(int[][] array, int invalid) {
		int sum = 1, row = array.length, col = array[0].length;

		// 1. Traverse the initial array to get the number of valid data
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				if (array[i][j] != invalid)
					sum++;
			}
		}

		// 2. Create the corresponding sparse array
		int[][] sparseArr = new int[sum][3];
		// Assign values to sparse arrays
		sparseArr[0][0] = row;
		sparseArr[0][1] = col;
		sparseArr[0][2] = sum - 1;
		// Put valid data into sparse array
		int count = 0;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				if (array[i][j] != invalid) {
					count++;
					sparseArr[count][0] = i;
					sparseArr[count][1] = j;
					sparseArr[count][2] = array[i][j];

				}
			}
		}
		return sparseArr;
	}

  • Sparse array to 2D array
  1. Create the original array from the first row of the sparse array
  2. Restore data from sparse array to original array
	/**
	 * <pre>
	 * Restore sparse array
	 * Invalid data restored to 0
	 * </pre>
	 * 
	 * @param sparseArr Sparse array
	 * @return Recovered array
	 */
	public static int[][] toArray(int[][] sparseArr) {
		// 1. Create original array
		int[][] array = new int[sparseArr[0][0]][sparseArr[0][1]];
		// 2. Restore data
		for (int i = 1; i <= sparseArr[0][2]; i++) {
			array[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
		}
		return array;
	}

Full program test:

  • test data
    Original matrix:
    00000000000
    00000100000
    00000010000
    00000020000
    00000000000
    00000000100
    00000000001
    00000000000
    00000000000
    00002000000
    00000200000
  • Result prediction
    11 11 7
    1 5 1
    2 6 1
    3 6 2
    5 8 1
    6 10 1
    9 4 2
    10 5 2
  • Program code
package DataStructures.linear.sparseArray;

/**
 * 
 * <pre>
 * Sparse array
 * Compress storage (keep effective data) can save storage space to avoid unnecessary waste of resources
 * </pre>
 * 
 * <pre>
 * Non chained storage
 * The first row stores the total number of original data rows, columns, and non-zero data
 * Next, each row stores the row, column, and specific value of the valid data
 * </pre>
 * 
 * @author Lasion
 * @version 1.1
 * @since 1.0
 */
public class SparseArray {

	public SparseArray() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * <pre>
	 * Convert array to sparse array
	 * The default invalid data is 0
	 * </pre>
	 * 
	 * {@link SparseArray#toSparseArray(int[][], int)}
	 */
	public static int[][] toSparseArray(int[][] array) {
		return toSparseArray(array, 0);
	}

	/**
	 * 
	 * <pre>
	 * Convert array to sparse array
	 * </pre>
	 * 
	 * @param array   Primitive array
	 * @param invalid Invalid data
	 * @return Sparse array
	 */
	public static int[][] toSparseArray(int[][] array, int invalid) {
		int sum = 1, row = array.length, col = array[0].length;

		// 1. Traverse the initial array to get the number of valid data
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				if (array[i][j] != invalid)
					sum++;
			}
		}

		// 2. Create the corresponding sparse array
		int[][] sparseArr = new int[sum][3];
		// Assign values to sparse arrays
		sparseArr[0][0] = row;
		sparseArr[0][1] = col;
		sparseArr[0][2] = sum - 1;
		// Put valid data into sparse array
		int count = 0;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				if (array[i][j] != invalid) {
					count++;
					sparseArr[count][0] = i;
					sparseArr[count][1] = j;
					sparseArr[count][2] = array[i][j];

				}
			}
		}
		return sparseArr;
	}

	/**
	 * <pre>
	 * Restore sparse array
	 * Invalid data restored to 0
	 * </pre>
	 * 
	 * @param sparseArr Sparse array
	 * @return Recovered array
	 */
	public static int[][] toArray(int[][] sparseArr) {
		// 1. Create original array
		int[][] array = new int[sparseArr[0][0]][sparseArr[0][1]];
		// 2. Restore data
		for (int i = 1; i <= sparseArr[0][2]; i++) {
			array[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
		}
		return array;
	}

	/**
	 * <pre>
	 * Whether two arrays are equal
	 * </pre>
	 * 
	 * @param array
	 * @param sparseArr
	 * @return Two arrays are equal - > true
	 */
	private static boolean equal(int[][] array, int[][] sparseArr) {
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array[0].length; j++) {
				if (array[i][j] != sparseArr[i][j])
					return false;
			}
		}
		return true;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[][] a = new int[11][11];
		a[1][5] = 1;
		a[3][6] = 2;
		a[2][6] = 1;
		a[6][10] = 1;
		a[10][5] = 2;
		a[5][8] = 1;
		a[9][4] = 2;
		System.out.println("Original array:");
		for (int[] is : a) {
			for (int n : is)
				System.out.print(n);
			System.out.println();
		}

		System.out.println("-----------------------------");
		int[][] s = toSparseArray(a);
		System.out.println("Sparse array:");
		for (var is : s) {
			for (var n : is)
				System.out.print(n + "  ");
			System.out.println();
		}

		System.out.println("-----------------------------");
		int[][] b = toArray(s);
		System.out.println("Restored array:");
		for (var is : b) {
			for (var n : is)
				System.out.print(n);
			System.out.println();
		}

		System.out.println("-----------------------------");
		System.out.println((equal(a, b)) ? "Two arrays are the same" : "Two arrays are different");
	}

}
  • Operation result
Published 1 original article, praised 0 and visited 5
Private letter follow

Posted by adi on Fri, 14 Feb 2020 04:05:58 -0800