JAVA sparse array data structures

Sparse array

Introduction sparse array:

  • When an arrayMost of the data are 0 (both the same value) , You can use a sparse array to hold the array
  • Compression memory (valid data ready) You can save storage space to avoid unnecessary waste of resources

Storage (non-chain store):

0 1 2
Raw array lines Raw array of column The number of valid data
Effective data row Where valid data column valid data
  • word description
  1. The first line of original data is storedTotal number of lines, the total number of columns, the total number of valid data
  2. Next each row storageValid data row, column, and where the specific value
  • Icon
    original array:
0 1 2 3 4
0 0 1 0 2 0
1 0 0 0 2 0
2 1 1 0 0 0
3 0 0 0 2 0

Sparse array:

0 (Number of lines) 1 (Number of columns) 2 (valid data)
0 5 (Total number of rows) 4 (Total number of columns) 6 (The number of valid data)
1 0 1 1
2 0 3 2
3 1 3 2
4 1 0 1
5 1 1 1
6 2 4 2

Code:

  • Two-dimensional array is converted to a sparse array
  1. Get the original arrayRanks number
  2. Get the original array traversalThe number of valid data
  3. Create a sparse array and sparse array assignment
    (1) on the first line of the sparse array assignment (the original arrayTotal number of lines, the total number of columns, the total number of valid data)
    (2) traversing the original arrayPosition corresponding to valid data andInto the sparse array
/**
	 * 
	 * <pre>
	 * 将数组转换为稀疏数组
	 * </pre>
	 * 
	 * @param array   原始数组
	 * @param invalid 无效数据
	 * @return 稀疏数组
	 */
	public static int[][] toSparseArray(int[][] array, int invalid) {
		int sum = 1, row = array.length, col = array[0].length;

		// 1.遍历初始数组,得到有效数据个数
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				if (array[i][j] != invalid)
					sum++;
			}
		}

		// 2.创建对应的稀疏数组
		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;
	}

  • Sparse array is converted to a two-dimensional array
  1. According to the sparse arrayfirst rowCreating original array
  2. The sparse array restore data to the original array
	/**
	 * <pre>
	 * 将稀疏数组还原
	 * 无效数据还原成0
	 * </pre>
	 * 
	 * @param sparseArr 稀疏数组
	 * @return 恢复后的数组
	 */
	public static int[][] toArray(int[][] sparseArr) {
		// 1.创建原始数组
		int[][] array = new int[sparseArr[0][0]][sparseArr[0][1]];
		// 2.还原数据
		for (int i = 1; i <= sparseArr[0][2]; i++) {
			array[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
		}
		return array;
	}

Complete test procedures:

  • Test data
    of the original matrix:
    00000000000
    00000100000
    00000010000
    00000020000
    00000000000
    00000000100
    00000000001
    00000000000
    00000000000
    00002000000
    00000200000
  • Results predicted
    . 11. 11. 7
    . 1. 5. 1
    2. 1. 6
    . 3 2. 6
    . 5. 1. 8
    . 6 10. 1
    . 9. 4 2
    10 2. 5
  • code
package DataStructures.linear.sparseArray;

/**
 * 
 * <pre>
 * 稀疏数组
 * 压缩存储(保留有效数据)可以节省存储空间以避免资源的不必要的浪费
 * </pre>
 * 
 * <pre>
 * 非链式存储方式
 * 第一行存储原始数据总行数,总列数,总的非0数据个数
 * 接下来每一行都存储有效数据所在行,所在列,和具体值
 * </pre>
 * 
 * @author Lasion
 * @version 1.1
 * @since 1.0
 */
public class SparseArray {

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

	/**
	 * <pre>
	 * 将数组转换为稀疏数组
	 * 默认无效数据为0
	 * </pre>
	 * 
	 * {@link SparseArray#toSparseArray(int[][], int)}
	 */
	public static int[][] toSparseArray(int[][] array) {
		return toSparseArray(array, 0);
	}

	/**
	 * 
	 * <pre>
	 * 将数组转换为稀疏数组
	 * </pre>
	 * 
	 * @param array   原始数组
	 * @param invalid 无效数据
	 * @return 稀疏数组
	 */
	public static int[][] toSparseArray(int[][] array, int invalid) {
		int sum = 1, row = array.length, col = array[0].length;

		// 1.遍历初始数组,得到有效数据个数
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				if (array[i][j] != invalid)
					sum++;
			}
		}

		// 2.创建对应的稀疏数组
		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;
	}

	/**
	 * <pre>
	 * 将稀疏数组还原
	 * 无效数据还原成0
	 * </pre>
	 * 
	 * @param sparseArr 稀疏数组
	 * @return 恢复后的数组
	 */
	public static int[][] toArray(int[][] sparseArr) {
		// 1.创建原始数组
		int[][] array = new int[sparseArr[0][0]][sparseArr[0][1]];
		// 2.还原数据
		for (int i = 1; i <= sparseArr[0][2]; i++) {
			array[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
		}
		return array;
	}

	/**
	 * <pre>
	 * 两数组是否相等
	 * </pre>
	 * 
	 * @param array
	 * @param sparseArr
	 * @return 两数组相等->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("原始数组:");
		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("稀疏数组:");
		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("还原后的数组:");
		for (var is : b) {
			for (var n : is)
				System.out.print(n);
			System.out.println();
		}

		System.out.println("-----------------------------");
		System.out.println((equal(a, b)) ? "两数组相同" : "两数组不相同");
	}

}
  • operation result
    operation resultoperation result
Published an original article · won praise 0 · Views 42

Guess you like

Origin blog.csdn.net/weixin_43905191/article/details/104317326