Tuesday 28 November 2017

Java Program to Find Transpose of a Graph Matrix


Code:

import java.util.Scanner;

public class TransposeOfGraph
{
    private int transposeMatrix[][];
    private int numberOfVertices;

    public TransposeOfGraph(int numberOfVertices)
    {
        this.numberOfVertices = numberOfVertices;
        transposeMatrix = new int[numberOfVertices + 1][numberOfVertices + 1];
    }

    public int[][] transpose(int adjacencyMatrix[][])
    {
        for (int source = 1; source <= numberOfVertices; source++)
        {
            for (int destination = 1; destination <= numberOfVertices; destination++)
            {
                transposeMatrix[source][destination] = adjacencyMatrix[destination][source];
            }
        }
        return transposeMatrix;
    }

    public static void main(String... arg)
    {
        int number_of_nodes;
        Scanner scanner = null;

        System.out.println("Enter the number of nodes in the graph");
        scanner = new Scanner(System.in);
        number_of_nodes = scanner.nextInt();

        int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes + 1];
        int transpose_matrix[][];
        System.out.println("Enter the adjacency matrix");
        for (int i = 1; i <= number_of_nodes; i++)
            for (int j = 1; j <= number_of_nodes; j++)
                adjacency_matrix[i][j] = scanner.nextInt();

        TransposeOfGraph transposeOfGraph = new TransposeOfGraph(number_of_nodes);
        transpose_matrix = transposeOfGraph.transpose(adjacency_matrix);

        System.out.println("The transpose of the given graph");
        for (int i = 1; i <= number_of_nodes; i++)
            System.out.print("\t" + i);

        System.out.println();
        for (int source = 1; source <= number_of_nodes; source++)
        {
            System.out.print(source +"\t");
            for (int destination = 1; destination <= number_of_nodes; destination++)
            {
                System.out.print(transpose_matrix[source][destination] + "\t");
            }
            System.out.println();
        }
        scanner.close();
    }
}


Output:

Enter the number of nodes in the graph
4
Enter the adjacency matrix
0 0 3 0
2 0 0 0 
0 7 0 1
6 0 0 0
The transpose of the given graph
1 2 3 4
1 0 2 0 6
2 0 0 7 0
3 3 0 0 0
4 0 0 1 0



More Java Programs:



















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...