import java.util.*; class Graph { private int numVertices; private Map<Integer, List<Edge>> adjList; public Graph(int numVertices) { this.numVertices = numVertices; adjList = new HashMap<>(); for (int i = 0; i < numVertices; i++) { adjList.put(i, new LinkedList<>()); } } public void addEdge(int source, int destination, int weight) { Edge edge = new Edge(destination, weight); adjList.get(source).add(edge); // For an undirected graph, you can uncomment the following line to add the reverse edge as well. // Edge reverseEdge = new Edge(source, weight); // adjList.get(destination).add(reverseEdge); } public void printGraph() { for (int i = 0; i < numVertices; i++) { List<Edge> neighbors = adjList.get(i); System.out.print("Vertex " + i + " is connected to: "); for (Edge neighbor : neighbors) { System.out.print(neighbor.destination + " (weight: " + neighbor.weight + ") "); } System.out.println(); } } private static class Edge { private int destination; private int weight; public Edge(int destination, int weight) { this.destination = destination; this.weight = weight; } } } public class Main { public static void main(String[] args) { int numVertices = 5; Graph graph = new Graph(numVertices); // Adding edges with weights graph.addEdge(0, 1, 2); graph.addEdge(0, 4, 1); graph.addEdge(1, 2, 3); graph.addEdge(1, 3, 4); graph.addEdge(1, 4, 5); graph.addEdge(2, 3, 6); graph.addEdge(3, 4, 7); // Printing the graph graph.printGraph(); } }