This Java program,represents a given graph in the form of Adjacency list.
Here is the source code of the Java program to display a linked list in reverse. 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | import java.util.HashMap; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Scanner; public class GraphAdjacencyList { /* Makes use of Map collection to store the adjacency list for each vertex.*/ private Map<Integer, List<Integer>> Adjacency_List; /* * Initializes the map to with size equal to number of vertices in a graph * Maps each vertex to a given List Object */ public GraphAdjacencyList( int number_of_vertices) { Adjacency_List = new HashMap<Integer, List<Integer>>(); for ( int i = 1 ; i <= number_of_vertices ; i++) { Adjacency_List.put(i, new LinkedList<Integer>()); } } /* Adds nodes in the Adjacency list for the corresponding vertex */ public void setEdge( int source , int destination) { if (source > Adjacency_List.size() || destination > Adjacency_List.size()) { System.out.println( "the vertex entered in not present " ); return ; } List<Integer> slist = Adjacency_List.get(source); slist.add(destination); List<Integer> dlist = Adjacency_List.get(destination); dlist.add(source); } /* Returns the List containing the vertex joining the source vertex */ public List<Integer> getEdge( int source) { if (source > Adjacency_List.size()) { System.out.println( "the vertex entered is not present" ); return null ; } return Adjacency_List.get(source); } /* * Main Function reads the number of vertices and edges in a graph. * then creates a Adjacency List for the graph and prints it */ public static void main(String...arg) { int source , destination; int number_of_edges,number_of_vertices; int count = 1 ; Scanner scan = new Scanner(System.in); try { /* Read the number of vertices and edges in graph */ System.out.println( "Enter the number of vertices and edges in graph" ); number_of_vertices = scan.nextInt(); number_of_edges = scan.nextInt(); GraphAdjacencyList adjacencyList = new GraphAdjacencyList(number_of_vertices); /* Reads the edges present in the graph */ System.out.println( "Enter the edges in graph Format : <source index> <destination index>" ); while (count <= number_of_edges) { source = scan.nextInt(); destination = scan.nextInt(); adjacencyList.setEdge(source, destination); count++; } /* Prints the adjacency List representing the graph.*/ System.out.println ( "the given Adjacency List for the graph \n" ); for ( int i = 1 ; i <= number_of_vertices ; i++) { System.out.print(i+ "->" ); List<Integer> edgeList = adjacencyList.getEdge(i); for ( int j = 1 ; ; j++ ) { if (j != edgeList.size()) { System.out.print(edgeList.get(j - 1 )+ "->" ); } else { System.out.print(edgeList.get(j - 1 )); break ; } } System.out.println(); } } catch (InputMismatchException inputMismatch) { System.out.println( "Error in Input Format. \nFormat : <source index> <destination index>" ); } scan.close(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $javac GraphAdjacencyList.java $java GraphAdjacencyList Enter the number of vertices and edges in graph 4 5 Enter the edges in graph Format : <source index> <destination index> 1 2 2 3 3 4 4 1 1 3 the given Adjacency List for the graph 1 -> 2 -> 4 -> 3 2 -> 1 -> 3 3 -> 2 -> 4 -> 1 4 -> 3 -> 1 |
Related posts:
Using the Not Operator in If Conditions in Java
Spring @Primary Annotation
Java Program to Implement Strassen Algorithm
Từ khóa static và final trong java
Merging Streams in Java
Using a List of Values in a JdbcTemplate IN Clause
A Guide to the Java LinkedList
A Guide to the ResourceBundle
A Custom Data Binder in Spring MVC
Java InputStream to String
Copy a List to Another List in Java
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
Java Program to Implement Hash Tables Chaining with List Heads
Spring Data JPA and Null Parameters
How to use the Spring FactoryBean?
Java Program to Describe the Representation of Graph using Adjacency Matrix
Disable Spring Data Auto Configuration
Java Program to Implement Sparse Matrix
Spring 5 Testing with @EnabledIf Annotation
Introduction to Java 8 Streams
Getting Started with GraphQL and Spring Boot
Flattening Nested Collections in Java
Hướng dẫn Java Design Pattern – Null Object
Java Program to Find Basis and Dimension of a Matrix
Java Program to implement Sparse Vector
“Stream has already been operated upon or closed” Exception in Java
Hướng dẫn Java Design Pattern – Mediator
Create a Custom Exception in Java
Configure a RestTemplate with RestTemplateBuilder
Converting between an Array and a List in Java
Java IO vs NIO
Giới thiệu Json Web Token (JWT)