Graph representations
This chapter stores graphs and compares representation costs. Graph-theoretic properties belong to Engineering Mathematics; BFS, DFS, shortest paths and minimum spanning trees belong to Algorithms.
10.1 Matrices, adjacency lists and edge lists
Let be a graph, its vertex count and edge count. A directed adjacency matrix stores an entry at row , column for edge . An undirected matrix is symmetric. A weighted matrix must distinguish “no edge” from a genuine zero-weight edge, for example with a separate marker.
A matrix tests edge existence in constant time. An unsorted list takes to test ; storing incoming lists separately supports indegree-related access. For an undirected edge deletion, update both adjacency records. Representation choices depend on density and operations, not just memory.
10.2 Incidence storage and traversal interfaces
An incidence matrix has one row per vertex and one column per edge. For a directed edge, use at its tail and at its head; each non-loop column sums to zero. For an undirected non-loop edge, both endpoint entries are 1. Storage is entries, usually larger than adjacency lists for sparse graphs.
Traversal algorithms consume a neighbour iterator. Scanning every adjacency list once touches scale data; scanning every row of a matrix touches entries. The iterator's order affects traversal order but not the graph itself. Inserting edges at the head of a list reverses their insertion order. For a directed graph, outgoing lists alone do not directly enumerate incoming neighbours; reverse edges to obtain the transpose representation.
Last-minute recall
- Distinguish absence from a zero edge weight.
- Undirected adjacency lists normally store each edge twice.
- Matrices give constant-time edge lookup.
- Lists make sparse neighbour enumeration efficient.
- Preserve isolated vertices when storing an edge list.
- State the directed incidence sign convention.
- Representation order can change traversal order.