Graph as adjacency matrix time complexity
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Graphs are fundamental structures in computer science and mathematics, capturing pairwise relationships between objects. They are used in various areas like network analysis, pathway identification, and relationship mapping. Among the different ways to represent a graph, the adjacency matrix is a powerful approach. This article explores the intricacies of graph representation using adjacency matrices, particularly focusing on the time complexity associated with this representation.
Basics of Adjacency Matrix Representation
An adjacency matrix is a 2D array, typically a square matrix, used to represent a finite graph. Each element of the matrix indicates whether pairs of vertices are adjacent or not in the graph. Suppose you have a graph `G` with `n` vertices labeled as `0, 1, ..., n-1`.
Matrix Configuration
In the adjacency matrix `A` of graph `G`:
• : There is an edge from vertex `i` to vertex `j`. • : There is no edge from vertex `i` to vertex `j`.
For an undirected graph represented with an adjacency matrix, is equal to . In directed graphs, this property does not hold, reflecting the directionality of edges.
Time Complexity Analysis
The time complexity associated with adjacency matrices primarily involves operations like checking for an edge, exploring a vertex's neighbors, and graph traversal. Let's examine these in detail.
Edge Checking
To determine if there is an edge between two vertices `i` and `j`, you access `A[i][j]`.
• Time Complexity: • The operation involves a simple matrix lookup, making it extremely efficient.
Neighbor Exploration
Exploring all neighbors of a vertex `i` involves iterating through the row corresponding to `i` to identify which columns have non-zero values.
• Time Complexity: • Each vertex in the graph is checked to determine if an edge exists, leading to a time complexity that scales with the number of vertices.
Graph Traversal
Graph traversal algorithms like DFS or BFS that use adjacency matrices involve iterating over all vertices and potentially all edges. The adjacency matrix is not space-efficient for sparse graphs due to its size.
• Time Complexity: • Traversals can require processing each element of the matrix, where is the number of vertices. Therefore, the time complexity can be quite high for dense graphs or when using matrix representations for traversal operations.
Memory Usage
Regardless of edge density, adjacency matrices always require space to store a graph with `n` vertices.
Comparison with Other Representations
Besides adjacency matrices, other graph representations like adjacency lists are widely used. Here's a comparison:
| Feature | Adjacency Matrix | Adjacency List |
| Space Complexity | where is the number of edges | |
| Edge Checking | ||
| Neighbor Iteration | ||
| Ideal for | Dense graphs | Sparse graphs |
Example
Consider a simple undirected graph with 4 vertices and the following edges:
• `(0,1)`, `(0,2)`, `(1,3)`, `(2,3)`
The adjacency matrix representation for this graph would look like this:
0 1 2 3 0 0 1 1 0 1 1 0 0 1 2 1 0 0 1 3 0 1 1 0
• Checking edge existence between vertices 0 and 1 is (True). • To find neighbors of vertex 0, observe row 0's non-zero elements, leading to vertices 1 and 2.
Related reading
- Graph auto-layout algorithm
- Graph serialization
- Graph theory - force based autolayout algorithm
- Graph theory best algorithm to find combination of edges “directions”, where each node has at most one edge directed to it
- Graph disconnected cannot obtain value for tensor Tensor
- Graph optimizations on a tensorflow serveable created using tf.Estimator
- Graphs find a sink in less than OV - or show it can't be done
- Griddbs data partitioning implementation and impact on performance

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.