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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | 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(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | $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:
Why String is Immutable in Java?
Remove the First Element from a List
Java Program to Implement Efficient O(log n) Fibonacci generator
A Guide to Concurrent Queues in Java
How to Read a File in Java
Java Program to Perform String Matching Using String Library
Convert String to int or Integer in Java
New Features in Java 15
How to Iterate Over a Stream With Indices
RestTemplate Post Request with JSON
Refactoring Design Pattern với tính năng mới trong Java 8
Lớp Properties trong java
An Intro to Spring Cloud Contract
Prevent Brute Force Authentication Attempts with Spring Security
How To Serialize and Deserialize Enums with Jackson
Spring WebClient vs. RestTemplate
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Transactions with Spring and JPA
Java Program to Find Strongly Connected Components in Graphs
How to Get All Dates Between Two Dates?
Model, ModelMap, and ModelAndView in Spring MVC
Spring – Injecting Collections
Java Program to Implement SynchronosQueue API
Spring Boot - File Handling
Java Program to Implement Counting Sort
Count Occurrences of a Char in a String
Hướng dẫn Java Design Pattern – Decorator
Java Program to Implement LinkedBlockingQueue API
How to Replace Many if Statements in Java
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
An Intro to Spring Cloud Task
Call Methods at Runtime Using Java Reflection