This is a java program to represent graph as a adjacency matrix. Nodes are arranged in matrix and at an index of i, j zero is displayed if nodes i and j are not connected, one otherwise.
Here is the source code of the Java Program to Represent Graph Using Adjacency Matrix. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
//This is a java program to represent graph as a adjacency matrix
import java.util.Scanner;
public class Represent_Graph_Adjacency_Matrix
{
private final int vertices;
private int[][] adjacency_matrix;
public Represent_Graph_Adjacency_Matrix(int v)
{
vertices = v;
adjacency_matrix = new int[vertices + 1][vertices + 1];
}
public void makeEdge(int to, int from, int edge)
{
try
{
adjacency_matrix[to][from] = edge;
}
catch (ArrayIndexOutOfBoundsException index)
{
System.out.println("The vertices does not exists");
}
}
public int getEdge(int to, int from)
{
try
{
return adjacency_matrix[to][from];
}
catch (ArrayIndexOutOfBoundsException index)
{
System.out.println("The vertices does not exists");
}
return -1;
}
public static void main(String args[])
{
int v, e, count = 1, to = 0, from = 0;
Scanner sc = new Scanner(System.in);
Represent_Graph_Adjacency_Matrix graph;
try
{
System.out.println("Enter the number of vertices: ");
v = sc.nextInt();
System.out.println("Enter the number of edges: ");
e = sc.nextInt();
graph = new Represent_Graph_Adjacency_Matrix(v);
System.out.println("Enter the edges: <to> <from>");
while (count <= e)
{
to = sc.nextInt();
from = sc.nextInt();
graph.makeEdge(to, from, 1);
count++;
}
System.out.println("The adjacency matrix for the given graph is: ");
System.out.print(" ");
for (int i = 1; i <= v; i++)
System.out.print(i + " ");
System.out.println();
for (int i = 1; i <= v; i++)
{
System.out.print(i + " ");
for (int j = 1; j <= v; j++)
System.out.print(graph.getEdge(i, j) + " ");
System.out.println();
}
}
catch (Exception E)
{
System.out.println("Somthing went wrong");
}
sc.close();
}
}
Output:
$ javac Represent_Graph_Adjacency_Matrix.java $ java Represent_Graph_Adjacency_Matrix Enter the number of vertices: 5 Enter the number of edges: 7 Enter the edges: <to> <from> 1 1 2 3 3 4 4 5 3 5 1 4 2 4 The adjacency matrix for the given graph is: 1 2 3 4 5 1 1 0 0 1 0 2 0 0 1 1 0 3 0 0 0 1 1 4 0 0 0 0 1 5 0 0 0 0 0
Related posts:
Spring Data JPA @Query
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Java 8 Collectors toMap
Creating a Generic Array in Java
Spring’s RequestBody and ResponseBody Annotations
Using a Mutex Object in Java
Java Program to Check whether Graph is a Bipartite using DFS
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
The Registration API becomes RESTful
Spring Cloud AWS – RDS
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Java Program to Implement Heap
Java Program to Implement Disjoint Sets
Marker Interface trong Java
Java Program to Implement Floyd Cycle Algorithm
Java Program to Implement HashSet API
Java Program to Implement Pollard Rho Algorithm
Spring RestTemplate Request/Response Logging
Guide to the Java TransferQueue
Check If a String Is Numeric in Java
Initialize a HashMap in Java
Debug a HttpURLConnection problem
Java – Convert File to InputStream
Java Program to Perform the Unique Factorization of a Given Number
Java Program to Implement Stein GCD Algorithm
Creating a Web Application with Spring 5
Sử dụng CyclicBarrier trong Java
Spring Boot - Logging
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
The DAO with Spring and Hibernate
Giới thiệu luồng vào ra (I/O) trong Java
Cài đặt và sử dụng Swagger UI