Search code examples
javaadjacency-matrix

Manipulating adjacent matrix


I have a network graph with nodes and edges and i managed to construct an adjacent matrix of my graph. sample adjacent matrix with edge weight
Nodes -> {A, B, C, D}
Edges -> {[A->B = 2] , [A->D = 5] , [C->A = 1] , [C->B = 4] , [D->B = ] , [D->C = 2]}

my adjacent network is like this

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

so i want to change the adjacent matrix to be like this with labels of the nodes and average of each column by considering non zero cells

   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

here is my the code i used to create adjacent matrix, Node.java

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

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

    }

}

so i ask for help to display the second matrix with average and labels...Thank you in advance


Solution

  • Not a very nice solution but that will do.

    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";
        }
    }
    

    Also add following line in Main.java

    g.convertMatrix(g.adjacentMatrix);