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.

import java.util.InputMismatchException;
import java.util.Scanner;
 
public class GraphIncidenceMatrix
{
    private final int MAX_ROWS ;
    private final int MAX_COLUMS;	
    private int Incidence_Matrix[][];
 
    public GraphIncidenceMatrix(int number_of_vertices, int number_of_edges)
    {
        MAX_COLUMS = number_of_edges;
        MAX_ROWS = number_of_vertices;
        Incidence_Matrix = new int[MAX_ROWS + 1][MAX_COLUMS + 1];
    }
 
    public void setVertices(int from_vertex, int to_vertex, int edge, int edge_num)
    { 
        try
        {
            Incidence_Matrix[from_vertex][edge_num] = edge;
            Incidence_Matrix[to_vertex][edge_num] = edge;
        }catch(ArrayIndexOutOfBoundsException indexBounce)
        {
            System.out.println("the vertex entered is not present");
        }
    }
 
    public int  getVertices(int edge_num, int vertex)
    {
        try
        {
            return Incidence_Matrix[vertex][edge_num];
        }catch(ArrayIndexOutOfBoundsException indexBounce)
        {
            System.out.println("the vertex entered is not present");
        }
        return -1;
    }
 
    public static void main(String...arg)
    {
        int number_of_vertices;
        int number_of_edges;
        int edge_count = 1;
        int edge_number ;
        int source;
        int destination;
 
        GraphIncidenceMatrix incedenceMatrix = null;	
        Scanner scan = new Scanner(System.in);
 
        try
        {
            System.out.println("Enter The Number Of Vertices and Edges \n");
            number_of_vertices = scan.nextInt();
            number_of_edges = scan.nextInt();
            incedenceMatrix = new GraphIncidenceMatrix(number_of_vertices, number_of_edges);
 
            System.out.println("Enter the Egdes Format :<edge-number> <source index> <destination index> \n");
            while (edge_count <= number_of_edges)
            {
                edge_number = scan.nextInt();
                source = scan.nextInt();
                destination = scan.nextInt();
                edge_count++;
                incedenceMatrix.setVertices(source, destination, 1, edge_number);
            }
 
            System.out.println("The Incendence Matrix for the given graph is ");
            for (int i = 1; i <= number_of_edges; i++)
            {
                System.out.print("\t" + i);
            }
            System.out.println();
            for (int i = 1; i <= number_of_vertices; i++)
            {
                System.out.print(i + "\t");
                for (int j = 1; j<= number_of_edges; j++)
                {
                    System.out.print(incedenceMatrix.getVertices(j, i) + "\t");
	        }
                System.out.println();
            }
        }catch(InputMismatchException inputMismatch)
        {
            System.out.println("the vertex entered is not present");
        }
        scan.close();
    }
}
$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

Related posts:

Tính trừu tượng (Abstraction) trong Java
Java String to InputStream
Java InputStream to Byte Array and ByteBuffer
Inject Parameters into JUnit Jupiter Unit Tests
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Java – Delete a File
Java Program to Implement CountMinSketch
Các kiểu dữ liệu trong java
Spring Boot - Google OAuth2 Sign-In
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Xử lý ngoại lệ trong Java (Exception Handling)
Instance Profile Credentials using Spring Cloud
Java Program to Implement ConcurrentSkipListMap API
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Introduction to Spring Data JDBC
Java Program to Implement Shunting Yard Algorithm
Case-Insensitive String Matching in Java
Java Program to Implement Hash Tables with Double Hashing
Programmatic Transaction Management in Spring
Hướng dẫn Java Design Pattern – Composite
Java – Write to File
Guide to the Java TransferQueue
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Iterable to Stream in Java
Java – Try with Resources
Spring Boot - Zuul Proxy Server and Routing
Quick Guide to Spring Controllers
Java Program to Perform the Sorting Using Counting Sort
Spring Boot - Admin Client
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph