特殊矩陣的處理 實驗5:稀疏矩陣ADT的十字鏈表實現,矩陣乘法/加法/轉置

稀疏矩陣ADT的實現:

在現實應用中,一些規模很大的特殊矩陣具有重要的地位。特殊矩陣可以採用二維數組存儲,簡單直接(順序存儲方式保持了矩陣中元素之間的二維線性關係),矩陣操作的算法都很簡單,但是其空間的利用率很低(因爲重複元素或零元素比較多)。 稀疏矩陣就是一種應用很廣泛的特殊的矩陣,在實現稀疏矩陣ADT時通常採用“壓縮”存儲方案,即把只存儲稀疏矩陣的非零元素,把稀疏矩陣抽象成爲一個以三元組(行,列,值)爲數據元素的線性表來處理,而我們知道:線性表可以採用順序存儲,也可以採用鏈式存儲(通常用十字鏈表)。

現要求編程實現稀疏矩陣在“壓縮”存儲時的常用操作,如輸出、轉置、求和、乘等。(注:在代碼註釋中說明你採用的存儲結構)

需要輸入兩個矩陣,完成:

(1) 轉置。對第一個矩陣進行轉置並輸出,前面輸出標題 “The transformed matrix is:”

(2) 矩陣加。如兩個矩陣可以相加,進行兩個矩陣加並輸出,前面輸出標題 “The added matrix is:”

               如果不能相加輸出 “Can not add!”;

(3) 矩陣乘。如果兩個矩陣可以相乘,進行兩個矩陣乘並輸出,前面輸出標題 “The product matrix is:”

                    如果不能相乘輸出 “Can not multiply!”

矩陣的輸入:有多行,第1行包括三個整數,分別是矩陣的大小m,n及非零元素的個數r。後面r行分別輸入各個非零元素的 行、列、值。

矩陣的輸出:有兩種形式,操作時分別用符號“L”、“H”指出輸出形式。

L: 以三元組的形式輸出,即先輸出矩陣的行數、列數和非零元素個數,再依次輸出各個非零元素的行、列和值。

H: 按人們習慣的矩陣格式輸出,即輸出一個m*n的矩陣,是零元素的輸出0,非零元素輸出元素值。設定每個元素佔位寬度爲4。(要輸出行號和列號,並對齊)

例如:輸入如下:

10 8 4 //第1個矩陣 10行,8列,4個非零元素

1 8 1 //第1行第8列元素值爲1

3 3 2 //第3行第3列元素值爲2

3 7 3 //第3行第7列元素值爲3

10 1 4 //第10行第1列元素值爲4

10 8 2 //第2個矩陣 10行,8列,2個非零元素

2 6 1 //第2行第6列元素值爲1

3 7 -3 //第3行第7列元素值爲-3

H //輸出格式類別

輸出如下:

The transformed matrix is:

    1   2   3   4   5   6   7   8   9  10

1 0 0 0 0 0 0 0 0 0 4

2 0 0 0 0 0 0 0 0 0 0

3 0 0 2 0 0 0 0 0 0 0

4 0 0 0 0 0 0 0 0 0 0

5 0 0 0 0 0 0 0 0 0 0

6 0 0 0 0 0 0 0 0 0 0

7 0 0 3 0 0 0 0 0 0 0

8 1 0 0 0 0 0 0 0 0 0

The added matrix is:

   1   2   3   4   5   6   7   8

1 0 0 0 0 0 0 0 1

2 0 0 0 0 0 1 0 0

3 0 0 2 0 0 0 0 0

4 0 0 0 0 0 0 0 0

5 0 0 0 0 0 0 0 0

6 0 0 0 0 0 0 0 0

7 0 0 0 0 0 0 0 0

8 0 0 0 0 0 0 0 0

9 0 0 0 0 0 0 0 0

10 4 0 0 0 0 0 0 0

Can not multiply!

思路
1.使用結構體實現
(1)存儲:位置(i,j),值aij
(2)輸出操作:先將結構體按i排序,再按j排序,然後輸出方式類似以下代碼的輸出
(3)轉置:所有結構體的i,j互換,再次排序
(4)加法:兩重循環遍歷i,j,在滿足:x1>=i,y1>=j的條件下更新矩陣M1的(x1,y1)值,矩陣M2同矩陣M1也更新(x2,y2),若x1=i,y2=j則將M1的aij的值放到求和後的矩陣M3中,矩陣M2同M1,若x1=x2,y1=y2,則將兩元素求和後的值放到M3中。M3排序
(5)乘法:類似加法的更新操作,做矩陣的乘法操作。

2.使用十字鏈表,見代碼

給組測試用例

Input:

3 3 7
1 1 3
1 2 2
2 1 2
2 2 3
2 3 2
3 2 2
3 3 3
3 3 7
1 1 1
1 3 3
2 2 2
2 3 4
3 1 3
3 2 4
3 3 3
L

Output:

The transformed matrix is:
Rows=3,Cols=3,r=7
1 1 3
1 2 2
2 1 2
2 2 3
2 3 2
3 2 2
3 3 3
The added matrix is:
Rows=3,Cols=3,r=9
1 1 4
1 2 2
1 3 3
2 1 2
2 2 5
2 3 6
3 1 3
3 2 6
3 3 6
The product matrix is:
Rows=3,Cols=3,r=9
1 1 3
1 2 4
1 3 17
2 1 8
2 2 14
2 3 24
3 1 9
3 2 16
3 3 17
///使用十字鏈表實現
#include <iostream>
#include <cstdio>

using namespace std;

const int maxn=10000;
typedef int etype;
struct node
{
    int r,c;
    etype val;
    node *right,*down,*left,*up;
    node(int r,int c,int val=0)
    {
        this->val=val;
        this->r=r,this->c=c;
        right=down=left=up=NULL;
    }
};

class CrossLinks
{
public:
    int row,col,cnt;
    node *h[maxn];///頭節點
    CrossLinks(){}
    CrossLinks(int r,int c,int n);
    void push(int r,int c,int val);
    void Hprint(int type);
    void Lprint(int type);
};

CrossLinks::CrossLinks(int r,int c,int n)
{
    row=r,col=c,cnt=n;
    for(int i=1;i<=max(col,row);i++)
        h[i]=new node(0,0);///頭節點處於第0行,第0列
    for(int i=1;i<=row;i++)///頭節點形成環
    {
        h[i]->right=h[i];
        h[i]->left=h[i];
    }
    for(int i=1;i<=col;i++)///頭節點形成環
    {
        h[i]->down=h[i];
        h[i]->up=h[i];
    }
}

void CrossLinks::push(int r,int c,int val)
{
    node *row=h[r]->right,*col=h[c]->down;
    node *temp=new node(r,c,val);
    ///行插入
    while(temp->c<row->c) row=row->left;
    row->left->right=temp;
    temp->left=row->left;
    row->left=temp;
    temp->right=row;
    ///列插入
    while(temp->r<col->r) col=col->up;
    col->up->down=temp;
    temp->up=col->up;
    col->up=temp;
    temp->down=col;
}

void CrossLinks::Hprint(int type)
{
    if(type==1)
    {
        printf("    ");
        for(int i=1;i<=col;i++) printf("%4d",i);
        printf("\n");
        for(int i=1;i<=row;i++)
        {
            node *head=h[i]->left;
            printf("%4d",i);///行標
            for(int j=1;j<=col;j++)
            {
                if(j<head->c||head==h[i])///小於或回到頭節點
                    printf("   0");
                else if(j==head->c)
                {
                    printf("%4d",head->val);
                    head=head->left;
                }
            }
            printf("\n");
        }
    }
    else if(type==2)///轉置輸出
    {
        printf("    ");
        for(int i=1;i<=row;i++) printf("%4d",i);
        printf("\n");
        for(int i=1;i<=col;i++)
        {
            node *head=h[i]->up;
            printf("%4d",i);///行標
            for(int j=1;j<=row;j++)
            {
                if(j<head->r||head==h[i])///小於或回到頭節點
                    printf("   0");
                else if(j==head->r)
                {
                    printf("%4d",head->val);
                    head=head->up;
                }
            }
            printf("\n");
        }
    }
}

void CrossLinks::Lprint(int type)
{
    if(type==1)
    {
        printf("Rows=%d,Cols=%d,r=%d\n",row,col,cnt);
        for(int i=1;i<=row;i++)
        {
            node *head=h[i]->left;
            while(head!=h[i])
            {
                printf("%d %d %d\n",head->r,head->c,head->val);
                head=head->left;
            }
        }
    }
    else if(type==2)///轉置輸出
    {
        printf("Rows=%d,Cols=%d,r=%d\n",col,row,cnt);
        for(int i=1;i<=col;i++)
        {
            node *head=h[i]->up;
            while(head!=h[i])
            {
                printf("%d %d %d\n",head->c,head->r,head->val);
                head=head->up;
            }
        }
    }

}
/*
///有待完善
void mar_tran(CrossLinks m)///矩陣轉置
{
    node *temp1[m.row],*temp2[m.row];
    for(int i=1;i<=m.row;i++) ///暫存行頭節點的左右節點
    {
        temp1[i]=m.h[i]->right;
        temp2[i]=m.h[i]->left;
    }
    for(int i=1;i<=m.col;i++)///行列交換
    {
        m.h[i]->left=m.h[i]->down;
        m.h[i]->right=m.h[i]->up;
        m.h[i]->up=temp1[i];
        m.h[i]->down=temp2[i];
    }
    for(int i=m.col+1;i<=m.row;i++)///行多餘了
    {
        m.h[i]->right=m.h[i];
        m.h[i]->left=m.h[i];
    }
}
*/
void mar_add(CrossLinks m1,CrossLinks m2,char type)
{
    if(m1.row!=m2.row||m1.col!=m2.col)
    {
        cout<<"Can not add!"<<endl;
        return;
    }
    cout<<"The added matrix is:"<<endl;
    CrossLinks m3(m1.row,m1.col,0);
    for(int i=1;i<=m1.row;i++)
    {
        node *head1=m1.h[i]->left;
        node *head2=m2.h[i]->left;
        while(head1!=m1.h[i]||head2!=m2.h[i])
        {
            if(head1!=m1.h[i]&&head2!=m2.h[i]&&head2->c==head1->c)
            {
                if(head1->val+head2->val)
                {
                    m3.push(head2->r,head2->c,head1->val+head2->val);
                    m3.cnt++;
                }
                head2=head2->left;
                head1=head1->left;
            }
            else if(head2==m2.h[i]||(head1!=m1.h[i]&&head1->c<head2->c))
            {
                m3.push(head1->r,head1->c,head1->val);
                m3.cnt++;
                head1=head1->left;
            }
            else if(head1==m1.h[i]||(head2!=m2.h[i]&&head2->c<head1->c))
            {
                m3.push(head2->r,head2->c,head2->val);
                m3.cnt++;
                head2=head2->left;
            }

        }
    }
    if(type=='H')m3.Hprint(1);
    if(type=='L')m3.Lprint(1);
}

void mar_mul(CrossLinks m1,CrossLinks m2,char type)
{
    if(m1.col!=m2.row)
    {
        cout<<"Can not multiply!"<<endl;
        return;
    }
    cout<<"The product matrix is:"<<endl;
    CrossLinks m3(m1.row,m2.col,0);
    for(int i=1;i<=m1.row;i++)
    {
        for(int j=1;j<=m2.col;j++)
        {
            int sum=0;
            node *head1=m1.h[i]->left;
            node *head2=m2.h[j]->up;
            while(head1!=m1.h[i]&&head2!=m2.h[j])
            {
                if(head1->c<head2->r)
                    head1=head1->left;
                else if(head2->r<head1->c)
                    head2=head2->up;
                else if(head2->r==head1->c)
                {
                    sum+=head1->val*head2->val;
                    head2=head2->up;
                    head1=head1->left;
                }
            }
            if(sum)
            {
                m3.push(i,j,sum);
                m3.cnt++;
            }
        }
    }
    if(type=='H')m3.Hprint(1);
    if(type=='L')m3.Lprint(1);
}

int main()
{
    int col,row,n;
    cin>>col>>row>>n;
    CrossLinks cl1(col,row,n);
    for(int i=1;i<=n;i++)
    {
        int c,r,val;
        cin>>c>>r>>val;
        cl1.push(c,r,val);
    }
    cl1.Hprint(2);
    mar_tran(cl1);
    cl1.Hprint(1);
    cin>>col>>row>>n;
    CrossLinks cl2(col,row,n);
    for(int i=1;i<=n;i++)
    {
        int c,r,val;
        cin>>c>>r>>val;
        cl2.push(c,r,val);
    }
    char type;
    cin>>type;
    cout<<"The transformed matrix is:"<<endl;
    if(type=='H')cl1.Hprint(2);
    if(type=='L')cl1.Lprint(2);
    mar_add(cl1,cl2,type);
    mar_mul(cl1,cl2,type);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章