Graphs and their representations
Vertices and edges model relationships; adjacency lists and matrices are two ways to store the same graph with different costs.
Trees are graphs with rules. Remove the rules — allow any node to connect to any other, allow cycles, allow direction on the edges — and you get the most general relational structure in computer science. Maps, social networks, dependency graphs and the web itself are all graphs, and almost every AI pipeline ends up walking one.
Vertices are things, edges are relationships
A graph is a set of vertices plus a set of edges that say which pairs are connected. Everything else — directions, weights, labels — is extra data on top. Once you can name the things and the relationships, you can choose a representation and an algorithm.
Add edges below and flip between an adjacency list and an adjacency matrix to see the same graph stored two ways.
Add edges and switch how the graph is stored
Adjacency lists win on sparse graphs (most real graphs) because memory scales with the number of edges. Matrices win when the graph is dense or when the question is "is there an edge?" — that check is one array lookup. To be undirected, an edge is stored in both directions.
Vocabulary
- Directed edges have a direction (
u → vis notv → u); undirected edges are mutual. - Weighted edges carry a number such as a distance or cost.
- The degree of a vertex is how many edges touch it; in a directed graph, in-degree and out-degree are counted separately.
- A path is a sequence of edges; a cycle returns to where it started.
- A graph is connected if every vertex is reachable from every other.
Two representations
An adjacency list stores, for each vertex, the list of its neighbours. It uses space and enumerates neighbours in time proportional to their number — ideal for sparse graphs, which is almost all real ones.
An adjacency matrix is a grid of 0s and 1s. It uses space but answers "is there an edge between u and v?" in and is convenient when the graph is dense or the algorithm is matrix-flavoured.
graph = {
"A": ["B", "C"],
"B": ["D"],
"C": ["D", "F"],
"D": ["E"],
"E": [],
"F": [],
}This is an adjacency list built from a dict, with a list per vertex. To make it
undirected, add each edge in both directions; to make it weighted, store
(neighbour, weight) pairs instead of bare neighbours.
Sparse graphs punish matrices
A matrix for a graph with a million vertices needs a trillion cells even if it has only a few million edges. Always ask how dense the graph is before picking a representation — the wrong choice can be the difference between fitting in memory and not.
Graphs also generalise everything from this chapter: a linked list is a path graph, a tree is a connected acyclic graph, and a grid is a graph whose neighbours are the cells above, below, left and right.
Illustrative vs real
The panel uses five vertices and a handful of edges so the list and matrix fit side by side. Real graphs have millions of vertices, arrive as edge streams, and are often stored in compressed formats. The space formula — for a list versus for a matrix — is the whole point of the comparison.
Check yourself
Eduspheria wiki · Programming & Data Structures, Data structures
0 / 5 answered
From the assignment paper
Modeled on NITJ AI-507, Assignment/Quiz
0 / 6 answered
Where next: asymptotic analysis — the language for saying how these costs grow as the input does.