稀疏矩阵是指在矩阵中大部分元素为零的情况下,仅对非零元素进行存储和操作的矩阵。在实际应用中,常常会遇到大规模的矩阵,而这些矩阵中非零元素的比例很小,这时候使用稀疏矩阵的存储方式可以大大节省存储空间和计算时间。
举例:有如下矩阵(多为0):
0 0 0 0 0 0 0 0 0
0 0 0 0 0 3 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 4 0 1 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0
稀疏矩阵的改写【默认为3列】:
9 9 4 //第一行:[0][0]表示原矩阵的行数 [0][1]表示原矩阵的列数 [0][2]代表非零值的个数
1 5 3 //第二行:[1][0]非零值所在的行,[1][1]非零值所在的列,[1][2]非零值的值
3 5 4 //同上
3 7 1
7 8 1
import java.util.Arrays;
import java.util.Random;
public class Sparsearray {
public static void main(String[] args) {
System.out.println("Generate a original matrix");
int[][] randomArray = randomArray(9, 9, 4);
System.out.println("Show this matrix");
printArray(randomArray);
System.out.println("Convert to sparse matrix");
int[][] newSparseMatrix = toSparseMatrix(randomArray, 4);
System.out.println("Show this newSparseMatrix");
printArray(newSparseMatrix);
System.out.println("Convert to original matrix");
int[][] originalMatrix = toOriginalMatrix(newSparseMatrix);
System.out.println("Show this originalMatrix");
printArray(originalMatrix);
}
/**
* Generate a original matrix
* @param m number of rows
* @param n number of columns
* @param p number of non-zero value
* @return
*/
public static int[][] randomArray(int m,int n,int p) {
int[][] randomArray = new int[m][n];
//Execute p times
for (int i = 0; i < p; i++) {
randomArray[(int)(Math.random() * m)][(int)(Math.random() * n)] = (int)(Math.random() * 9 + 1);
}
return randomArray;
}
/**
* Convert to sparse matrix
* @param originalMatrix
* @param p number of non-zero value
* @return
*/
public static int[][] toSparseMatrix(int[][] originalMatrix,int p) {
int[][] newSparseMatrix = new int[p+1][3];
newSparseMatrix[0][0] = originalMatrix.length;
newSparseMatrix[0][1] = originalMatrix[0].length;
newSparseMatrix[0][2] = p;
int count = 0;
for (int i = 0; i < originalMatrix.length; i++) {
for (int j = 0; j < originalMatrix[i].length; j++) {
if (originalMatrix[i][j] != 0) {
count++;
newSparseMatrix[count][0] = i;
newSparseMatrix[count][1] = j;
newSparseMatrix[count][2] = originalMatrix[i][j];
}
}
}
return newSparseMatrix;
}
/**
* Convert to original matrix
* @param sparseMatrix
* @return
*/
public static int[][] toOriginalMatrix(int[][] sparseMatrix){
int[][] originalMatrix = new int[sparseMatrix[0][0]][sparseMatrix[0][1]];
for (int count = 1; count <= sparseMatrix[0][2]; count++) {
originalMatrix[sparseMatrix[count][0]][sparseMatrix[count][1]] = sparseMatrix[count][2];
}
return originalMatrix;
}
/**
* Display Matrix to Console
* @param matrix
*/
public static void printArray(int[][] matrix) {
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[i].length;j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
输出结果如下:
Generate a original matrix
Show this matrix
0 0 0 0 0 0 0 0 0
0 0 0 0 0 5 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 4 0 0 0
0 0 0 0 0 0 0 5 0
0 0 0 4 0 0 0 0 0
Convert to sparse matrix
Show this newSparseMatrix
9 9 4
1 5 5
6 5 4
7 7 5
8 3 4
Convert to original matrix
Show this originalMatrix
0 0 0 0 0 0 0 0 0
0 0 0 0 0 5 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 4 0 0 0
0 0 0 0 0 0 0 5 0
0 0 0 4 0 0 0 0 0