稀疏矩阵压缩存储

通常在高级语言编程中,都是用二维数组来存储矩阵的元,然而在阶数很高的矩阵中,有很多值相同的元或者是零元素。有时为了节省存储空间,可以对这类矩阵进行压缩存储。所谓压缩存储是指:为多个值相同的元只分配一个存储空间;对零元素不分配空间。

三元组书序表又称有序的双下标法,其特点:非零元在表中按行序有序存储,因此便于进行依行顺序处理的矩阵运算。但是,若需按行号存取某一行的非零元素,则需从头开始进行查找。(时间复杂度较高)

行逻辑链接的顺序表:便于随机存取任意一行的非零元。

十字链表:当矩阵的非零元个数和位置在操作过程中变化较大时,就不适宜采用顺序存储结构来表示三元组的线性表。

对于稀疏矩阵的存储结构应该根据对稀疏矩阵的操作来选择合适的存储方式,否则出现以时间换空间的情况,适得其反。以下只举例说明三元组顺序表的实现方式:

三元组顺序表:以顺序存储结构来表示三元组表。

/**
 * 三元组结构
 */
class Triple {
	int rowIndex;//非零元的行下标
	int colIndex;//非零元的列下标
	int value;//非零元的值
	
	Triple(){
		this.rowIndex = 0;
		this.colIndex = 0;
		this.value = 0;
	}
	
	Triple(int i, int j, int value){
		this.rowIndex = i;
		this.colIndex = j;
		this.value = value;
	}
}


public class SparseMatrix {
	public static void main(String[] args) {
		int[][] a = { { 0, 12, 9, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0, 0 },
				{ -3, 0, 0, 0, 0, 14, 0 }, { 0, 0, 24, 0, 0, 0, 0 },
				{ 0, 18, 0, 0, 0, 0, 0 }, { 15, 0, 0, -7, 0, 0, 0 } };

		System.out.println("稀疏矩阵转换成三元组顺序表:");
		LinkedList<?> list = constructTripleList(a);
		printTripleList(list);
		
		System.out.println("输出对应第2行的元素");
		restoreArray(list, 2);
		
		System.out.println("三元组顺序表还原成稀疏矩阵:");
		reverseToSparseMatrix(list);

	}

	/**
	 * 根据行号还原顺序表中的某一行
	 * 
	 * @param list
	 * @param rowIndex
	 * @return
	 */
	public static float[] restoreArray(LinkedList<?> list, int rowIndex) {
		float[] array = new float[7];
		Triple t = null;
		int index = 0;
		for (int i = 0; i < list.size(); i++) {
			t = (Triple) list.get(i);
			if (t.getRowIndex() == rowIndex) {
				index = t.getColIndex();
				array[index] = t.getValue();
			} else {
				// array[index]=0;
			}
			if (t.getRowIndex() > rowIndex) {
				break;
			}
		}// for_i
		
		for(int i = 0; i < array.length; i++){
			System.out.print(array[i] + " ");
		}
		System.out.println();
		return array;
	}

	/**
	 * 构造三元组顺序表
	 * 
	 * @param matrix
	 * @return
	 */
	public static LinkedList<Triple> constructTripleList(int[][] matrix) {
		Triple e = null;
		LinkedList<Triple> list = new LinkedList<Triple>();
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[0].length; j++)
				if (matrix[i][j] != 0) {
					e = new Triple(i, j, matrix[i][j]);
					list.add(e);
				}
		}

		return list;
	}

	/**
	 * 将三元组顺序表转换成矩阵并输出
	 * 
	 * @param list
	 */
	public static void reverseToSparseMatrix(List<?> list) {
		Triple t = null;
		float[][] a = new float[6][7];
		int index = 0;
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++) {
				if (index < list.size()) {
					t = (Triple) list.get(index);
					if (t.getRowIndex() == i) {
						// 肯定是存在t.getColIndex() == j的情况
						if (j == t.getColIndex()) {
							a[i][j] = t.getValue();
							index++;
							j = t.getColIndex();
						} else if (j > t.getColIndex()) {
							j = 0;
						}
					}
				}
				// 以下注释部分会存在j>t.getColIndex,而t.getColIndex的确有值的情况,若按照以下操作取值,则会丢掉前面描述的情况的值
				// if((t.getRowIndex()==i) && (t.getColIndex() == j)){
				// a[i][j] = t.getValue();
				// index++;
				// }
			}// for_j
		}// for_i

		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++) {
				System.out.print(a[i][j] + " ");
			}
			System.out.println();
		}
	}

	/**
	 * 输出稀疏矩阵
	 * 
	 * @param matrix
	 */
	public static void printSparseMatrix(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();
		}
	}

	/**
	 * 输出三元组顺序表
	 * 
	 * @param list
	 */
	public static void printTripleList(List<?> list) {
		Triple e = null;
		Iterator<?> iter = list.iterator();
		while (iter.hasNext()) {
			e = (Triple) iter.next();
			System.out.println((e.rowIndex + 1) + " " + (e.colIndex + 1) + " "
					+ e.value);
		}
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值