Graph Representation using Adjacency Matrix in Java

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.

  1. //This is a java program to represent graph as a adjacency matrix
  2. import java.util.Scanner;
  3.  
  4. public class Represent_Graph_Adjacency_Matrix 
  5. {
  6.     private final int vertices;
  7.     private int[][] adjacency_matrix;
  8.  
  9.     public Represent_Graph_Adjacency_Matrix(int v) 
  10.     {
  11.         vertices = v;
  12.         adjacency_matrix = new int[vertices + 1][vertices + 1];
  13.     }
  14.  
  15.     public void makeEdge(int to, int from, int edge) 
  16.     {
  17.         try 
  18.         {
  19.             adjacency_matrix[to][from] = edge;
  20.         }
  21.         catch (ArrayIndexOutOfBoundsException index) 
  22.         {
  23.             System.out.println("The vertices does not exists");
  24.         }
  25.     }
  26.  
  27.     public int getEdge(int to, int from) 
  28.     {
  29.         try 
  30.         {
  31.             return adjacency_matrix[to][from];
  32.         }
  33.         catch (ArrayIndexOutOfBoundsException index) 
  34.         {
  35.             System.out.println("The vertices does not exists");
  36.         }
  37.         return -1;
  38.     }
  39.  
  40.     public static void main(String args[]) 
  41.     {
  42.         int v, e, count = 1, to = 0, from = 0;
  43.         Scanner sc = new Scanner(System.in);
  44.         Represent_Graph_Adjacency_Matrix graph;
  45.         try 
  46.         {
  47.             System.out.println("Enter the number of vertices: ");
  48.             v = sc.nextInt();
  49.             System.out.println("Enter the number of edges: ");
  50.             e = sc.nextInt();
  51.  
  52.             graph = new Represent_Graph_Adjacency_Matrix(v);
  53.  
  54.             System.out.println("Enter the edges: <to> <from>");
  55.             while (count <= e) 
  56.             {
  57.                 to = sc.nextInt();
  58.                 from = sc.nextInt();
  59.  
  60.                 graph.makeEdge(to, from, 1);
  61.                 count++;
  62.             }
  63.  
  64.             System.out.println("The adjacency matrix for the given graph is: ");
  65.             System.out.print("  ");
  66.             for (int i = 1; i <= v; i++)
  67.                 System.out.print(i + " ");
  68.             System.out.println();
  69.  
  70.             for (int i = 1; i <= v; i++) 
  71.             {
  72.                 System.out.print(i + " ");
  73.                 for (int j = 1; j <= v; j++) 
  74.                     System.out.print(graph.getEdge(i, j) + " ");
  75.                 System.out.println();
  76.             }
  77.  
  78.         }
  79.         catch (Exception E) 
  80.         {
  81.             System.out.println("Somthing went wrong");
  82.         }
  83.  
  84.         sc.close();
  85.     }
  86. }

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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.