Java Program to Represent Graph Using Incidence Matrix

This is a java program to represent graph as a incidence list. The incidence matrix of G is a n × m matrix (b_{ij}), where n and m are the numbers of vertices and edges respectively, such that b_{ij} = 1 if the vertex v_i and edge x_j are incident and 0 otherwise.

Here is the source code of the Java Program to Represent Graph Using Incidence 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 incidence matrix
import java.util.Scanner;
 
public class Represent_Graph_Incidence_Matrix 
{
    private final int rows;
    private final int cols;
    private int[][] incidence_matrix;
 
    public Represent_Graph_Incidence_Matrix(int v, int e) 
    {
        rows = v;
        cols = e;
        incidence_matrix = new int[rows + 1][cols + 1];
    }
 
    public void makeEdge(int to, int from, int edge, int edge_number) 
    {
        try 
        {
            incidence_matrix[to][edge_number] = edge;
            incidence_matrix[from][edge_number] = edge;
        }
        catch (ArrayIndexOutOfBoundsException index) 
        {
            System.out.println("The vertices does not exists");
        }
    }
 
    public int getEdge(int edge_number, int v) 
    {
        try 
        {
            return incidence_matrix[edge_number][v];
        }
        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, edge_number;
        Scanner sc = new Scanner(System.in);
        Represent_Graph_Incidence_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_Incidence_Matrix(v, e);
 
            System.out.println("Enter the edges: <edge_number> <to> <from>");
            while (count <= e) 
            {
                edge_number = sc.nextInt();
                to = sc.nextInt();
                from = sc.nextInt();
 
                graph.makeEdge(to, from, 1, edge_number);
                count++;
            }
 
            System.out.println("The incidence 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_Incidence_Matrix.java
$ java Represent_Graph_Incidence_Matrix
 
Enter the number of vertices: 
4 
Enter the number of edges: 
5
Enter the edges: <edge_number> <to> <from>
1 1 2
2 2 3
3 3 4 
4 4 1
1 1 3
The incidence matrix for the given graph is: 
  1 2 3 4 
1 1 0 0 1 
2 1 1 0 0 
3 1 1 1 0 
4 0 0 1 1

Related posts:

HttpClient 4 Cookbook
Java Program to Implement the Checksum Method for Small String Messages and Detect
How to Get the Last Element of a Stream in Java?
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Ignore Null Fields with Jackson
Xây dựng ứng dụng Client-Server với Socket trong Java
Remove the First Element from a List
Hướng dẫn Java Design Pattern – Intercepting Filter
Supplier trong Java 8
Feign – Tạo ứng dụng Java RESTful Client
Java Program to Implement ArrayList API
Logging in Spring Boot
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Java Program to implement Circular Buffer
Encode a String to UTF-8 in Java
Java Program to Implement Maximum Length Chain of Pairs
Java Program to Decode a Message Encoded Using Playfair Cipher
Dynamic Proxies in Java
Logout in an OAuth Secured Application
Guide to the Synchronized Keyword in Java
Java Program to Implement the Monoalphabetic Cypher
Java Program to Implement Circular Singly Linked List
Java Program to Find kth Largest Element in a Sequence
HttpClient Timeout
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Java Program to Implement Ternary Heap
Apache Commons Collections BidiMap
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Java Program to Implement HashTable API
Kiểu dữ liệu Ngày Giờ (Date Time) trong java