Introduction

A graph G=(V,E)G = (V, E) is a set of vertices VV and a set of edges EE, each edge joining two vertices. Graphs model road networks, links between web pages, dependencies between tasks and much more. Before any graph algorithm can run, the graph must be stored, and the choice of representation fixes the cost of the basic questions: "is there an edge from uu to vv?" and "what are the neighbours of uu?".

This note covers the adjacency matrix, the adjacency list, and the edge list and incidence matrix, with their space and time costs and the facts that follow from them. The graph theory itself (paths, connectivity, special graphs) is in Graph Theory Fundamentals; traversals are in Breadth-First Search and Depth-First Search.

Conventions: n=∣V∣n = |V|, m=∣E∣m = |E|; vertices are numbered 0,1,…,n−10, 1, \ldots, n-1; graphs are simple (no self-loops, no parallel edges) unless stated. An adjacency matrix or array of lists is 0-based unless a question states 1-based numbering. Linked adjacency lists use pointers, not array indices.

Prerequisites

Core concepts

Terminology needed here

  • In an undirected graph, edge {u,v}\{u, v\} makes uu and vv adjacent; the degree deg⁡(u)\deg(u) is the number of incident edges.
  • In a directed graph, edge (u,v)(u, v) goes from uu to vv; uu has out-degree and in-degree.
  • Handshake lemma: ∑udeg⁡(u)=2m\sum_{u} \deg(u) = 2m (undirected); ∑uout(u)=∑uin(u)=m\sum_u \text{out}(u) = \sum_u \text{in}(u) = m (directed).
  • A simple undirected graph has at most (n2)=n(n−1)/2\binom{n}{2} = n(n-1)/2 edges; a simple directed graph at most n(n−1)n(n-1).
  • Dense: mm close to n2n^2. Sparse: mm much smaller than n2n^2, for example m=O(n)m = O(n).

Running example

Undirected graph with n=5n = 5 and the m=5m = 5 edges {0,1},{0,2},{1,2},{1,3},{3,4}\{0,1\}, \{0,2\}, \{1,2\}, \{1,3\}, \{3,4\}. Degrees: 2, 3, 2, 2, 1, summing to 10=2m10 = 2m.

Adjacency matrix and adjacency list for one graph
Figure 1. The running example graph (left), its 5 by 5 adjacency matrix (centre, symmetric with a zero diagonal), and its adjacency lists (right) built by inserting each edge at the head of both endpoint lists in the order given.

Adjacency matrix

An n×nn\times n adjacency matrix stores 1 for an unweighted edge and 0 otherwise. For a weighted graph, store the edge weight and a distinct “no edge” sentinel outside the permitted weight domain, or track presence separately. Zero is a valid sentinel only if every edge weight is nonzero; ∞\infty works only when it is excluded as a genuine edge weight.

For the running example:

A=(0110010110110000100100010)A = \begin{pmatrix} 0 & 1 & 1 & 0 & 0\\ 1 & 0 & 1 & 1 & 0\\ 1 & 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 1\\ 0 & 0 & 0 & 1 & 0 \end{pmatrix}
  • Undirected graph: AA is symmetric; only the upper triangle needs storing (n(n−1)/2n(n-1)/2 entries without the diagonal; see triangular storage).
  • Simple graph: zero diagonal.
  • Row sum of row uu = degree (undirected) or out-degree (directed); column sum = in-degree.
  • The transpose ATA^{\mathsf T} represents the graph with every edge reversed.
  • Powers: the entry (Ak)[u][v](A^k)[u][v] is the number of walks of length exactly kk from uu to vv. In the example A2[0][3]=1A^2[0][3] = 1 (the walk 0, 1, 3), and the diagonal of A2A^2 is the degree sequence. The number of triangles in a simple undirected graph is trace⁡(A3)/6\operatorname{trace}(A^3)/6; here it is 1 (vertices 0, 1, 2).

Adjacency list

An array of nn lists; the list for uu holds every vv adjacent to uu (for a directed graph, every vv with an edge (u,v)(u, v)).

C
struct edge { int to; struct edge *next; };        /* add an int weight field if weighted */
struct edge *adj[V];                                /* adj[u]: head of u's list */

void add(int u, int v) {                            /* insert at head: O(1) */
    struct edge *e = malloc(sizeof *e);             /* allocation check omitted for brevity */
    e->to = v; e->next = adj[u]; adj[u] = e;
}
/* undirected edge {u, v}: add(u, v); add(v, u); */

Inserting at the head, edges added in the order given produce these lists for the running example:

Text
0: 2 1
1: 3 2 0
2: 1 0
3: 4 1
4: 3

The order of vertices inside a list depends on insertion order, and traversal algorithms visit neighbours in list order. A question that asks for a BFS or DFS order "using adjacency lists" therefore depends on how the lists were built; read the stated order.

  • Total list entries: 2m2m for undirected graphs, mm for directed graphs.
  • Length of the list of uu = degree (out-degree for directed).
  • Finding the in-degree of uu in a directed graph needs a scan of all lists: O(n+m)O(n + m).
  • The reversed graph GTG^{\mathsf T} is built from the lists in O(n+m)O(n + m).

Edge list and incidence matrix

  • Edge list: an array of the mm pairs (u,v)(u, v) (with weights). Space Θ(m)\Theta(m). Natural for Kruskal's algorithm, which sorts edges by weight (Minimum Spanning Trees).
  • Incidence matrix: n×mn \times m, entry 1 if the vertex is an endpoint of the edge. Each column of an undirected incidence matrix has exactly two 1s; row sums are the degrees. With BB the incidence matrix, BBT=D+ABB^{\mathsf T} = D + A, where DD is the diagonal matrix of degrees. Rarely used for algorithms because it costs Θ(nm)\Theta(nm) space.

How it works: choosing a representation

Question or costAdjacency matrixAdjacency list
SpaceΘ(n2)\Theta(n^2)Θ(n+m)\Theta(n + m)
Is (u,v)(u, v) an edge?O(1)O(1)O(deg⁡(u))O(\deg(u))
List the neighbours of uuΘ(n)\Theta(n)Θ(deg⁡(u))\Theta(\deg(u))
Visit every edge (BFS, DFS)Θ(n2)\Theta(n^2)Θ(n+m)\Theta(n + m)
Add an edgeO(1)O(1)O(1)O(1) at the head
Delete directed edge (u,v)(u,v)O(1)O(1)O(outdeg⁡(u))O(\operatorname{outdeg}(u)), searching the outgoing list
Delete undirected edge {u,v}\{u,v\}O(1)O(1)O(deg⁡(u)+deg⁡(v))O(\deg(u)+\deg(v)) without cross pointers: remove both list records
Add a vertexΘ(n2)\Theta(n^2) to rebuildO(1)O(1) amortised
  • Sparse graphs (most real graphs, trees, planar graphs) favour lists: Θ(n+m)\Theta(n + m) space and traversal.
  • Dense graphs, or algorithms that need many O(1)O(1) edge tests (for example Floyd-Warshall all-pairs shortest paths), favour the matrix.
  • BFS and DFS cost Θ(n+m)\Theta(n + m) with lists and Θ(n2)\Theta(n^2) with a matrix, because scanning a matrix row costs nn no matter how few neighbours there are.

For a tree with nn vertices (m=n−1m = n - 1) the matrix stores n2n^2 entries of which only 2(n−1)2(n-1) are 1; the lists store 2(n−1)2(n - 1) entries.

Worked examples

The adjacency lists used below are the ones built by the add function above (head insertion, edges in the order given).

Example 1. Running example: row sums of AA are 2, 3, 2, 2, 1; total 10=2m10 = 2m; the lists contain 2m=102m = 10 entries; the matrix has n2=25n^2 = 25 cells.

Example 2 (directed graph). n=4n = 4, edges (0,1),(0,2),(1,2),(2,0),(2,3)(0,1), (0,2), (1,2), (2,0), (2,3). Out-degrees 2, 1, 2, 0; in-degrees 1, 1, 2, 1; both sum to m=5m = 5. In the transpose, row 2 has 1s in columns 0 and 1 (the edges into vertex 2 were from 0 and 1).

Example 3 (walks). In the running example, A2[0][1]=1A^2[0][1] = 1 (walk 0, 2, 1) and A2[2][3]=1A^2[2][3] = 1 (walk 2, 1, 3); trace⁡(A3)=6\operatorname{trace}(A^3) = 6, giving one triangle.

Example 4 (incidence). The 5 by 5 incidence matrix of the running example has two 1s in every column and row sums 2, 3, 2, 2, 1.

Important properties and observations

  • Matrix: Θ(n2)\Theta(n^2) space regardless of mm; O(1)O(1) edge test.
  • Lists: Θ(n+m)\Theta(n + m) space; neighbour iteration proportional to degree.
  • An undirected graph's matrix is symmetric; each undirected edge appears twice in the lists.
  • The order of entries in each adjacency list decides tie-breaking in BFS and DFS.
  • AkA^k counts walks of length kk; trace⁡(A3)/6\operatorname{trace}(A^3)/6 counts triangles in a simple undirected graph.

GATE examination perspective

  • Space or time with a given representation: BFS or DFS is Θ(n+m)\Theta(n + m) with lists, Θ(n2)\Theta(n^2) with a matrix.
  • Degrees and edge counts from a matrix or from lists, using the handshake lemma.
  • Traversal orders that depend on adjacency list order.
  • Matrix facts: symmetry, row and column sums, transpose, powers counting walks.
  • Dense versus sparse choice for a stated algorithm.

Summary

Sources

  1. T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, 2009: Section 22.1 (representations of graphs: adjacency lists and adjacency matrix, transpose, incidence matrix in the exercises).
  2. K. H. Rosen, Discrete Mathematics and Its Applications, 7th ed., McGraw-Hill, 2012: Section 10.3 (representing graphs, adjacency and incidence matrices) and Section 10.4 (counting paths with powers of the adjacency matrix).

Next steps