Java Program to Implement Adjacency List Last Updated : 15 May, 2024 Comments Improve Suggest changes Like Article Like Report Adjacency List is the data structure used to represent graphs which can consist of the vertices(nodes) and the edges(connections between the nodes). In the adjacency list, each vertex is associated with the list of its neighbouring vertices. This data structure is most commonly used to efficiently represent sparse graphs where the number of edges is much smaller than the number of possible edges. Implementation of Adjacency ListAdjacency List can be implemented in Java using collections like HashMap for mapping vertices to their adjacent vertices and LinkedList or ArrayList for storing the adjacent vertices. Basic OperationsAdd Vertex: This operation can add a new vertex to the graph. A vertex can represents the node in the graph and adding the vertex means adding the new entity into the graph without any connections to the other vertices.Add Edge: This operation can add a new edge between the two vertices in the graph. An Edge can represent the connection between the two vertices. Adding the edge establishes the link between the two vertices and it can be allowing the traversal between them.Remove Vertex: This operation can remove the vertex and all its incident edges from the graph. Removing the vertex eliminates it from the graph.Remove Edge: This operation can remove the edge between the two vertices in the graph. Removing the edge is the connection between the two vertices without affecting the vertices.Find Neighbors: This operation can retrieve the list of the neighbours (adjacent vertices) of the given vertex. Neighbours are vertices directly connected to a particular vertex via an edge.Print Graph: This operation can be used to prints the entire graph with its vertices and their corresponding adjacency lists. Printing the graph allows the visualizing the structure of the graph.Program to Implement Adjacency List in JavaBelow is the implementation of the Adjacency List in Java: Java // Java Program to Implement Adjacency List import java.util.*; // Class representing a graph using adjacency list // representation class Graph { private Map<Integer, List<Integer> > adjacencyList; // Constructor to initialize the adjacency list public Graph() { adjacencyList = new HashMap<>(); } // Method to add a new vertex to the graph public void addVertex(int vertex) { adjacencyList.put(vertex, new ArrayList<>()); } // Method to add an edge between two vertices public void addEdge(int source, int destination) { adjacencyList.get(source).add(destination); } // Method to remove a vertex from the graph public void removeVertex(int vertex) { adjacencyList.remove(vertex); // Remove the vertex from the neighbors of other // vertices for (List<Integer> neighbors : adjacencyList.values()) { neighbors.remove(Integer.valueOf(vertex)); } } // Method to remove an edge between two vertices public void removeEdge(int source, int destination) { adjacencyList.get(source).remove( Integer.valueOf(destination)); // For undirected graph, uncomment below line // adjacencyList.get(destination).remove(Integer.valueOf(source)); } // Method to get the neighbors of a vertex public List<Integer> getNeighbors(int vertex) { return adjacencyList.get(vertex); } // Method to print the graph public void printGraph() { for (Map.Entry<Integer, List<Integer> > entry : adjacencyList.entrySet()) { System.out.print(entry.getKey() + " -> "); for (Integer neighbor : entry.getValue()) { System.out.print(neighbor + " "); } System.out.println(); } } } public class MainAdjacency { public static void main(String[] args) { // Create a graph Graph graph = new Graph(); // Add vertices graph.addVertex(0); graph.addVertex(1); graph.addVertex(2); // Add edges graph.addEdge(0, 1); graph.addEdge(0, 2); graph.addEdge(1, 2); // Print the graph System.out.println("Graph:"); graph.printGraph(); // Remove an edge and print the graph graph.removeEdge(0, 1); System.out.println("After removing edge (0, 1):"); graph.printGraph(); // Remove a vertex and print the graph graph.removeVertex(2); System.out.println("After removing vertex 2:"); graph.printGraph(); } } Output0 -> 1 2 1 -> 2 2 -> After removing edge (0, 1): 0 -> 2 1 -> 2 2 -> After removing vertex 2: 0 -> 1 -> Complexity of the above Method:Time Complexity: Adding a vertex: O(1)Adding an edge: O(1)Removing a vertex: O(V + E)Removing an edge: O(V)Finding neighbors: O(1)Printing the graph: O(V + E)Space Complexity: Space complexity depends in the number of vertices(V) and edges(E) in the graph.Space complexity for storing the adjacency list is O(V+E)Overall, Space complexity is also O(V+E)Applications of Adjacency ListAdjacency List can be used to representing the social networks where the nodes are users and edges are friendships.It can used in modeling computer network where the nodes are routers and edges are connections.It can used to finding the shortest path using algorithms like Breadth First Search(BFH) or Dijkstra. Comment More infoAdvertise with us Next Article Java Program to Implement Adjacency List K kadambalamatclo Follow Improve Article Tags : Java Java-DSA Practice Tags : Java Similar Reads Adjacency List in Python Given the adjacency list and the number of vertices and edges of a graph, the task is to represent the adjacency list for a directed graph. Examples: Input:V = 3edges[][] = {{0, 1}, {1, 2}, {2, 0}}Output:0 -> 11 -> 22 -> 0Explanation: The output represents the adjacency list for the given g 3 min read Adjacency List Representation An adjacency list is a data structure used to represent a graph where each node in the graph stores a list of its neighboring vertices.Table of Content1. Adjacency List for Directed graph:2. Adjacency List for Undirected graph:3. Adjacency List for Directed and Weighted graph:4. Adjacency List for U 14 min read Graph Adjacency Matrix in Java A graph is a type of data structure used to represent the relationship between the entities. In this article, we will learn to represent a graph in the form of Adjacency Matrix. Graph Adjacency MatrixThe Adjacency matrix is the way to represent the graphs using the 2D array. It is the fundamental da 6 min read Implementing a Binary Tree in Java A binary tree is a hierarchical data structure composed of the nodes. Each node contains the value and references to its left child node and right child node, which are also binary trees that are possibly null. The structure resembles the tree with the nodes branching out from a central root, where 5 min read Print adjacency list of a Bidirectional Graph Given the adjacency list of a bidirectional graph. The task is to copy/clone the adjacency list for each vertex and return a new list for a bidirectional graph. An Adjacency List is used for representing graphs. Here, for every vertex in the graph, we have a list of all the other vertices to which t 7 min read Like