1. 程式人生 > >資料結構--稀疏矩陣(相乘)

資料結構--稀疏矩陣(相乘)

// RLSMatrix.cpp : Defines the entry point for the console application.
/*-----CODE FOR FUN---------------
-------CREATED BY Dream_Whui------
-------2015-2-3-------------------*/

#include "stdafx.h"
#include <iostream>
using namespace std;

#define   TRUE                    1
#define   FALSE                    0
#define   OK                    1
#define   ERROR                    0
#define   OVERFLOW                -2
#define   INFEASIBLE            -1

#define MAXSIZE 12500
#define MAXRC 100

#define      ElemType int

typedef struct
{
    int i,j;//行,列
    ElemType e;//元素值
}Triple;

typedef struct
{
    Triple  data[MAXSIZE+1];
    int        rpos[MAXRC+1];//每行第一個非零元素在data陣列中的位置
    int        mu,nu,tu;//行數,列數,元素個數
}RLSMatrix;

int MultSMatrix(RLSMatrix M, RLSMatrix N, RLSMatrix &Q)
{
    if(M.nu != N.mu)//矩陣M的列數與矩陣N的行數不等,則不能做矩陣乘運算
        return ERROR;
    Q.mu = M.mu;
    Q.nu = N.nu;
    Q.tu = 0;
    if(M.tu * N.tu == 0)//其中任意矩陣的元素個數為零,則不能做乘運算
        return ERROR;
    else
    {
        int arow;
        int ccol;
        for(arow=1; arow<=M.mu; arow++)//處理矩陣M的每一行
        {
            int ctemp[MAXRC+1] ={};
            Q.rpos[arow] = Q.tu + 1;
            int tp;
            if(arow < M.mu)
                tp = M.rpos[arow+1];//獲取矩陣M的下一行第一個非零元素在data陣列中位置
            else
                tp = M.tu+1;//若當前行是最後一行,則取最後一個元素+1
            int p;
            int brow;
            for(p=M.rpos[arow]; p<tp; p++)//對當前矩陣M中的每一個非零元素,在矩陣N中找到對應可乘元素
            {
                brow = M.data[p].j;
                int t;
                if(brow < N.mu)
                    t = N.rpos[brow+1];
                else
                    t = N.tu+1;
                int q;
                //int ccol;
                for(q=N.rpos[brow]; q<t; q++)
                {
                    ccol = N.data[q].j;
                    ctemp[ccol] += M.data[p].e * N.data[q].e;
                }
            }
            for(ccol=1; ccol<=Q.nu; ccol++)
            {
                if(ctemp[ccol])
                {
                    if(++Q.tu > MAXSIZE)
                        return ERROR;
                    Q.data[Q.tu].e = ctemp[ccol];
                    Q.data[Q.tu].i = arow;
                    Q.data[Q.tu].j = ccol;
                }
            }
        }
        return OK;
    }
}

void PrintMartix(RLSMatrix &M)
{
    int k;
    for(k=1; k<=M.tu; k++)
        cout<<"{"<<M.data[k].i<<","<<M.data[k].j<<","<<M.data[k].e<<"}"<<endl;
    cout<<endl;
}

int main(int argc, char* argv[])
{
    RLSMatrix M,N,T;
    
    M.tu = 5;
    M.mu = 3;
    M.nu = 4;

    M.rpos[1] = 1;
    M.rpos[2] = 3;
    M.rpos[3] = 4;

    M.data[1].e = 3;
    M.data[1].i = 1;
    M.data[1].j = 1;

    M.data[2].e = 5;
    M.data[2].i = 1;
    M.data[2].j = 4;

    M.data[3].e = -1;
    M.data[3].i = 2;
    M.data[3].j = 2;

    M.data[4].e = 2;
    M.data[4].i = 3;
    M.data[4].j = 1;

    M.data[5].e = 2;
    M.data[5].i = 3;
    M.data[5].j = 3;

    N.tu = 4;
    N.mu = 4;
    N.nu = 2;

    N.rpos[1] = 1;
    N.rpos[2] = 2;
    N.rpos[3] = 3;
    N.rpos[4] = 5;

    N.data[1].e = 2;
    N.data[1].i = 1;
    N.data[1].j = 2;

    N.data[2].e = 1;
    N.data[2].i = 2;
    N.data[2].j = 1;

    N.data[3].e = -2;
    N.data[3].i = 3;
    N.data[3].j = 1;

    N.data[4].e = 4;
    N.data[4].i = 3;
    N.data[4].j = 2;

    PrintMartix(M);
    PrintMartix(N);

    MultSMatrix(M,N,T);
    PrintMartix(T);
    return 0;
}