Search⌘ K
AI Features

Graph Representations: Adjacency Matrix vs. Adjacency List

Explore how graphs can be represented in C# using adjacency matrix and adjacency list structures. Learn the advantages and limitations of each method, and understand when to use each based on graph density and application needs.

A graph can be represented in memory in different ways. Two of the most common representations are the adjacency matrix and the adjacency list. Each representation has its own advantages and limitations, and the best choice depends on the graph and the problem we want to solve.

Adjacency matrix

An adjacency matrix uses a two-dimensional array, such as bool[,] or int[,] in C#, to represent connections between vertices. If a graph has n vertices, then the matrix has n×nn \times n ...