Java Program to Implement Adjacency Matrix

Java program to describe the implement Adjacency Matrix. The Adjacency Matrix is used to represent a graph.The adjacency matrix of a finite graph G on n vertices is the n × n matrix where the non-diagonal entry aij is the number of edges from vertex i to vertex j, and the diagonal entry aii, depending on the convention, is either once or twice the number of edges (loops) from vertex i to itself.

Here is source code of the Java program to implement the Adjacency Matrix.The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

import java.util.InputMismatchException;
import java.util.Scanner;
 
public class AdjacencyMatrix
{
    private final int MAX_NO_OF_VERTICES;
    private int adjacency_matrix[][];
 
    public AdjacencyMatrix(int number_of_vertices)
    {
        MAX_NO_OF_VERTICES = number_of_vertices;
        adjacency_matrix = new int[MAX_NO_OF_VERTICES + 1][MAX_NO_OF_VERTICES + 1];
    }
 
    public void setEdge(int from_vertex, int to_vertex, int edge) 
    {
        try 
        {
            adjacency_matrix[from_vertex][to_vertex] = edge;
        } catch (ArrayIndexOutOfBoundsException indexBounce)
        {
            System.out.println("the vertex entered is not present");
        }
    }
 
    public int getEdge(int from_vertex, int to_vertex)
    {
        try 
        {
            return adjacency_matrix[from_vertex][to_vertex];
        } catch (ArrayIndexOutOfBoundsException indexBounce)
        {
            System.out.println("the vertex entered is not present")
        }
        return -1;
    }
 
    public static void main(String... arg) 
    {
        int number_of_vertices, count = 1;
        int source = 0, destination = 0;
        Scanner scan = new Scanner(System.in);
        AdjacencyMatrix adjacencyMatrix;
 
        try 
        {   
            System.out.println("Enter the Number of Vertices");
            number_of_vertices = scan.nextInt();
 
            System.out.println("Enter the Number of Edges"); 
            int number_of_edges = scan.nextInt();
 
            adjacencyMatrix  = new AdjacencyMatrix(number_of_vertices);
            System.out.println("Enter The Graph Egdes :<source> <destination>");
            while (count <= number_of_edges)
            {
                source = scan.nextInt();
                destination = scan.nextInt();
                adjacencyMatrix.setEdge(source, destination, 1);
                count++;
            }
            System.out.println("The adjacency matrix for given graph is");
            for (int i = 1; i <= number_of_vertices; i++)
                System.out.print(i);
 
            System.out.println();
            for (int i = 1; i <= number_of_vertices; i++) 
            {
                System.out.print(i);
                for (int j = 1; j <= number_of_vertices; j++)
                {
                    System.out.print(adjacencyMatrix.getEdge(i, j));
                } 
                System.out.println();
            }
        } catch (InputMismatchException inputMisMatch) 
        {
            System.out.println("Error in Input Format.<source index> <destination index>");
        }
        scan.close();
    }
}
$javac AdjacencyMatrix.java
$java AdjacencyMatrix
 
Enter the Number of Vertices and Edges
4 5
Enter The Graph Egdes Format : <source index> <destination index> 
 
1 2 
2 3
3 4
4 1
1 3
 
The adjacency matrix for given graph is 
 
	1	2	3	4
1	0	1	1	0	
2	0	0	1	0	
3	0	0	0	1	
4	1	0	0	0

Related posts:

Constructor Injection in Spring with Lombok
Java Program to Generate All Possible Combinations Out of a, b, c, d, e
Exploring the New Spring Cloud Gateway
Hướng dẫn Java Design Pattern – Proxy
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Spring – Injecting Collections
Java Program to Perform the Shaker Sort
Introduction to the Java NIO2 File API
Java Program to implement Bit Matrix
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
How to Set TLS Version in Apache HttpClient
How to Remove the Last Character of a String?
Logout in an OAuth Secured Application
Registration with Spring Security – Password Encoding
Spring Boot - Tomcat Port Number
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Guide To CompletableFuture
Converting Java Date to OffsetDateTime
Queue và PriorityQueue trong Java
Java Program to Represent Graph Using 2D Arrays
Java Program to Solve the Fractional Knapsack Problem
Transaction Propagation and Isolation in Spring @Transactional
Apache Commons Collections MapUtils
Java Program to Implement LinkedBlockingDeque API
Performance Difference Between save() and saveAll() in Spring Data
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
@Lookup Annotation in Spring
Java Program to Implement Jarvis Algorithm
Implementing a Binary Tree in Java
An Example of Load Balancing with Zuul and Eureka
Removing Elements from Java Collections
Java Program to Implement Queue using Two Stacks