数据结构复习9 数组

本文深入探讨了稀疏矩阵和对称矩阵的压缩存储技术,包括使用三元组结构进行稀疏矩阵的存储,以及对称矩阵的压缩存储方法。通过具体的C++实现代码,展示了如何有效地减少存储空间,提高矩阵运算效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

稀疏矩阵1

/* Trituple.cpp*/
#include<iostream.h>
#include<stdlib.h>
using namespace std;
const int DefaultSize=100;
template<class T>
struct Trituple{
	int row,col;
	T value;
	Trituple<T>& operator=(Trituple<T> &t){
		row=t.row;col=t.col;value.t.value;
	}
};

template<class T>
class SparseMatrix{
	friend ostream& operator<<(ostream &out,SparseMatrix<T> &m);
	friend istream& operator>>(istream &in,SparseMatrix<T> &m);
	protected:
	int rows,cols,terms;
	Trituple<T> *smArray;
	int maxTerms;
	public:
	SparseMatrix(int maxSize=DefaultSize);
	SparseMatrix(SparseMatrix<T> &x);
	~SparseMatrix(){delete []smArray;}
	SparseMatrix<T>& operator=(SparseMatrix<T> &sm);
	SparseMatrix<T> Transpose();
	SparseMatrix<T> Add(SparseMatrix<T> &sm);
	SparseMatrix<T> Multiply(SparseMatrix<T> &sm);
};

template<class T>
SparseMatrix<T>::SparseMatrix(int maxSize):maxTerms(maxSize){
	assert(maxSize>=1);
	smArray= new Trituple<T>[maxSize];
	assert(smArray!=NULL);
	rows=cols=terms=0;
}

template<class T>
SparseMatrix<T>::SparseMatrix(SparseMatrix<T> &x){
	rows=x.rows;
	terms=x.terms;
	cols=x.cols;
	maxTerms=x.maxTerms;
	smArray=new Trituple<T>[x.maxTerms];
	assert(smArray!=NULL);
	for(int i=0;i<terms;i++){
		smArray[i]=x.smArray[i];
	}
}

template<class T>
ostream& operator<<(ostream &out,SparseMatrix<T> &x){
	out<<"row:"<<x.rows<<endl;
	out<<"col:"<<x.cols<<endl;
	out<<"none zero terms:"<<x.terms<<endl;
	for(int i=0;i<x.terms;i++){
		out<<"X["<<x.rows<<"]"<<"["<<x.cols<<"]="<<x.smArray[i]<<endl;
	}
	return out;
}

template<class T>
istream& operator>>(istream &in,SparseMatrix<T> &x){
	in>>x.rows>>x.cols>>x.terms;
	assert(x.terms<=x.maxTerms);
	for(int i=0;i<x.terms;i++){
		in>>x.smArray[i].row>>x.smArray[i].col>>x.smArray[i].value;
	}
	return in;
}

template<class T>
SparseMatrix<T> SparseMatrix<T>::Transpose(){
	SparseMatrix<T> newMat=new SparseMatrix<T>(maxTerms);
	newMat.rows=cols;
	newMat.cols=rows;
	newMat.terms=terms;
	if(terms>0){
		int k,i,bp;
		//exchange col and row
		for(k=0;k<cols;k++)
			for(i=0;i<terms;i++)
				if(smArray[i).col==k){
					newMat.smArray[pb].row=smArray[i].col;
					newMat.smArray[pb].col=smArray[i].row;
					newMat.smArray[pb].value=smArray[i].value;
					pb++;
				}
	}
	return newMat;
}

/* transpose in O(cols*terms) */


/* better choice */
template<class T>
SparseMatrix<T> SparseMatrix<T>::FastTranspose(){
	int *rowSize=new int[cols];
	int *rowStart=new int[cols];
	SparseMatrix<T> b(maxTerms);
	b.rows=cols;b.cols=row;b.terms=terms;
	if(terms>0){
		int i,j
		for(i=0;i<cols;i++)
			rowSize[i]=0;
		for(i=0;i<terms;i++)
			rowSize[smArray[i].col]++;
		rowStart[0]=0;
		for(i=1;i<cols;i++)
			rowStart[i]=rowStart[i-1]+rowSize[i-1];
		for(i=0;i<terms;i++){
			j=rowStart[smArray[i].col];
			b.smArray[j].row=smArray[i].col;
			b.smArray[j].col=smArray[i].row;
			b.smArray[j].value=smArray[i].value;
			rowStart[smArray[i].col]++;
		}
			
			
	}
	delete []rowSize;delete []rowStart;
}

对称矩阵和三对角矩阵的压缩

void SquareMatrix(int *dest,int **src,int n){
	for(int i=0;i<n;i++)
		for(int j=0;j<=i;j++){
			dest[(i+1)*i/2+j]=src[i][j];
		//  dest[2i+j-3]=src[i][j];
		//	
		}
}

void SquareMatrix(vector<int> &dest,const vector<vector<int> > &src){
	dest=new vector<int>(src.size());
	for(int i=0;i<src.size();i++)
		for(int j=0;j<=i;j++){
			dest[(i+1)*i/2+j]=src[i][j];
		}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值