01 / 21

Representing Graphs
Adjacency List & Matrix

Learn two fundamental ways to store and represent graphs in discrete math: the adjacency matrix and the adjacency list.

Discrete Math

What Is a Graph?

A graph \( G = (V, E) \) is a structure consisting of a set of vertices (nodes) \( V \) and a set of edges \( E \) connecting pairs of vertices. Graphs model relationships: social networks, road maps, computer networks, and more.

A B C D
\( V = \{A, B, C, D\} \)
\( |E| = 5 \)

Directed vs Undirected Graphs

Undirected Graph
Edges have no direction. If \( \{u, v\} \in E \), then both \( u \) and \( v \) are adjacent to each other.
Directed Graph (Digraph)
Edges are ordered pairs. \( (u, v) \in E \) means there is an edge from \( u \) to \( v \), but not necessarily from \( v \) to \( u \).
Undirected 1 2 3 Directed 1 2 3

Why Represent Graphs?

Before we can run algorithms on a graph (shortest path, connectivity, coloring), we need to store the graph in a computer's memory. The two most common representations are:

Adjacency Matrix
A \( |V| \times |V| \) grid of 0s and 1s. Quick edge lookup in \( O(1) \).
Adjacency List
For each vertex, a list of its neighbors. Space-efficient for sparse graphs.

The Adjacency Matrix

For a graph \( G = (V, E) \) with \( n = |V| \) vertices, the adjacency matrix \( A \) is an \( n \times n \) matrix where:

1
Undirected graph: The matrix is symmetric: \( A[i][j] = A[j][i] \).
2
Directed graph: The matrix may be asymmetric: \( A[i][j] \neq A[j][i] \) in general.
3
No self-loops: Diagonal entries \( A[i][i] = 0 \) (unless self-loops are allowed).

Matrix Example: Undirected Graph

Graph G 1 2 3 4 Adjacency Matrix A 1 2 3 4 1 0 1 1 0 2 1 0 1 0 3 1 1 0 1 4 0 0 1 0
E = {1-2, 1-3, 2-3, 3-4, 1-4}

The Adjacency List

An adjacency list stores, for each vertex \( v \in V \), a list of all vertices adjacent to \( v \). This is typically an array of linked lists or dynamic arrays.

1
Undirected: If \( \{u, v\} \in E \), then \( u \in \text{Adj}[v] \) and \( v \in \text{Adj}[u] \).
2
Directed: If \( (u, v) \in E \), then \( v \in \text{Adj}[u] \) only.
3
Space: \( O(|V| + |E|) \) total, much less than \( O(|V|^2) \) for sparse graphs.

List Example: Same Graph

Graph G 1 2 3 4 Adjacency List 1 2 3 4 2 1 3 3 1 2 4 4 3

Matrix vs List: Key Distinction

Adjacency Matrix Adjacency List
Space \( O(|V|^2) \) \( O(|V| + |E|) \)
Check edge \( (u,v) \) \( O(1) \) \( O(\deg(u)) \)
List all neighbors \( O(|V|) \) \( O(\deg(v)) \)
Add edge \( O(1) \) \( O(1) \)
Best for Dense graphs Sparse graphs

How to Build a Representation

1
Label the vertices: Assign each vertex a number from \( 0 \) (or \( 1 \)) to \( n - 1 \).
2
List all edges: Write down every edge pair \( (u, v) \).
3
For a matrix: Create an \( n \times n \) grid of 0s. Set \( A[u][v] = 1 \) for each edge. If undirected, also set \( A[v][u] = 1 \).
4
For a list: Create \( n \) empty lists. Append \( v \) to \( \text{Adj}[u] \) for each edge. If undirected, also append \( u \) to \( \text{Adj}[v] \).
5
Verify: Count entries. Matrix should have \( 2|E| \) ones (undirected). List should have \( 2|E| \) total entries (undirected).

Example 1: Triangle Graph

A B C Matrix A B C A 0 1 1 B 1 0 1 C 1 1 0 A: [B, C] B: [A, C] C: [A, B]

Example 2: Directed Graph

Digraph 1 2 3 Matrix (NOT symmetric) 1 2 3 1 0 1 1 2 0 0 1 3 1 0 0 Adj[1] = [2, 3] Adj[2] = [3] Adj[3] = [1]

Example 3: A Larger Undirected Graph

0 1 2 3 4 Adjacency List 0: [1, 4] 1: [0, 2, 3] 2: [1, 3] 3: [2, 4, 1] 4: [0, 3]
\( |V| = 5, |E| = 6 \)
Total list entries = 12 = \( 2 \times 6 \)

Reading Degree from Representations

The degree of vertex \( v \), written \( \deg(v) \), is the number of edges incident to \( v \).

From the Matrix
Sum row \( v \): \( \deg(v) = \sum_{j} A[v][j] \). This takes \( O(|V|) \) time.
From the List
\( \deg(v) = |\text{Adj}[v]| \), the length of the list. This takes \( O(1) \) if stored.

Common Mistakes

✕ Forgetting Symmetry
In undirected graphs, if you set \( A[u][v] = 1 \), you must also set \( A[v][u] = 1 \). Same for adjacency lists: add to both.
✕ Wrong Matrix Size
The matrix is \( |V| \times |V| \), not \( |E| \times |E| \). Rows and columns correspond to vertices, not edges.
✕ Mixing Directed/Undirected
For a digraph, only add \( v \) to Adj[\( u \)] for edge \( (u,v) \). Do not add \( u \) to Adj[\( v \)] unless \( (v,u) \) also exists.

Summary Comparison

Property Adjacency Matrix Adjacency List
Space complexity \( \Theta(V^2) \) \( \Theta(V + E) \)
Edge lookup \( O(1) \) \( O(\deg(v)) \)
Iterate neighbors \( O(V) \) \( O(\deg(v)) \)
Undirected symmetry Symmetric matrix Both lists updated
Simple implementation 2D array Array of lists
Ideal use case Dense, \( E \approx V^2 \) Sparse, \( E \ll V^2 \)

Question 1 of 5

What is the space complexity of an adjacency matrix for a graph with \( n \) vertices?

Question 2 of 5

In the adjacency matrix of an undirected graph, which property always holds?

Question 3 of 5

An undirected graph has \( 4 \) vertices and \( 5 \) edges. How many total entries appear across all adjacency lists?

Question 4 of 5

Which representation is more space-efficient for a sparse graph where \( |E| \ll |V|^2 \)?

Question 5 of 5

A directed graph has vertices \( \{1, 2, 3\} \) and edges \( (1, 2), (2, 3), (3, 1) \). What is \( A[2][1] \) in its adjacency matrix?