Adjacency List and Matrix Representation of Graph

โšก Smart Summary

Adjacency list and matrix representation of graph store vertices and edges in memory, allowing algorithms to traverse networks. Adjacency list uses linked lists per vertex while adjacency matrix uses a square two-dimensional grid.

  • ๐Ÿ“ Adjacency List: An array of V linked lists where each list at index i stores every vertex adjacent to vertex i, giving O(V + E) memory.
  • ๐Ÿ—บ๏ธ Adjacency Matrix: A V ร— V two-dimensional array where matrix[i][j] holds the edge weight or 1 when an edge exists between vertex i and vertex j.
  • โšก Lookup Speed: Adjacency matrix answers “is there an edge between i and j?” in O(1) time, while adjacency list needs O(degree) time to scan the neighbour list.
  • ๐Ÿ’พ Memory: Adjacency matrix always consumes O(Vยฒ) memory even for sparse graphs, whereas an adjacency list scales with the actual edge count.
  • ๐Ÿ” Best Fit: Choose the adjacency matrix for dense graphs with frequent edge queries and the adjacency list for sparse graphs and traversal-heavy workloads.
  • ๐Ÿ› ๏ธ Applications: Both representations power BFS, DFS, Dijkstra, PageRank, road-network routing, and Graph Neural Network pipelines used across AI systems.

Adjacency List and Matrix Representation of Graph

Even though they look different, all types of graphs can be represented in a similar way. There are generally two types of graph representation:

  1. Adjacency Matrix
  2. Adjacency List

Adjacency List

An adjacency list consists of linked lists. Each vertex is considered an array index, and each element represents a linked list. These linked lists contain the vertices that share an edge with the index vertex.

Here is an example of an adjacency list:

Adjacency List

Let a graph contain V number of vertices and E number of edges. The space complexity of the adjacency list is O(V + E), which scales with the number of real edges rather than every possible pair of vertices.

Worst-case space complexity becomes O(Vยฒ) if the given graph is a complete graph, since every vertex then connects to every other vertex.

Adjacency Matrix

An adjacency matrix is composed of a 2D array. For a graph with V vertices, the size of the matrix will be V ร— V.

Say matrix[i][j] = 5. It means there is an edge between node i and node j where the weight is 5.

Let us look at the following graph and its adjacency matrix:

Adjacency Matrix

We built the 2D array using these steps:

Step 1) Vertex A has a direct edge with B, and the weight is 5. So, the cell in row A and column B will be filled with 5. The rest of the cells in row A will be filled with zero.

Step 2) Vertex B has a direct edge with C, and the weight is 4. So, the cell in row B and column C will be filled with 4. The remaining cells in row B will be filled with zero, since B has no outgoing edge to any other node.

Step 3) Vertex C has no direct edges with any other vertices. So, row C will be filled with zeros.

Step 4) Vertex D has a directed edge with A and C.

  • The cell in row D and column A will have a value of 7. The cell in row D and column C will have a value of 2.
  • The rest of the cells in row D will be filled with zeros.

Step 5) Vertex E has a directed edge with B and D. The cell in row E and column B will have a value of 6. The cell in row E and column D will have a value of 3. The rest of the cells in row E will be filled with zeros.

Here are some points to notice:

  • The graph has no self-loops when the primary diagonal of the adjacency matrix is 0.
  • The graph is a directed graph if the cells at (a, b) and (b, a) do not hold the same value. Otherwise, the graph is undirected.
  • The graph is a weighted graph if the value of any cell is greater than 1.

The main problem with the adjacency matrix is that it requires squared space. Even edges that do not exist still allocate cells in memory.

For example, if we have a graph with 100 nodes, then 10,000 cells are needed to store it in RAM. With fewer edges in the graph, allocating such large memory can be wasteful. So the space complexity using the adjacency matrix is O(Nยฒ), where N is the number of nodes in the graph.

Adjacency List vs Adjacency Matrix

Before choosing a representation, it helps to compare both models side by side across the operations that dominate real graph workloads:

OperationAdjacency MatrixAdjacency List
Space complexityO(Vยฒ)O(V + E)
Add a vertexO(Vยฒ)O(1)
Add an edgeO(1)O(1)
Remove an edgeO(1)O(E)
Check if edge (i, j) existsO(1)O(degree of i)
Iterate over neighbours of iO(V)O(degree of i)
Best forDense graphs, frequent edge queriesSparse graphs, traversal-heavy tasks

In short, adjacency matrix wins on constant-time edge lookups, while adjacency list wins on memory and neighbour iteration, which is why algorithms such as BFS, DFS, and Dijkstra usually pair with adjacency lists.

Advantages and Disadvantages of Graph Representation

Each representation carries its own trade-offs. Knowing the strengths and weaknesses of both models helps you pick the right one for the problem you are solving.

Advantages of Adjacency Matrix:

  • Constant-time O(1) edge-existence queries between any pair of vertices.
  • Fixed indexing makes matrix-based algorithms such as Floyd-Warshall and transitive closure easy to implement.
  • Weighted edges fit naturally in a single matrix cell.

Disadvantages of Adjacency Matrix:

  • Wastes O(Vยฒ) memory when the graph is sparse.
  • Adding a new vertex requires resizing the entire matrix.
  • Iterating over neighbours of a single vertex takes O(V) even when the vertex has only a few edges.

Advantages of Adjacency List:

  • Uses only O(V + E) memory, which is close to the real edge count in sparse graphs.
  • Adding a new vertex or edge is O(1).
  • Traversal algorithms such as BFS and DFS iterate neighbours in O(degree), giving overall O(V + E) running time.

Disadvantages of Adjacency List:

  • Checking whether a specific edge exists takes O(degree) time instead of O(1).
  • Cache locality is weaker because linked lists are scattered across memory.
  • Weighted edges need a companion field or a list of pairs, slightly complicating the data structure.

When to Use Adjacency List vs Adjacency Matrix

The choice of representation depends on the density of the graph and the operations you run most often. Use this quick guide to pick the right structure:

  • Prefer the adjacency matrix when the graph is dense (E is close to Vยฒ), when edges rarely change, and when your algorithm asks “is there an edge between i and j?” many times.
  • Prefer the adjacency list when the graph is sparse (E is much smaller than Vยฒ), when the vertex or edge set grows during execution, and when you traverse the graph with BFS, DFS, or Dijkstra’s shortest-path algorithm.
  • Prefer a mixed model (adjacency list plus a hash set of edges) when you need both fast neighbour iteration and O(1) edge queries, at the cost of extra memory.

Modern graph libraries such as NetworkX and igraph default to adjacency lists because most real-world graphs โ€” social networks, road maps, web pages, package dependencies โ€” are sparse and traversal-heavy.

FAQs

An adjacency list is an array of V linked lists where each list at index i stores every vertex adjacent to vertex i. Memory usage is O(V + E), which suits sparse graphs and traversal algorithms such as BFS and DFS.

An adjacency matrix is a V ร— V two-dimensional array where matrix[i][j] holds the edge weight or 1 if an edge exists between vertex i and vertex j. Edge lookup is O(1) but memory is always O(Vยฒ).

Adjacency matrix answers edge-existence queries in O(1). Adjacency list iterates neighbours in O(degree), which is faster for traversal algorithms such as BFS, DFS, and Dijkstra. The best choice depends on the operations that dominate your workload.

Use an adjacency list when the graph is sparse, when vertices and edges change during execution, and when the algorithm traverses neighbours often. Social networks, road maps, and web-page graphs all fit this profile.

Use an adjacency matrix when the graph is dense, when the vertex set is fixed, and when the algorithm queries the same edge repeatedly. Floyd-Warshall and transitive closure both work naturally on adjacency matrices.

Yes. For directed graphs the matrix is not symmetric and the list stores only outgoing neighbours. For weighted graphs the matrix cell holds the weight while the list stores pairs of neighbour and weight.

Graph Neural Networks feed adjacency matrices or sparse edge tensors into machine learning layers for fraud detection, molecule property prediction, and recommendation systems. Knowledge graphs also rely on adjacency-list encodings for retrieval-augmented AI.

Yes. GitHub Copilot and ChatGPT generate adjacency list and matrix boilerplate for Python, C++, and Java. Developers still need to verify edge cases such as duplicate edges, self-loops, and correct handling of directed or weighted graphs.

Summarize this post with: