00:00

Graph Adjacency Matrix

Last updated:

Before a program can work with a graph, the graph has to be stored in memory. The two standard ways are the adjacency matrix and the adjacency list. This tutorial explains the adjacency matrix, shows how to build one in Java, and discusses when it is the right choice.

What is an Adjacency Matrix in a Graph?

An adjacency matrix stores a graph with V vertices in a V × V two-dimensional array. Row i and column j both stand for vertices, and the cell adjMatrix[i][j] records whether there is an edge from vertex i to vertex j: 1 if there is (or the edge's weight in a weighted graph) and 0 if there is not.

To build it for a graph with n vertices, create an n × n array filled with 0, then for every edge from i to j:

  • set adjMatrix[i][j] to 1 (or to the weight);
  • for an undirected graph, also set adjMatrix[j][i], so the matrix is symmetric.

Adjacency Matrix Representation of Undirected Graph

The undirected graph below has 4 vertices (A, B, C, D) and 4 edges: (A-B), (A-C), (B-D) and (C-D).

In its matrix, each row and each column belongs to one vertex. The cell where row A meets column B is 1 because A and B are connected; the cell for A and D is 0 because there is no edge between them. Since the graph is undirected, the matrix is symmetric about its main diagonal, and the diagonal itself is 0 because there are no self-loops.

Undirected graph A-B, A-C, B-D, C-D and its 4 by 4 adjacency matrix

Adjacency Matrix Example

The program below builds the matrix for the graph shown above, using the indexes 0 to 3 for A to D.

AdjacencyMatrix.java
// A Java program to implement adjacency matrix of undirected graph
public class AdjacencyMatrix {
  // Method to add edge
  void addEdge(int[][] matrix, int i, int j) {
    matrix[i][j] = 1;
    matrix[j][i] = 1;
  }

  // Method to print matrix
  void printMatrix(int[][] matrix) {
    for (int[] row : matrix) {
      for (int value : row) {
        System.out.print(value + " ");
      }
      System.out.println(); // Line break
    }
  }

  public static void main(String[] args) {
    AdjacencyMatrix adjMatrix = new AdjacencyMatrix();
        int V = 4;   // total vertices are 4
        int[][] matrix = new int[V][V];   // all values are initialized as 0

    // Adding edges one by one into the matrix
    // Vertices 0, 1, 2, 3 stand for A, B, C, D in the diagram above
    adjMatrix.addEdge(matrix, 0, 1);   // A - B
    adjMatrix.addEdge(matrix, 0, 2);   // A - C
    adjMatrix.addEdge(matrix, 1, 3);   // B - D
    adjMatrix.addEdge(matrix, 2, 3);   // C - D

    System.out.println("Adjacency Matrix Elements:");
    adjMatrix.printMatrix(matrix);
  }
}
Output
Adjacency Matrix Elements:
0 1 1 0
1 0 0 1
1 0 0 1
0 1 1 0

Advantages of Adjacency Matrix

  • O(1) edge lookup: checking whether u and v are connected, or adding or removing an edge, is a single array access.
  • Simple to implement: just a 2D array, with no lists or objects per vertex.
  • Good for dense graphs: when most pairs of vertices are connected, an adjacency list would need about as much memory and more overhead.
  • Fits matrix-based algorithms: Floyd-Warshall all-pairs shortest paths and transitive closure work directly on the matrix.
  • Handles all edge types: directed graphs (non-symmetric matrix) and weighted graphs (store the weight instead of 1) use the same structure.

Disadvantages of Adjacency Matrix

  • O(V2) memory regardless of the number of edges, which is wasteful for sparse graphs.
  • Slow neighbour iteration: finding all neighbours of a vertex means scanning a whole row, O(V), so BFS and DFS take O(V2) instead of O(V + E).
  • Costly to add or remove vertices: the whole matrix has to be resized and copied, O(V2).
  • Parallel edges cannot be stored with a single 0/1 cell.

Applications of Graph Adjacency Matrix

Adjacency matrices are a good fit when graphs are small or dense, or when the algorithm itself is matrix-based:

  • All-pairs shortest paths and reachability (Floyd-Warshall, transitive closure).
  • Dense networks such as fully meshed communication or circuit connection tables.
  • Graph analysis that uses linear algebra on the matrix (for example counting walks with matrix powers, or spectral methods).
  • Small graphs in games and puzzles, where constant-time edge checks matter more than memory.