graph theory
adjacency matrix
time complexity
computational efficiency
data structures

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.

Practice algorithms

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`:

A[i][j]=1A[i][j] = 1: There is an edge from vertex `i` to vertex `j`. • A[i][j]=0A[i][j] = 0: There is no edge from vertex `i` to vertex `j`.

For an undirected graph represented with an adjacency matrix, A[i][j]A[i][j] is equal to A[j][i]A[j][i]. 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: O(1)O(1) • 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: O(n)O(n) • 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 n×nn \times n size.

Time Complexity: O(n2)O(n^2) • Traversals can require processing each element of the matrix, where nn 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 O(n2)O(n^2) 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:

FeatureAdjacency MatrixAdjacency List
Space ComplexityO(n2)O(n^2)O(n+m)O(n + m) where mm is the number of edges
Edge CheckingO(1)O(1)O(degree of vertex)O(\text{degree of vertex})
Neighbor IterationO(n)O(n)O(degree of vertex)O(\text{degree of vertex})
Ideal forDense graphsSparse 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 A[0][1]=1A[0][1] = 1 (True). • To find neighbors of vertex 0, observe row 0's non-zero elements, leading to vertices 1 and 2.


Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.