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:
The DAO with JPA and Spring
Base64 encoding và decoding trong Java 8
JUnit 5 @Test Annotation
LinkedList trong java
Custom Thread Pools In Java 8 Parallel Streams
Spring Cloud – Securing Services
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Practical Java Examples of the Big O Notation
Spring Boot - Google Cloud Platform
Hướng dẫn Java Design Pattern – Observer
Jackson vs Gson
Java Program to do a Depth First Search/Traversal on a graph non-recursively
Giới thiệu Design Patterns
Java Program to Implement PriorityQueue API
Object cloning trong java
Lớp Collectors trong Java 8
Allow user:password in URL
Java Program to Describe the Representation of Graph using Adjacency List
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Java Program to Implement Bloom Filter
Spring Boot: Customize the Jackson ObjectMapper
Java Program to Implement CopyOnWriteArraySet API
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Java Program to Create a Random Graph Using Random Edge Generation
Converting Between a List and a Set in Java
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Java Program to Generate Random Hexadecimal Byte
Object Type Casting in Java
Java Program to Implement Sieve Of Atkin
Java Program to Find kth Largest Element in a Sequence
Supplier trong Java 8