TutorChase logo
Login
AQA A-Level Computer Science

11.4.4 Adjacency matrix representation

Graphs can be represented in various ways, and the adjacency matrix is one of the most structured and visual approaches. It uses a two-dimensional array to model connections between nodes in a graph, offering fast edge access and suitability for dense graphs.

What is an adjacency matrix?

An adjacency matrix is a mathematical representation of a graph using a 2D array (matrix) where:

  • Rows and columns correspond to the vertices (or nodes) of the graph.

  • Each element at position (i, j) in the matrix indicates whether there is an edge from vertex i to vertex j.

    • In unweighted graphs, the matrix element is typically 1 (edge exists) or 0 (no edge).

    • In weighted graphs, the value stored at position (i, j) is the weight of the edge.

  • The matrix is always of size n × n, where n is the number of vertices in the graph.

Key idea

The position of values in the matrix indicates the direction and presence of edges. If the graph is undirected, the connection goes both ways, and the matrix is symmetric. If it’s directed, the edge (i, j) may exist without the edge (j, i).

Representing different types of graphs

Directed graphs

Take your grades to the next level!

UPGRADING TO PREMIUM UNLOCKS
AI Tutor
AI-powered study assistant
instant feedback and guidance
Predicted Papers
Examiner-style predicted papers
based on recent exam trends
Practice Questions
All exam practice questions
by topic for each subject
Study Notes
All detailed revision notes
written by expert teachers
Cheat Sheets
Quick revision summaries
perfect for last-minute review
Past Papers
Complete collection
of practice and past exam papers
Email
Password
Confirm Password
Already have an account?

Practice Questions

FAQ

The adjacency matrix is always a square matrix because its dimensions are determined by the number of vertices in the graph, not the number of edges. Each row and each column represent a single vertex, so a graph with n vertices will always have an n × n matrix. This allows the matrix to capture every possible connection between all pairs of vertices, whether or not an edge exists. Even if a graph is sparse and only has a few edges, the structure of the matrix must still reserve space for every possible connection to maintain consistency. This square layout ensures that any edge from vertex i to vertex j can be directly represented by the entry at matrix[i][j]. The total number of edges has no effect on the shape of the matrix—it only affects how many of the entries are non-zero or meaningful. This structure is crucial for fast edge lookups and uniform access across all possible vertex pairs.

The standard adjacency matrix is not well-suited for representing multigraphs—graphs that allow multiple edges between the same pair of vertices. In a typical adjacency matrix, each cell matrix[i][j] can only store a single value, either indicating the presence of a single edge (in unweighted graphs) or the weight of an edge (in weighted graphs). This means that multiple edges between the same two vertices cannot be represented directly. One workaround is to store the number of edges instead of a binary value or to store a list of weights at each matrix cell, but this breaks the basic array structure and requires using a more complex data structure, such as an array of lists or matrices of sets. Another method is to use a 3D matrix, where each (i, j) position contains multiple layers or slots to represent parallel edges, but this significantly increases memory complexity. Because of this, adjacency matrices are typically used only for simple graphs, where parallel edges and loops are either disallowed or rare.

Yes, an adjacency matrix can be used to efficiently detect isolated vertices, which are vertices with no incoming or outgoing edges. In the matrix, each vertex corresponds to a row and a column. To identify if a vertex is isolated, you simply check whether its entire row and column consist only of zeroes (or the chosen representation for no edge, such as None or infinity). A row of zeroes indicates that the vertex has no outgoing edges, while a column of zeroes indicates no incoming edges. If both are true, the vertex is isolated. This detection process has a time complexity of O(n) per vertex, since it requires scanning a full row and column. While not as fast as constant time operations, this is still practical for small to moderately sized graphs. In sparse graphs, though, adjacency lists are more efficient for this task since they allow quicker checks on connectedness by examining list lengths directly.

The amount of memory an adjacency matrix consumes is heavily influenced by the data type chosen to store its entries. For example, if the matrix is implemented using simple integers (0 or 1) to represent edge presence, each element typically uses 4 bytes (assuming 32-bit integers), although this can be reduced using booleans or bit arrays, which use as little as 1 bit per entry. However, when using floating-point numbers (to store weights with decimal precision), each matrix element might take 8 bytes (assuming 64-bit floats), which doubles memory usage. For weighted graphs that need to support the concept of “no connection”, using None, null, or a placeholder like infinity may require storing additional metadata or using special representations like object references, which take up more space. Additionally, using higher-level data structures such as Python lists or Java objects introduces overhead due to internal pointers and array headers. In large graphs, this difference becomes significant. Therefore, careful data type selection can substantially impact memory efficiency.

Yes, an adjacency matrix can be compressed, especially when dealing with sparse graphs, where the majority of matrix entries are zero or empty. One common method is to use sparse matrix representations, such as Compressed Sparse Row (CSR) or Compressed Sparse Column (CSC) formats. These techniques store only the non-zero entries and their positions, significantly reducing memory usage. In CSR, for instance, three one-dimensional arrays are used: one for storing all non-zero values, one for the column indices of these values, and one to mark where each row starts and ends. These formats are widely used in scientific computing and graph libraries. Another method is run-length encoding, where sequences of identical values (like long runs of zeros) are stored as value-count pairs. Compression is useful for very large, sparse graphs, but it does come with trade-offs in access time. Random access to edges is no longer constant time and becomes slower, typically O(log n) or O(k), depending on implementation.

Hire a tutor

Please fill out the form and we'll find a tutor for you.

1/2
Your details
Alternatively contact us via
WhatsApp, Phone Call, or Email