int[][] matrix = new int[3][3]; matrix[0][1] = 1; matrix[1][0] = 1; matrix[1][2] = 1; matrix[2][1] = 1; matrix[2][0] = 1; matrix[0][2] = 1; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); }
The adjacency matrix is symmetric because the graph is undirected. Edges between nodes 0-1, 1-2, and 2-0 set the corresponding entries to 1 in both directions.
int[][] matrix = new int[3][3]; matrix[0][1] = 1; matrix[1][2] = 1; matrix[2][0] = 1; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); }
Each directed edge sets matrix[from][to] = 1 only. So 0->1 sets matrix[0][1] = 1, and so on.
int[][] matrix = new int[3][3]; matrix[0][3] = 1;
Index 3 is out of bounds for an array of size 3 (valid indices are 0,1,2). This causes ArrayIndexOutOfBoundsException.
An adjacency matrix always uses memory proportional to nodes squared, while adjacency list memory depends on edges. For sparse graphs, adjacency list is more memory efficient.
Option A checks all pairs of distinct nodes and returns false if any pair is not connected (0). This correctly identifies completeness.
