# Java : Adjacency list implementation for storing graph

#### Storing graph as an adjacency list using a list of the lists

Below is a simple example of a graph where each node has a number that uniquely identifies it and differentiates it from other nodes in the graph. Such a graph can be stored in an adjacency list where each node has a list of all the adjacent nodes that it is connected to.
An adjacency list can be implemented as a list of lists in Java.

Example : In the below adjacency list we can see
a) Node 0 has a list storing adjacent nodes 1 and 2.
b) Node 1 has a list storing adjacent nodes 0, 3 and 4. Java : Storing graph in an adjacency list using list of lists

``````import java.util.List;
import java.util.ArrayList;

class Graph {

int nodes;

Graph (int arg_nodes) {
nodes = arg_nodes;
adjlist = new ArrayList<>(nodes);
for (int i=0; i<nodes; i++)
}

void AddEdge (int src, int dst) {
}

void Iterate (int src) {
System.out.print("\n" + src + " : ");
System.out.print(adj_node + " ");
}
}

public static void main (String[] args) {

Graph g = new Graph(7);

System.out.print("Adjacency list implementation for graph");

g.Iterate (0);
g.Iterate (1);
g.Iterate (4);
}
}
``````

Output

``````Adjacency list implementation for graph
0 : 1 2
1 : 0 3 4
4 : 1 6
``````

#### Storing graph as an adjacency list using map and list in Java

Below is an example of a graph where each node has a name (string) and an id (number) that uniquely identifies it and differentiates it from other nodes in the graph. Such a graph can be stored in an adjacency list where each node has a list of all the adjacent nodes that it is connected to.
An adjacency list for such a graph can be implemented as a map of node and list of nodes in Java.

Example : In the below adjacency list we can see
a) Node ( Alfa, 1 ) has a list storing adjacent nodes ( Cod, 2 ), ( Pi, 3 ) and ( Ram , 4) Java : Storing graph in an adjacency list using a map of string and list of string.

``````import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;

class Graph {

static class Node {
String name;
Integer id;

Node(String arg_name, Integer arg_id) {
name = arg_name;
id   = arg_id;
}
void Display() {
System.out.print(" ( " + name + " , " + id + " )");
}
}

Map<Node, List<Node>> adjlist = new HashMap<Node, List<Node>>();

}

void Iterate (Node src) {
src.Display();
System.out.print(" : ");
for (Node node : adj_nodes) {
node.Display();
} System.out.print("\n");
}

public static void main (String[] args) {
Node n1 = new Node("Alfa", 1);
Node n2 = new Node("Cod", 2);
Node n3 = new Node("Pi", 3);
Node n4 = new Node("Ram", 4);
Node n5 = new Node("Yo", 5);

List<Node> n1_list = Arrays.asList(n2, n3, n4);
List<Node> n2_list = Arrays.asList(n1, n4);
List<Node> n3_list = Arrays.asList(n1, n4, n5);
List<Node> n4_list = Arrays.asList(n1, n2, n3, n5);
List<Node> n5_list = Arrays.asList(n3, n4);

System.out.println("Adjacency list implementation for graph");

Graph g = new Graph();

g.Iterate(n1);
g.Iterate(n2);
g.Iterate(n3);
g.Iterate(n4);
}
}
``````

Output

``````Adjacency list implementation for graph
( Alfa , 1 ) : ( Cod , 2 ) ( Pi , 3 ) ( Ram , 4 )
( Cod , 2 ) : ( Alfa , 1 ) ( Ram , 4 )
( Pi , 3 ) : ( Alfa , 1 ) ( Ram , 4 ) ( Yo , 5 )
( Ram , 4 ) : ( Alfa , 1 ) ( Cod , 2 ) ( Pi , 3 ) ( Yo , 5 )
``````

Copyright (c) 2019-2022, Algotree.org.