Graph Representation using 2D Arrays in Java

«
»
This is a java program to represent graph as a 2D array. 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 2D Arrays. 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 two d array
  2. import java.util.Scanner;
  3.  
  4. public class Represent_Graph_TwoD_Array 
  5. {
  6.     private final int vertices;
  7.     private int[][] twoD_array;
  8.  
  9.     public Represent_Graph_TwoD_Array(int v) 
  10.     {
  11.         vertices = v;
  12.         twoD_array = new int[vertices + 1][vertices + 1];
  13.     }
  14.  
  15.     public void makeEdge(int to, int from, int edge) 
  16.     {
  17.         try 
  18.         {
  19.             twoD_array[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 twoD_array[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_TwoD_Array 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_TwoD_Array(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 two d array 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("Something went wrong");
  82.         }
  83.         sc.close();
  84.     }
  85. }

Output:

$ javac Represent_Graph_TwoD_Array.java
$ java Represent_Graph_TwoD_Array
 
Enter the number of vertices: 
4 
Enter the number of edges: 
5
Enter the edges: <to> <from>
1 2
2 3 
3 4
1 3
1 4
The two d array for the given graph is: 
  1 2 3 4 
1 0 1 1 1 
2 0 0 1 0 
3 0 0 0 1 
4 0 0 0 0

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

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

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 & technical discussions at Telegram SanfoundryClasses.