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 Convert ArrayList to LinkedList Given an array list, your task is to write a program to convert the given array list to Linked List in Java. Examples: Input: ArrayList: [Geeks, forGeeks, A computer Portal] Output: LinkedList: [Geeks, forGeeks, A computer Portal] Input: ArrayList: [1, 2, 3, 4, 5] Output: LinkedList: [1, 2, 3, 4, 5]
6 min read
Graph Representation using Incidence Matrix in C++ A graph is a non-linear data structure that is represented as a collection of vertices and edges that connect pairs of vertices. One way to represent a graph in computer memory is using an incidence matrix. An incidence matrix is a 2D array where rows represent edges and columns represent vertices.
5 min read
Graph Representation using Java ArrayList Prerequisite : Graph and its representations In this article, we will be discussing Adjacency List representation of Graph using ArrayList in Java. Following is adjacency list representation of the above graph. The idea is to use ArrayList of ArrayLists. Java // Java code to demonstrate Graph repres
2 min read
Graph Representation using Adjacency Matrix in C A graph is a data structure having a set of vertices and a collection of edges that each connect a pair of vertices. There are several ways to represent a graph in computer memory, and one of them is using an adjacency matrix. An adjacency matrix is a 2D array with one row per vertex and one column
4 min read
Graph Representation Learning In this article we are going to learn about Graph representation in Machine Learning (ML). Graph is basically a data structure which provide a mathematical model of representing information by the collection of nodes and edges connecting them. It is used in machine learning to solve the problem of r
8 min read