java数据结构与算法: 稀疏矩阵

稀疏矩阵

在矩阵中,若数值为0的元素数目远远多于非0元素的数目,并且非0元素分布没有规律,则称改矩阵为稀疏矩阵。反正则为稠密矩阵。

Coordinate(COO)

使用values, rows, columns 三个数组来保存非0元素的信息。这三个数组长度相同。

  1. values: 矩阵中的非零元素数据,顺序任意(一般行优先)
  2. rows: 数据所处的行
  3. columns:数据所处的列

请添加图片描述

代码实现

  1. 稀疏矩阵转化为COO表示:
public class MatrixUtil {
    public static int[][] MatrixToCOO(int[][] matrix) {
        int num = 0;
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (matrix[i][j] != 0){
                    num++;
                }
            }
        }
        int[][] Coo = new int[3][num];
        int CooNum = 0;
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (matrix[i][j] != 0){
                    Coo[0][CooNum] = i;
                    Coo[1][CooNum] = j;
                    Coo[2][CooNum] = matrix[i][j];
                    CooNum++;
                }
            }
        }
        return Coo;
    }

    public static void printMatrix(int[][] matrix) {
        for (int i = 0; i <matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println("");
        }
    }
}

测试:

public class MatrixDemo {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int rowNum = 15;    // 棋盘行数
		int columnNum = 15; // 棋盘列数
		
		// 初始化五子棋盘面
		int[][] gomokuMatrix = new int[rowNum][columnNum];
		gomokuMatrix[6][5] = 1;
		gomokuMatrix[6][8] = 2;
		gomokuMatrix[7][6] = 1;
		gomokuMatrix[7][7] = 2;
		gomokuMatrix[8][6] = 2;
		gomokuMatrix[8][7] = 1;
		gomokuMatrix[8][8] = 1;
		gomokuMatrix[9][8] = 2;

		// 打印矩阵
		MatrixUtil.printMatrix(gomokuMatrix);

		// 转为COO表示
		int[][] cooMatrix = MatrixUtil
				.MatrixToCOO(gomokuMatrix);

		// 打印矩阵
		MatrixUtil.printMatrix(cooMatrix);
	}
}

结果如下:

请添加图片描述

  1. COO表示转稀疏矩阵
public static int[][] COOToMatrix(int[][] Coo,int rows,int cols) {
    int[][] matrix = new int[rows][cols];
    for (int i = 0; i < Coo.length; i++) {
        for (int j = 0; j < Coo[0].length; j++) {
            matrix[Coo[0][j]][Coo[1][j]] = Coo[2][j];
        }
    }
    return matrix;
}

测试:

public class MatrixDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int rowNum = 15;    // 棋盘行数
		int columnNum = 15; // 棋盘列数

		// 初始化五子棋盘面
		int[][] gomokuMatrix = new int[rowNum][columnNum];
		gomokuMatrix[6][5] = 1;
		gomokuMatrix[6][8] = 2;
		gomokuMatrix[7][6] = 1;
		gomokuMatrix[7][7] = 2;
		gomokuMatrix[8][6] = 2;
		gomokuMatrix[8][7] = 1;
		gomokuMatrix[8][8] = 1;
		gomokuMatrix[9][8] = 2;

		// 打印矩阵
		MatrixUtil.printMatrix(gomokuMatrix);

		// 转为COO表示
		int[][] cooMatrix = MatrixUtil
				.MatrixToCOO(gomokuMatrix);

		// 打印矩阵
		MatrixUtil.printMatrix(cooMatrix);

		// COO表示转为稀疏数组
		int[][] matrix = MatrixUtil.COOToMatrix(cooMatrix,
				rowNum, columnNum);

		// 打印矩阵
		MatrixUtil.printMatrix(matrix);

	}

}

结果如下:

请添加图片描述

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林小果呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值