Java Program to Describe the Representation of Graph using Incidence List

This Java program,to describe the representation of graph using incident list. Vertices and edges are stored as records or objects. Each vertex stores its incident edges, and each edge stores its incident vertices. This data structure allows the storage of additional data on vertices and edges.

Here is the source code of the Java program to describe the representation of graph using incident list. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
 
public class IncidentList
{
    private Map<Integer, List<Integer>> incidentList;
    private int numberOfVertices;
 
    public IncidentList(int numberOfVertices)
    {
        this.numberOfVertices = numberOfVertices;
        incidentList = new HashMap<Integer, List<Integer>>();
 
        for (int vertex = 1; vertex <= numberOfVertices; vertex++)
            incidentList.put(vertex, new LinkedList<Integer>());
    }
 
    public void setEdge(int sourcevertex, int destinationvertex, int edgeNumber)
    {
        List<Integer> slist = incidentList.get(sourcevertex);
        slist.add(edgeNumber);
        return;
    }
 
    public List<Integer> getEdge(int vertex)
    {
        return incidentList.get(vertex);
    }
 
    public void printIncidentList()
    {
        System.out.println("Vertex   EdgeNumber");
        for (int vertex = 1; vertex <= numberOfVertices; vertex++)
        {
            System.out.print(vertex + ":");
            List<Integer> edgeList = getEdge(vertex);
 
            for (int j = 1; ; j++)
            {
                if (j != edgeList.size())
                    System.out.print(edgeList.get(j - 1) + "\t");
                else
                {
                    System.out.print(edgeList.get(j - 1));
                    break;
                }
            }
            System.out.println();
        }
    }
 
    public static void main(String... arg)
    {
        int numberOfVertices, numberOfEdges;
        int source, destination, edgeNumber;
        int edgeCount = 1;
 
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the number of vertices");
        numberOfVertices = scanner.nextInt();
 
        IncidentList incidentList = new IncidentList(numberOfVertices);
        System.out.println("Enter the number of edges");
        numberOfEdges = scanner.nextInt();
 
        System.out.println("Enter the edges format : <edgeNumber> <source> <destination>");
        while (edgeCount <= numberOfEdges)
        {
            edgeNumber = scanner.nextInt();
            source = scanner.nextInt();
            destination = scanner.nextInt();
            incidentList.setEdge(source, destination, edgeNumber);
            edgeCount++;
        }
 
        System.out.println("\nThe Incident List is ");
        incidentList.printIncidentList();
        scanner.close();
    }
}
$javac IterativeDeepening.java
$java IterativeDeepening
Enter the number of vertices
5
Enter the number of edges
5
Enter the edges format : <edgeNumber> <source> <destination>
1 1 2
2 2 4 
3 5 4
4 4 3
5 5 1
 
The Incident List is 
Vertex   EdgeNumber
  1    : 1 5
  2    : 1 2
  3    : 4
  4    : 2 3 4
  5    : 3 5

Related posts:

Introduction to Spring Data JDBC
String Processing with Apache Commons Lang 3
Mockito and JUnit 5 – Using ExtendWith
Chuyển đổi giữa các kiểu dữ liệu trong Java
Java Program to Perform LU Decomposition of any Matrix
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Spring Boot - Rest Controller Unit Test
How to Manually Authenticate User with Spring Security
Simple Single Sign-On with Spring Security OAuth2
Java Program to Implement Ternary Search Algorithm
Java Program to Perform the Sorting Using Counting Sort
Exploring the New Spring Cloud Gateway
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Registration – Password Strength and Rules
Java Program to Implement Warshall Algorithm
Setting the Java Version in Maven
Java Program to Find the Minimum value of Binary Search Tree
Java Program to Check whether Directed Graph is Connected using BFS
Java – Get Random Item/Element From a List
The HttpMediaTypeNotAcceptableException in Spring MVC
Notify User of Login From New Device or Location
Java Program to implement Priority Queue
A Guide to LinkedHashMap in Java
Intro to Inversion of Control and Dependency Injection with Spring
Spring Boot - Cloud Configuration Server
Optional trong Java 8
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Java Convenience Factory Methods for Collections
Java Program to Implement PrinterStateReasons API
Java Program to Implement Pairing Heap
Java Program to Implement the Hill Cypher
Stack Memory and Heap Space in Java