关闭

Sparse Matrix's Transpose

189人阅读 评论(0) 收藏 举报
分类:

Sparse Matrix

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

Transpose Algorithm

  • The data is ordered by row.

About classical method:

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

    Time Complexity: O(munu)


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).

    Description:
    From first row, find the elem whose row-num(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 times operation.

    Time Complexity: O(nutu)

    If tumu, 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 times operation.

    Time Complexity: O(nu+tu)

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:381623次
    • 积分:6420
    • 等级:
    • 排名:第3775名
    • 原创:345篇
    • 转载:8篇
    • 译文:1篇
    • 评论:77条
    博客专栏
    最新评论