We can build on top of this: Graph Representation - Adjacency List and Map. We can store source as key in map and pair of destination and weight as adjacency list. We are using int array of size 2 here, as java doesnt still have a good pair class. Code Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class Graph { private Map<Integer, List<int[]>> adjList = new LinkedHashMap<>(); private int V; private final boolean isDirected; public Graph(int n, boolean isDirected) { V = n; for (int i = 0; i < n; i++) { adjMap.put(i, new LinkedList<>()); } this.isDirected = isDirected; } public boolean addEdge(int i, int j, int w) { adjMap.putIfAbsent(i, new LinkedList<>()); adjMap.get(i).add(new int[]{j, w}); if (!isDirected) { adjMap.putIfAbsent(j, new LinkedList<>()); adjMap.get(j).add(new int[]{i, w}); } return true; } }