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:
Prevent Cross-Site Scripting (XSS) in a Spring Application
Request Method Not Supported (405) in Spring
Spring MVC and the @ModelAttribute Annotation
Java Program to Perform Naive String Matching
Java Program to Implement SimpeBindings API
Java Program to Optimize Wire Length in Electrical Circuit
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Spring Boot - Cloud Configuration Server
Servlet 3 Async Support with Spring MVC and Spring Security
Hướng dẫn Java Design Pattern – Mediator
Java Program to Implement Treap
Spring Boot - Creating Docker Image
Adding a Newline Character to a String in Java
Java Program to Convert a Decimal Number to Binary Number using Stacks
New Features in Java 14
Spring Security Login Page with React
Java program to Implement Tree Set
Lớp Arrarys trong Java (Arrays Utility Class)
Convert char to String in Java
Java Program to Find the Longest Path in a DAG
Hướng dẫn Java Design Pattern – Flyweight
Java Program to Implement Find all Back Edges in a Graph
Java Program to Implement Stein GCD Algorithm
Java Program to Implement Queue
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Using Spring @ResponseStatus to Set HTTP Status Code
Constructor Injection in Spring with Lombok
Java Program to Implement IdentityHashMap API
How to Get All Spring-Managed Beans?
Quick Guide on Loading Initial Data with Spring Boot
Java Program to Represent Graph Using Adjacency Matrix
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges