数据结构-矩阵-稀疏矩阵(Java语言)

详细的代码可见github:

https://github.com/AbitGo/myClassWork/tree/master/workspace_ds

矩阵中,若数值为0的元素数目远远多于非0元素的数目,并且非0元素分布没有规律时,则称该矩阵为稀疏矩阵;与之相反,若非0元素数目占大多数时,则称该矩阵为稠密矩阵。定义非零元素的总数比上矩阵所有元素的总数为矩阵的稠密度。

具体实现类:

package com.company.ch4.Matrix;

import com.company.ch4.TripleNode;

public class SparseMatrix {
    private TripleNode data[];
    private int rows;
    private int cols;
    private int nums;

    public SparseMatrix(int maxSize) {
        data = new TripleNode[maxSize];
        for (int i = 0; i < data.length; i++) {
            data[i] = new TripleNode();
        }
        this.rows = 0;
        this.cols = 0;
        this.nums = 0;
    }

    public SparseMatrix(int mat[][]){
        int count = 0;
        this.rows = mat.length;//行数
        this.cols = mat[0].length;//列数

        for(int i=0;i<rows;i++){
            for(int j=0;j<cols;j++){
                if(mat[i][j]!=0){
                    count++;
                }
            }
        }
        this.nums = count;
        int k=0;
        data = new TripleNode[this.nums];
        for(int i=0;i<rows;i++){
            for(int j=0;j<cols;j++){
                if(mat[i][j]!=0){
                    data[k++] = new TripleNode(i,j,mat[i][j]);
                }
            }
        }

    }
    public void display(){
        for(int i=0;i<this.nums;i++){
            System.out.println("行数:"+this.data[i].getRow()+" 列数:"+this.data[i].getColumn()+" 数值:"+this.data[i].getValue());
        }
    }
    public SparseMatrix transpose(){
        SparseMatrix tm = new SparseMatrix(nums);
        int count = 0;
        tm.cols = this.rows;
        tm.rows = this.cols;
        tm.nums = this.nums;
        for(int i=0;i<rows;i++){
            for(int j=0;j<nums;j++){
                if(data[j].getColumn()==i){
                    tm.data[count].setColumn(this.data[j].getRow());
                    tm.data[count].setRow(this.data[j].getColumn());
                    tm.data[count].setValue(this.data[j].getValue());
                    count++;
                }
            }
        }
        return tm;
    }

    public SparseMatrix fastTranspose(){
        SparseMatrix tm = new SparseMatrix(nums);
        int count = 0;
        tm.cols = this.rows;
        tm.rows = this.cols;
        tm.nums = this.nums;
        tm.data = new TripleNode[nums];

        int[] button = new int[nums];//存放每列非零数的数组

        for(int i=0;i<nums;i++){
            button[this.data[i].getColumn()]++;
        }
        for(int i:button){

        }

        return tm;
    }
}

测试类:

package com.company.ch4.Matrix;

public class SparseMatrixTest {
    public static void main(String[] args) {
        int a[][]={{0,0,8,0,0},{0,0,0,0,0},{5,0,0,0,16},{0,0,18,0,0},{0,0,0,9,0}};
        SparseMatrix sparseMatrix = new SparseMatrix(a);
        sparseMatrix.display();
        System.out.println();
        SparseMatrix sm1 = sparseMatrix.transpose();
        sm1.display();
        System.out.println();

    }
}

测试结果:

行数:0 列数:2 数值:8
行数:2 列数:0 数值:5
行数:2 列数:4 数值:16
行数:3 列数:2 数值:18
行数:4 列数:3 数值:9

行数:0 列数:2 数值:5
行数:2 列数:0 数值:8
行数:2 列数:3 数值:18
行数:3 列数:4 数值:9
行数:4 列数:2 数值:16
发布了84 篇原创文章 · 获赞 39 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/Abit_Go/article/details/104149494
今日推荐