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):
- The first row stores the total number of original data rows, total columns, and total effective data
- 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
- Get the number of rows and columns of the original array
- Traversing the original array to get the number of valid data
- 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
public static int[][] toSparseArray(int[][] array, int invalid) {
int sum = 1, row = array.length, col = array[0].length;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (array[i][j] != invalid)
sum++;
}
}
int[][] sparseArr = new int[sum][3];
sparseArr[0][0] = row;
sparseArr[0][1] = col;
sparseArr[0][2] = sum - 1;
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;
}
- Create the original array from the first row of the sparse array
- Restore data from sparse array to original array
public static int[][] toArray(int[][] sparseArr) {
int[][] array = new int[sparseArr[0][0]][sparseArr[0][1]];
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;
public class SparseArray {
public SparseArray() {
}
public static int[][] toSparseArray(int[][] array) {
return toSparseArray(array, 0);
}
public static int[][] toSparseArray(int[][] array, int invalid) {
int sum = 1, row = array.length, col = array[0].length;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (array[i][j] != invalid)
sum++;
}
}
int[][] sparseArr = new int[sum][3];
sparseArr[0][0] = row;
sparseArr[0][1] = col;
sparseArr[0][2] = sum - 1;
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;
}
public static int[][] toArray(int[][] sparseArr) {
int[][] array = new int[sparseArr[0][0]][sparseArr[0][1]];
for (int i = 1; i <= sparseArr[0][2]; i++) {
array[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
return array;
}
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) {
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
Posted by adi on Fri, 14 Feb 2020 04:05:58 -0800