Java Program to Describe the Representation of Graph using Incidence Matrix

This Java program, represents a given graph in the incident matrix form.

Here is the source code of the Java program to represent the graph in incident matrix. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3.  
  4. public class GraphIncidenceMatrix
  5. {
  6.     private final int MAX_ROWS ;
  7.     private final int MAX_COLUMS;	
  8.     private int Incidence_Matrix[][];
  9.  
  10.     public GraphIncidenceMatrix(int number_of_vertices, int number_of_edges)
  11.     {
  12.         MAX_COLUMS = number_of_edges;
  13.         MAX_ROWS = number_of_vertices;
  14.         Incidence_Matrix = new int[MAX_ROWS + 1][MAX_COLUMS + 1];
  15.     }
  16.  
  17.     public void setVertices(int from_vertex, int to_vertex, int edge, int edge_num)
  18.     { 
  19.         try
  20.         {
  21.             Incidence_Matrix[from_vertex][edge_num] = edge;
  22.             Incidence_Matrix[to_vertex][edge_num] = edge;
  23.         }catch(ArrayIndexOutOfBoundsException indexBounce)
  24.         {
  25.             System.out.println("the vertex entered is not present");
  26.         }
  27.     }
  28.  
  29.     public int  getVertices(int edge_num, int vertex)
  30.     {
  31.         try
  32.         {
  33.             return Incidence_Matrix[vertex][edge_num];
  34.         }catch(ArrayIndexOutOfBoundsException indexBounce)
  35.         {
  36.             System.out.println("the vertex entered is not present");
  37.         }
  38.         return -1;
  39.     }
  40.  
  41.     public static void main(String...arg)
  42.     {
  43.         int number_of_vertices;
  44.         int number_of_edges;
  45.         int edge_count = 1;
  46.         int edge_number ;
  47.         int source;
  48.         int destination;
  49.  
  50.         GraphIncidenceMatrix incedenceMatrix = null;	
  51.         Scanner scan = new Scanner(System.in);
  52.  
  53.         try
  54.         {
  55.             System.out.println("Enter The Number Of Vertices and Edges \n");
  56.             number_of_vertices = scan.nextInt();
  57.             number_of_edges = scan.nextInt();
  58.             incedenceMatrix = new GraphIncidenceMatrix(number_of_vertices, number_of_edges);
  59.  
  60.             System.out.println("Enter the Egdes Format :<edge-number> <source index> <destination index> \n");
  61.             while (edge_count <= number_of_edges)
  62.             {
  63.                 edge_number = scan.nextInt();
  64.                 source = scan.nextInt();
  65.                 destination = scan.nextInt();
  66.                 edge_count++;
  67.                 incedenceMatrix.setVertices(source, destination, 1, edge_number);
  68.             }
  69.  
  70.             System.out.println("The Incendence Matrix for the given graph is ");
  71.             for (int i = 1; i <= number_of_edges; i++)
  72.             {
  73.                 System.out.print("\t" + i);
  74.             }
  75.             System.out.println();
  76.             for (int i = 1; i <= number_of_vertices; i++)
  77.             {
  78.                 System.out.print(i + "\t");
  79.                 for (int j = 1; j<= number_of_edges; j++)
  80.                 {
  81.                     System.out.print(incedenceMatrix.getVertices(j, i) + "\t");
  82. 	        }
  83.                 System.out.println();
  84.             }
  85.         }catch(InputMismatchException inputMismatch)
  86.         {
  87.             System.out.println("the vertex entered is not present");
  88.         }
  89.         scan.close();
  90.     }
  91. }


$javac GraphIncidenceMatrix.java
$java GrapIncidenceMatrix
Enter The Number Of Vertices and Edges
4 5
Enter the Egdes Format :<edge-number> <source index> <destination index> 
 
1 1 2
2 2 3 
3 3 4
4 4 1
5 1 3
The Incendence Matrix for the given graph is 
	1	2	3	4	5
1	1	0	0	1	1	
2	1	1	0	0	0	
3	0	1	1	0	1	
4	0	0	1	1	0

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

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.