溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C++實現稀疏矩陣的壓縮存儲、轉置、快速轉置代碼分享

發布時間:2020-05-08 14:56:00 來源:億速云 閱讀:575 作者:Leah 欄目:編程語言

本文在介紹關于C++實現稀疏矩陣的壓縮存儲、轉置、快速轉置的基礎上,重點探討了其具體步驟分享了代碼,本文內容緊湊,希望大家可以有所收獲。

/*稀疏矩陣的壓縮存儲、轉置、快速轉置*/
#include <iostream>
using namespace std;
#include <vector>

//三元組
template<class T>
struct Triple
{
	size_t _row;
	size_t _col;
	T _value;

	Triple(size_t row = 0, size_t col = 0, const T& value = T())
		:_row(row)
		,_col(col)
		,_value(value)
	{}
};

template<class T>
class SparseMatrix
{
public:
	SparseMatrix(T* a = NULL, size_t M = 0, size_t N = 0, const T& invalid = T())
		:_rowSize(M)
		,_colSize(N)
		,_invalid(invalid)
	{
		for (size_t i = 0; i < M; ++i)
		{
			for (size_t j = 0; j < N; ++j)
			{
				if (a[i*N+j] != _invalid)
				{
					Triple<T> t;
					t._row = i;
					t._col = j;
					t._value = a[i*N+j];

					_a.push_back(t);
				}
			}
		}
	}

	void Display()
	{
		size_t index = 0;

		for (size_t i = 0; i < _rowSize; ++i)
		{
			for (size_t j = 0; j < _colSize; ++j)
			{
				if (index < _a.size()
					&& (_a[index]._row == i)
					&& (_a[index]._col == j))
				{
					cout<<_a[index++]._value<<" ";
				}
				else
				{
					cout<<_invalid<<" ";
				}
			}
			
			cout<<endl;
		}
	}

	//矩陣轉置 時間復雜度為 O(有效數據的個數*原矩陣的列數)
	SparseMatrix<T> Transport()
	{
		SparseMatrix<T> sm;
		sm._colSize = _rowSize;
		sm._rowSize = _colSize;
		sm._invalid = _invalid;

		for (size_t i = 0; i < _colSize; ++i)
		{
			size_t index = 0;

			while (index < _a.size())
			{
				if (_a[index]._col == i)
				{
					Triple<T> t;
					t._row = _a[index]._col;
					t._col = _a[index]._row;
					t._value = _a[index]._value;

					sm._a.push_back(t);
				}

				++index;
			}
		}

		return sm;
	}

	//快速轉置 時間復雜度為O(有效數據的個數+原矩陣的列數)
	SparseMatrix<T> FastTransport()
	{
		SparseMatrix<T> sm;
		sm._rowSize = _colSize;
		sm._colSize = _rowSize;
		sm._invalid = _invalid;

		int* RowCounts = new int[_colSize];
		int* RowStart = new int [_colSize];
		memset(RowCounts, 0, sizeof(int)*_colSize);
		memset(RowStart, 0, sizeof(int)*_colSize);
		
		size_t index = 0;
		while (index < _a.size())
		{
			++RowCounts[_a[index]._col];
			++index;
		}

		for (size_t i = 1; i < _colSize; ++i)
		{
			RowStart[i] = RowStart[i-1] + RowCounts[i-1];
		}

		index = 0;
		sm._a.resize(_a.size());
		while (index < sm._a.size())
		{
			Triple<T> t;
			t._row = _a[index]._col;
			t._col = _a[index]._row;
			t._value = _a[index]._value;

			sm._a[RowStart[_a[index]._col]] = t;

			++RowStart[_a[index]._col];
			++index;
		}

		delete[] RowCounts;
		delete[] RowStart;

		return sm;
	}
protected:
	vector<Triple<T>> _a;
	size_t _rowSize;
	size_t _colSize;
	T _invalid;
};

void Test()
{
	int array [6][5] = 
	{
		{1, 0, 3, 0, 5},
		{0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0},
		{2, 0, 4, 0, 6},
		{0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0}
	};

	SparseMatrix<int> sm1((int*)array, 6, 5, 0);
	sm1.Display();
	cout<<endl;
	//SprseMatrix<int> sm2 = sm1.Transport();
	SparseMatrix<int> sm2 = sm1.FastTransport();
	sm2.Display();
}

int main()
{
	Test();

	return 0;
}

C++實現稀疏矩陣的壓縮存儲、轉置、快速轉置代碼分享

看完上訴內容,你們掌握C++實現稀疏矩陣的壓縮存儲、轉置、快速轉置的方法了嗎?如果想了解更多相關內容,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女