# Sparse Matrix's Transpose

189人阅读 评论(0)

## Sparse Matrix

Usually, suppose that there is m$m$ rows and n$n$ cols, and t$t$ non-zero element in a matrix. Make δ=t/(mn)$\delta = t/(m*n)$, δ$\delta$ is called sparse factor. If δ0.05$\delta \le 0.05$, we could call the matrix a sparse matrix.

## Transpose Algorithm

• The data is ordered by row.

• According to matrix’s diagonal, making half of number of all element( munu$mu * nu$) tranpose is a easy way to realize matrix’s transpose.

Time Complexity: O(munu)$O(mu * nu)$

There are two methods.
• First

Requirement:
The result must orderly by col. If don’t require the result is orderly, We could do this in time O(nu)$O(nu)$.

Description:
From first row, find the elem whose row-num(i$i$) is this row, and transpose to col.

// M is source Matrix, T is target Matrix
// mu is row-num, nu is col-num, tu is non-zero-num
// .i is row, .j is col, .e is elem

Status TranposeMatrix(Matrix M, Matrix &T) {
T.mu = M.nu, T.nu = m.Mu, T.tu = M.tu;
if (T.mu) {
q = 1;
for(col = 1; col <= M.nu; ++col)
for(p = 1; p <= M.tu; ++p)
if(M.data[p].j == col) {
T.data[q].i = M.data[p].i;
T.data[q].j = M.data[p].j;
T.data[q].e = M.data[p].e;
++q;
}
}
return OK;
}

Obviously, there is nutu$nu * tu$ times operation.

Time Complexity: O(nutu)$O(nu * tu)$

If tumu$tu \ge mu$, this method is bad.

• second

Name: Fast Transpose

Description:
If we could determine every col of result serial number, we could make every data right location.

Known:
cpot[1] = 1;
cpot[col] = cpot[col- 1] + num[col- 1];

Because the source data is ordered by row, we could make every col a point to mark this col’s the data sequence.

Status FastTransposeMatrix(Matrix M, Matrix &T) {
T.mu = M.nu; T.nu = M.mu; T.tu = M.tu;
if(T.tu) {
for(col = 1; col <= M.nu; ++col) num[col] = 0;
for(t = 1; t <= M.tu; ++t) ++num[M.data[t].j];
cpot[i] = 1;

for (col = 2; col <= M.nu; ++col) cpot[col] = cpot[col - 1] + num [col - 1];
for (p= 1; p<= M.tu; ++p) {
col = M.data[p].j;
q = cpot[col];
T.data[q].i = M.data[p].j;
T.data[q].j = M.data[p].i;
T.data[q].e = M.data[p].e;
++ cpot[col];
}
}
}

Obviously, there is nu+tu$nu + tu$ times operation.

Time Complexity: O(nu+tu)$O(nu + tu)$

In worst situation, tu=munu$tu = mu * nu$, this time is O(munu)$O(mu * nu)$, it still works well.

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：381623次
• 积分：6420
• 等级：
• 排名：第3775名
• 原创：345篇
• 转载：8篇
• 译文：1篇
• 评论：77条
博客专栏
 图像语义分割 文章：19篇 阅读：12159
最新评论