java - 操作相邻矩阵

标签 java adjacency-matrix

我有一个带有节点和边的网络图,并且我设法构建了图的相邻矩阵。 使用边权重对相邻矩阵进行采样
节点 -> {A, B, C, D}
边 -> {[A->B = 2] 、[A->D = 5] 、[C->A = 1] 、[C->B = 4] 、[D->B = ] 、[D ->C = 2]}

我的相邻网络是这样的

0  2  0  2
0  0  0  0
4  4  0  0
0  6  6  0

所以我想通过考虑非零单元格将相邻矩阵更改为这样,带有节点标签和每列的平均值

   A  B  C  D
A  0  2  0  2
B  0  0  0  0
C  4  4  0  0
D  0  6  6  0

X  4  4  6  2    <- Mean of non zero column

这是我用来创建相邻矩阵的代码, 节点.java

public class Node 
{
    public char label;
    public Node(char l)
    {
        this.label=l;
    }
}

图.java

public class Graph 
{
    public ArrayList nodes=new ArrayList();
    public double[][] adjacentMatrix;
    int size;

    public void addNode(Node n)
    {
        nodes.add(n);
    }

    public void addEdge(Node start,Node end,int weight)
    {
        if(adjacentMatrix==null)
        {
            size=nodes.size();
            adjacentMatrix=new double[size][size];
        }

        int startIndex=nodes.indexOf(start);
        int endIndex=nodes.indexOf(end);
        adjacentMatrix[startIndex][endIndex]=weight;
    }

    public static void printAdjacentMatrix(double matrix[][]) {
         for (int row = 0; row < matrix.length; row++) {
                for (int column = 0; column < matrix[row].length; column++) {
                    System.out.print(matrix[row][column] + " ");
                }
                System.out.println();
            }   
    }
}

Main.java

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //Defining nodes
        Node nA=new Node('A');
        Node nB=new Node('B');
        Node nC=new Node('C');
        Node nD=new Node('D');

        //Creating adjacent matrix
        Graph g=new Graph();
        g.addNode(nA);
        g.addNode(nB);
        g.addNode(nC);
        g.addNode(nD);

        g.addEdge(nA, nB, 2);
        g.addEdge(nA, nD, 2);
        g.addEdge(nC, nA, 4);
        g.addEdge(nC, nB, 4);
        g.addEdge(nD, nB, 6);
        g.addEdge(nD, nC, 6);

        g.printAdjacentMatrix(g.adjacentMatrix);

    }

}

所以我请求帮助来显示带有平均值和标签的第二个矩阵...提前谢谢您

最佳答案

这不是一个很好的解决方案,但这样就可以了。

public class Graph {
    public ArrayList nodes = new ArrayList();
    public int[][] adjacentMatrix;
    int size;

    public void addNode(Node n) {
    nodes.add(n);
    }

    public void addEdge(Node start, Node end, int weight) {
    if (adjacentMatrix == null) {
        size = nodes.size();
        adjacentMatrix = new int[size][size];
    }
    int startIndex = nodes.indexOf(start);
    int endIndex = nodes.indexOf(end);
    adjacentMatrix[startIndex][endIndex] = weight;
    }

    public static void printAdjacentMatrix(int matrix[][]) {
    for (int row = 0; row < matrix.length; row++) {
        for (int column = 0; column < matrix[row].length; column++) {
        System.out.print(matrix[row][column] + " ");
        }
        System.out.println();
    }
    }

    public static void convertMatrix(int matrix[][]) {
    int row = matrix.length + 2;
    int column = matrix[0].length + 1;
    String newMatrix[][] = new String[row][column];
    initializeFirstRow(newMatrix);
    initializeFirstColumn(newMatrix);
    copyMatrix(matrix, newMatrix);
    addMean(matrix, newMatrix);
    printAdjacentMatrix(newMatrix);
    }

    private static void initializeFirstColumn(String[][] newMatrix) {
    newMatrix[1][0] = "A";
    newMatrix[2][0] = "B";
    newMatrix[3][0] = "C";
    newMatrix[4][0] = "D";
    newMatrix[5][0] = "X";
    }

    private static void printAdjacentMatrix(String[][] newMatrix) {
    for (int row = 0; row < newMatrix.length; row++) {
        for (int column = 0; column < newMatrix[row].length; column++) {
        System.out.print(newMatrix[row][column] + " ");
        }
        System.out.println();
    }
    }

    private static void addMean(int[][] matrix, String[][] newMatrix) {
    int mean = 0;
    int sum = 0;
    int divident = 0;
    for (int j = 0; j < matrix[0].length; j++) {
        sum = 0;
        divident = 0;
        for (int i = 0; i < matrix.length; i++) {
        if (matrix[i][j] != 0) {
            sum += matrix[i][j];
            divident++;
        }
        }
        if (sum != 0) {
        mean = sum / divident;
        }
        newMatrix[5][j + 1] = "" + mean;
    }
    }

    private static void copyMatrix(int[][] matrix, String[][] newMatrix) {
    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix[0].length; j++) {
        newMatrix[i + 1][j + 1] = "" + matrix[i][j];
        }
    }
    }

    private static void initializeFirstRow(String[][] newMatrix) {
    newMatrix[0][0] = " ";
    newMatrix[0][1] = "A";
    newMatrix[0][2] = "B";
    newMatrix[0][3] = "C";
    newMatrix[0][4] = "D";
    }
}

还在 Main.java 中添加以下行

g.convertMatrix(g.adjacentMatrix);

关于java - 操作相邻矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25016055/

相关文章:

java - Android Canvas 与图像

java - 重新调整保留在父 JFrame 内的 JInternalFrame 的大小,并且该父 JFrame 已使用 ComponentMover API 注册

java - @JoinTable 与 WHERE

c++ - C++-我应该使用Dijkstra的哪些数据结构?

c - 邻接矩阵中的传递关系

python - 如何使用 python 从 G(n,p) 图创建邻接矩阵?

java - JButton Action 监听器不起作用

java - 如何使用数据存储 api 对子实体进行排序

java - Dijkstra 用于邻接矩阵、最短且最便宜的路径、单一源、单一目标

algorithm - 使用邻接矩阵修改深度优先搜索算法来搜索特定的端节点