Java Program to Represent Graphs Using Linked List
Last Updated :
12 Apr, 2023
Data structures are divided into two categories Linear data structures and Non-Linear data structures. The major disadvantage of the linear data structure is we cannot arrange the data of linear data structure in hierarchy manner that's why in the computer field we use Non-Linear data structures. The most commonly used Non-Linear data structure is Graphs and Trees both data structures can be implemented using Linear data structures. In this article, we will discuss how to represent Graphs using Linked List.
Graphs consist of a finite set of vertices(or nodes) and a set of edges that connect a pair of nodes. Graphs are represented in two different ways. One method is using adjacency List representation and the second is adjacency matrix representation. Adjacency List representation is mostly used because of dynamic memory allocation using list representation.
Graphs are of two types:
- Directed Graphs: In this arrangement of graphs, every node is directed to one vertex is called directed Graphs.
- Undirected Graphs: In this arrangement of graphs, two nodes are connected using bi-direction vertex is called undirected graphs.
Undirected Graphs representation: Maximum number of a vertex in the undirected graphs is n*(n-1) where n is the total number of nodes present in the Undirected Graphs.

LinkedList Representation of undirected Graphs is as follows:
In undirected graphs, two nodes are connected in bi-direction vertex. We can use both Array List and Linked List collections to represent the undirected graphs. In Linked List the Manipulation of the data is faster than the Array List because Array List internally used the dynamic array to store the data whereas Linked List used Doubly Linked List that is faster in the operation of manipulation but not in accessing the elements.

Implementation: Here we will be discussing out both types of graphs in order to implement the same.
Example 1
Java
// Java Program to Implement the Unidirectional Graph
// Using Linked List
// Importing required classes from packages
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Method 1
// To make pair of nodes
static void
addEdge(LinkedList<LinkedList<Integer> > Adj, int u,
int v)
{
// Creating bi-directional vertex
Adj.get(u).add(v);
Adj.get(v).add(u);
}
// Method 2
// To print the adjacency list
static void
printadjacencylist(LinkedList<LinkedList<Integer> > adj)
{
for (int i = 0; i < adj.size(); ++i) {
// Printing the head
System.out.print(i + "->");
for (int v : adj.get(i)) {
// Printing the nodes
System.out.print(v + " ");
}
// Now a new line is needed
System.out.println();
}
}
// Method 3
// Main driver method
public static void main(String[] args)
{
// Creating vertex
int V = 5;
LinkedList<LinkedList<Integer> > adj
= new LinkedList<LinkedList<Integer> >();
for (int i = 0; i < V; ++i) {
adj.add(new LinkedList<Integer>());
}
// Inserting nodes
// Custom input node elements
addEdge(adj, 0, 1);
addEdge(adj, 0, 4);
addEdge(adj, 1, 2);
addEdge(adj, 1, 3);
addEdge(adj, 1, 4);
addEdge(adj, 2, 3);
addEdge(adj, 3, 4);
// Printing adjacency list
printadjacencylist(adj);
}
}
Output0->1 4
1->0 2 3 4
2->1 3
3->1 2 4
4->0 1 3
Now jumping onto the next type of graphs that is our directed graphs representation using Linked List. In directed graphs, two nodes are connected in uni-direction vertex.

Time Complexity: O(e)
Here e is number of edges
Auxiliary Space: O(e)
The extra space is used to store the nodes of the linkedlist.
Example 2
Java
// Java Program to Implement the Directed Graph
// Using Linked List
// Importing standard classes from respectively packages
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Method 1
// To make pair of nodes
static void
addEdge(LinkedList<LinkedList<Integer> > Adj, int u,
int v)
{
// Creating unidirectional vertex
Adj.get(u).add(v);
}
// Method 2
// To print the adjacency List
static void
printadjacencylist(LinkedList<LinkedList<Integer> > adj)
{
for (int i = 0; i < adj.size(); ++i) {
// Printing the head
if (adj.get(i).size() != 0) {
System.out.print(i + "->");
for (int v : adj.get(i)) {
// Printing the nodes
System.out.print(v + " ");
}
// A new line is needed
System.out.println();
}
}
}
// Method 3
// Main driver method
public static void main(String[] args)
{
// Creating vertex
int V = 5;
LinkedList<LinkedList<Integer> > adj
= new LinkedList<LinkedList<Integer> >();
for (int i = 0; i < V; ++i) {
adj.add(new LinkedList<Integer>());
}
// Inserting nodes
// Custom input elements
addEdge(adj, 0, 1);
addEdge(adj, 0, 4);
addEdge(adj, 1, 2);
// Printing adjacency List
printadjacencylist(adj);
}
}
Time Complexity: O(e)
Here e is number of edges
Auxiliary Space: O(e)
The extra space is used to store the nodes of the linkedlist.
Similar Reads
Java Program to Implement Unrolled Linked List An Unrolled Linked List is a special type of Linked List in which each node stores an array of elements, unlike a simple linked list. Here we use an ArrayList and a constructor that initializes the size of the Unrolled Linked List. Elements are added to the first Node until it is filled and then a n
5 min read
Java Program to Implement Triply Linked List Unlike arrays, linked list elements are not stored at a contiguous location; the elements are linked using pointers. In this post, methods to insert a new node in a linked list are discussed. A node can be inserted in three ways either at the front of the linked list or after a given node or at the
8 min read
How to Represent Graph Using Incidence Matrix in Java? An incidence matrix is simply a matrix that contains information about the relationship between objects (or instances) of any two classes. The rows of the matrix represent one class of objects while the columns represent the other class. Any coordinate (x, y) in the matrix represents the relationshi
3 min read
Java Program to Get Elements of a LinkedList Linked List is a linear data structure, in which the elements are not stored at the contiguous memory locations. Here, the task is to get the elements of a LinkedList. 1. We can use get(int variable) method to access an element from a specific index of LinkedList: In the given example, we have used
4 min read
How to Sort a LinkedList in Java? A Linked List is a linear data structure, in which the elements are not stored at contiguous memory locations.Sorting the nodes of a Singly Linked list in ascending order:Original ListSorted ListWe can sort the LinkedList by many sorting techniques:Selection SortInsertion sortQuick sortMerge sort Me
13 min read
Iterate a LinkedList in Reverse Order in Java For traversing a linked list in reverse order we can use Descending Iterator or List Iterator 1. Descending Iterator Syntax: LinkedList<String> linkedlist = new LinkedList<>(); Iterator<String> listIterator = linkedlist.descendingIterator(); Returns: Descending Iterator returns the
2 min read
Java Program to Perform Bandwidth Reduction on Graphs Bandwidth reduction on graphs refers to techniques used to reduce the amount of data that needs to be transmitted or stored in order to represent a graph. Graphs are often used to represent data in a wide range of applications, including social networks, transportation networks, and biological netwo
3 min read
How to Implement Stack in Java using LinkedList and Generics? Prerequisites: Generics in JavaLinkedList in JavaWhat is Stack? Stack is a linear Data Structure that follows LIFO(Last In First Out) order while performing its operations. The main operations that are performed on the stack are mentioned below: push() : Â inserts an element at beginning of the stack
6 min read
Java Program to Create a Singly Linked List and Count the Number of Nodes Linked List is a linear data structure. Linked list elements are not stored at a contiguous location, the elements are linked using pointers. Singly Linked list is the collection of nodes, where each node has two parts one is the data and other is the linked part. Example: Input : AddNodes = {2, 3,
3 min read
Java Program For Inserting A Node In A Linked List We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of the linked list. Java // Linked List Class class LinkedList { // Head of list Node
7 min read