// AdjacencyMatrix.java class Graph { private int[][] matrix; int size; // start with no edges at all public Graph(int size) { this.size = size; matrix = new int[size][size]; for(int i = 0; i < size; i++) { for(int j = 0; j < size; j++) { matrix[i][j] = 0; } } } // insert an edge public void insertEdge(int from, int to, int cost) { matrix[from][to] = cost; } // remove an edge public void remove(int from, int to) { matrix[from][to] = 0; } // return the cost of an edge or 0 for none public int getCost(int from, int to) { return matrix[from][to]; } } public class AdjacencyMatrix { public static void main(String args[]) { // create a graph with 6 nodes Graph graph = new Graph(6); // add the edges graph.insertEdge(0, 3, 15); graph.insertEdge(0, 4, 20); graph.insertEdge(1, 5, 25); graph.insertEdge(3, 0, 15); graph.insertEdge(3, 1, 25); graph.insertEdge(4, 2, 30); graph.insertEdge(4, 5, 10); graph.insertEdge(5, 1, 25); } }