Hello people..! This is a special extension for my discussion on Graph Theory Basics. Here, I give you the code for implementing the Adjacency List in Java. Some of the features of this code are –

• The Adjacency List is an array of LinkedLists, where each element is a Pair<Integer, Integer>, from javafx.util.Pair. This pair stores two values, the destination vertex, (V2 in an edge V1 → V2) and the weight of the edge.
• addEdge() – Appends an edge startVertexendVertex with a certain weight. This is an O(1) Insertion.
• getNumberOfVertices() – Returns the number of vertices in the graph. This value does not change once the graph is created.
• getNumberOfEdgesFromVertex() – Returns the number of edges coming out from a vertex. Basically, it returns the length of the linked list associated with the vertex.
• getEdgesFromVertex() – Returns a LinkedList object of edges that come out of this vertex. Changes made to the object returned does not affect the encapsulated adjacency list.
• removeEdge() – Removes an edge coming out of startVertex. It expects an object of Pair<Integer, Integer> which corresponds to the edge to be removed. Returns a boolean value regarding whether there was any change made to the Collection.
```/*
* using Collections API
*
* Authored by,
* Vamsi Sangam.
*/

import java.util.Scanner;
import javafx.util.Pair;

// Constructor

for (int i = 0; i < adjacencyList.length; ++i) {
}
}

// Appends a new Edge to the linked list
public void addEdge(int startVertex, int endVertex, int weight) {
}

// Returns number of vertices
// Does not change for an object
public int getNumberOfVertices() {
}

// Returns number of outward edges from a vertex
public int getNumberOfEdgesFromVertex(int startVertex) {
}

// Returns a copy of the Linked List of outward edges from a vertex
public LinkedList< Pair<Integer, Integer> > getEdgesFromVertex(int startVertex) {

return edgeList;
}

int i = 0;

System.out.print("adjacencyList[" + i + "] -> ");

for (Pair<Integer, Integer> edge : list) {
System.out.print(edge.getKey() + "(" + edge.getValue() + ")");
}

++i;
System.out.println();
}
}

// Removes an edge and returns true if there
// was any change in the collection, else false
public boolean removeEdge(int startVertex, Pair<Integer, Integer> edge) {
}
}

class testGraph
{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);

int vertices = s.nextInt();
int edges = s.nextInt();
int u, v, weight;

int i = 0;

while (i < edges) {
u = s.nextInt() - 1;
v = s.nextInt() - 1;
weight= s.nextInt();

++i;
}

}
}
```

Feel free to comment if you have any doubts..! Keep practising..! Happy Coding..! 😀

## 8 thoughts on “Adjacency List in Java”

1. Umesh

hey could u plz provide pictorial view ?

• It would look exactly like the Adjacency List here… http://theoryofprogramming.com/2014/12/24/graph-theory-basics/ …. There we used an array of pointers, here we are using an array of LinkedList references… Which are pretty much the same… Try to use it as a reference, I’ll update the post with a more accurate picture in a few days 🙂

2. Akash Agnihotri

I have another Implementation of the same idea, please take a look and let me know what do u think about it..
import java.util.*;
class Graph
{
int num;
Graph(int n)
{
num=n;
for(int i=0;i<num;i++)
}
{
lhm[a-1].put(new Integer(b),new Integer(wg));
lhm[b-1].put(new Integer(a),new Integer(wg));
}
void printMap()
{
for(int i=0;i<num;i++)
System.out.println(i+1+" connected to "+lhm[i]);
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of nodes in the graph");
int n=in.nextInt();
Graph obj=new Graph(n);
System.out.println("Enter number of edges");
int k=in.nextInt();
System.out.println("Enter the edges eg: node a node b edge weight");
for(int i=0;i<k;i++)
{
int a=in.nextInt();
int b=in.nextInt();
int wg=in.nextInt();
}
obj.printMap();
}
}

3. Aayush Pandey

how to check if the edge already exists???
i tried
public boolean hasNode(int src,int dest)
{
}

• Your code is perfect… Did you get the indexing right? In my code, the input is 1-indexed but the actual data structure is 0-indexed… Maybe you must use adjacency[src – 1].contains(dest – 1) in your code… Take a look and if there’s any further issue, comment with an Ideone link to your code 🙂

4. Icarus_XXII

Thanks Vamsi!

5. Aman

How would it return edges from given vertex ?
public LinkedList< Pair > getEdgesFromVertex(int startVertex) {