Print Adjacency List for a Directed Graph in Java28 Mar 2025 | 4 min read An adjacency list is a fundamental representation of a graph in graph theory. Every vertex in a directed graph keeps track of the other vertices to which it has outgoing edges. Particularly for sparse graphs, this form is economical in terms of both space and temporal complexity. This article explains how to create and display a directed graph's adjacency list in Java. Adjacency List RepresentationAn adjacency list for a directed graph is a collection of lists or arrays. Each index in this collection represents a vertex, and each list at that index contains the vertices that can be reached directly from the vertex. For instance, if vertex 'v' has edges to vertices 'w' and 'x', the adjacency list for 'v' will include 'w' and 'x'. Steps to Implement Adjacency List for a Directed Graph1. Choosing Data Structures We will use a HashMap to store the adjacency list where:
To represent the list of edges from a vertex, we will use an ArrayList, which will allow efficient addition of edges. 2. Class Design We will define a class Graph to represent the directed graph. The class will contain methods to:
3. Adding Vertices and Edges When adding a vertex, we will initialize an empty list of edges in the adjacency list. When adding an edge, we will append the destination vertex to the list associated with the source vertex. 4. Printing the Adjacency List The adjacency list will be printed by iterating through the map and displaying each vertex followed by its list of connected vertices. Let's implement the above steps in a Java program. File Name: Graph.java Output: Vertex 1: 2 3 Vertex 2: 3 Vertex 3: 4 Vertex 4: 1 ExplanationAn adjacency list is a simple way to represent a directed graph where every element then contains a list of elements connected to it by directed edges. To represent the above mentioned Java graph data structures, we can use a HashMap where the keys of the map hold each vertex of the graph and the value of the map hold at each key, a list or an array of the vertices which the key vertex points to. To this end, we design a Graph class that enables the addition of vertices, the addition of directed edges, and the printing of the adjacency list. The adjacency list is stored using hashmap, wherein the actual key is a value of an ArrayList of neighbors and this is efficient for storing sparse graph. It further involves creating available vertices along with edges after that traversing the map to print the adjacency list. Vertex insertion, edge insertion, and edge deletion alike operate in constant time, that is, O(1) while list printing takes O(V + E), where V is the number of vertices and E is the number of edges. Complexity AnalysisTime ComplexityTo add a vertex the time complexity is O(1) because it only involves insertion in the HashMap. To add an edge take O(1) on average adding an element to an ArrayList is done in constant time. Printing takes O(V + E) time to print list. Whenever vertices are being printed the process will take V while printing edges takes E since we will go through all edges. Space ComplexityThe space complexity is O(V + E ) since we are storing each vertex and the list of the edges which are connected with it. ConclusionAn adjacency list is good for representing a graph, particularly sparse, as an example above has illustrated. In this article, we have learn to construct a directed graph using an adjacency list in java language. We told you how to add vertices and edges and how to print the adjacency list. This structure is useful in any nodes traversal based algorithms for graphs such as depth first search (DFS) or the bread first search (BFS). Next TopicCharacter Array in Java |
Java has long been a cornerstone of enterprise software development, known for its platform independence, robust ecosystem, and strong community support. Java is still adjusting and changing as we go more into the era of micro services and cloud computing, especially with the introduction of containerization...
8 min read
? In Java, in order to create a file with a given charset, we must supply the character encoding when writing text to a file. Classes to produce Java files with a certain character set: The OutputStreamWriter class and a FileOutputStream allow us to create Java files...
3 min read
In Java, creating test cases for exceptions is not a difficult task. The JUnit testing tool of Java provides a way to track the exception handling of code. We can write the test cases to check whether the code throws a desired exception or not. In order...
4 min read
Java multicasting, also known as typecasting multiple times, refers to the process of applying several typecasting operations sequentially on a variable. This often occurs in scenarios where data types are incompatible but need to be transformed to make the code functional. Multicasting is especially useful in object-oriented...
4 min read
C Language C is a middle-level, compiled and general-purpose programming language that follows a top-down approach for developing applications. It was developed at Bell Labs by Dennis Ritchie in 1970 for the Unix Operating System 1970. It is ideal for developing firmware and portable applications. Example #include ...
4 min read
In Java, the Object class is the parent class of all the Java classes. Every Java class is a direct or indirect child of the Java Object class. Hence, every Java class extends the Object class. Therefore, we need not to write the following statement...
3 min read
In Java, double is a datatype. It is used for storing decimal numbers with high precision. It is a 64-bit IEEE 754 floating-point data type, meaning it can handle large values and fractions accurately. We often see it in scientific calculations, financial applications, and physics...
3 min read
The java 'instanceof' operator is used to test whether an object is an instance of a specified type (class or sub - class or visual interface). 'instanceof' in Java is also known as the type comparison operator because it compares the instances with type. It returns true...
6 min read
Batting average is a crucial statistic in the game of cricket that represents a player's performance at the crease. It measures the player's ability to score runs consistently and is one of the most widely used metrics for evaluating a batter's proficiency. In this article, we...
4 min read
Developers may now write and execute Java code directly in their web browsers thanks to the growing popularity of online Java compilers. With the help of these compilers, users may test their code easily without having to set up an Integrated Development Environment (IDE) on their...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India