Graph Representation using Java ArrayList

16 Apr, 2019
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 code to demonstrate Graph representation// using ArrayList in Javaimport java.util.*;  class Test {      static void addEdge(ArrayList<ArrayList<Integer> > adj,                        int u, int v)    {        adj.get(u).add(v);        adj.get(v).add(u);    }      static void printAdjacencyList(ArrayList<ArrayList<Integer> > adj)    {        for (int i = 0; i < adj.size(); i++) {            System.out.println("Adjacency list of " + i);            for (int j = 0; j < adj.get(i).size(); j++) {                System.out.print(adj.get(i).get(j) + " ");            }            System.out.println();        }    }      public static void main(String[] args)    {        // Creating a graph with 5 vertices        int V = 5;        ArrayList<ArrayList<Integer> > adj = new ArrayList<ArrayList<Integer> >(V);        for (int i = 0; i < V; i++)            adj.add(new ArrayList<Integer>());          // Adding edges one by one.        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);        printAdjacencyList(adj);    }}
Adjacency list of 0
1 4 
Adjacency list of 1
0 2 3 4 
Adjacency list of 2
1 3 
Adjacency list of 3
1 2 4 
Adjacency list of 4
0 1 3

Similar Implementation using a separate Class for Graph.
// Java code to demonstrate Graph representation// using ArrayList in Javaimport java.util.*;  class Graph {    ArrayList<ArrayList<Integer> > adj;    int V;    Graph(int v)    {        V = v;        adj = new ArrayList<ArrayList<Integer> >(V);        for (int i = 0; i < V; i++)            adj.add(new ArrayList<Integer>());    }      void addEdge(int u, int v)    {        adj.get(u).add(v);        adj.get(v).add(u);    }      void printAdjacencyList()    {        for (int i = 0; i < adj.size(); i++) {            System.out.println("Adjacency list of " + i);            for (int j = 0; j < adj.get(i).size(); j++) {                System.out.print(adj.get(i).get(j) + " ");            }            System.out.println();        }    }}  class Test {      public static void main(String[] args)    {        // Creating a graph with 5 vertices        int V = 5;          Graph g = new Graph(V);          // Adding edges one by one.        g.addEdge(0, 1);        g.addEdge(0, 4);        g.addEdge(1, 2);        g.addEdge(1, 3);        g.addEdge(1, 4);        g.addEdge(2, 3);        g.addEdge(3, 4);        g.printAdjacencyList();    }}
Adjacency list of 0
1 4 
Adjacency list of 1
0 2 3 4 
Adjacency list of 2
1 3 
Adjacency list of 3
1 2 4 
Adjacency list of 4
0 1 3

Java-ArrayList
Graph
Java Programs
Graph
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Find if there is a path between two vertices in a directed graph
Introduction to Data Structures
What is Data Structure: Types, Classifications and Applications
Find if there is a path between two vertices in an undirected graph
Minimum steps to reach target by a Knight | Set 1
Initializing a List in Java
Java Programming Examples
Convert a String to Character Array in Java
Convert Double to Integer in Java
Implementing a Linked List in Java using Class