SLIDE 1
Lecture 11 | Part 1
Adjacency Matrices
SLIDE 2 Representations
▶ How do we store a graph in a computer’s memory? ▶ Three approaches:
- 1. Adjacency matrices.
- 2. Adjacency lists.
- 3. “Dictionary of sets”
SLIDE 3
Adjacency Matrices
▶ Assume nodes are numbered 0, 1, …, |𝑊| − 1 ▶ Allocate a |𝑊| × |𝑊| (Numpy) array ▶ Fill array as follows:
▶ arr[i,j] = 1 if (𝑗, 𝑘) ∈ 𝐹 ▶ arr[i,j] = 0 if (𝑗, 𝑘) ∉ 𝐹
SLIDE 4
Example
SLIDE 5
Example
SLIDE 6
Observations
▶ If 𝐻 is undirected, matrix is symmetric. ▶ If 𝐻 is directed, matrix may not be symmetric.
SLIDE 7 Time Complexity
code time edge query
adj[i,j] == 1
Θ(1) degree(𝑗)
np.sum(adj[i,:])
Θ(|𝑊|)
SLIDE 8
Space Requirements
▶ Uses |𝑊|2 bits, even if there are very few edges. ▶ But most real-world graphs are sparse.
▶ They contain many fewer edges than possible.
SLIDE 9
Example: Facebook
▶ Facebook has 2 billion users. (2 × 109)2 = 4 × 1018 bits = 500 petabits ≈ 6500 years of video at 1080p ≈ 60 copies of the internet as it was in 2000
SLIDE 10 Adjacency Matrices and Math
▶ Adjacency matrices are useful mathematically. ▶ Example: (𝑗, 𝑘) entry of 𝐵2 gives number of hops
- f length 2 between 𝑗 and 𝑘.
SLIDE 11
Lecture 11 | Part 2
Adjacency Lists
SLIDE 12
What’s Wrong with Adjacency Matrices?
▶ Requires Θ(|𝑊|2) storage. ▶ Even if the graph has no edges. ▶ Idea: only store the edges that exist.
SLIDE 13
Adjacency Lists
▶ Create a list adj containing |𝑊| lists. ▶ adg[i] is list containing the neighbors of node 𝑗.
SLIDE 14
Example
SLIDE 15
Example
SLIDE 16
Observations
▶ If 𝐻 is undirected, each edge appears twice. ▶ If 𝐻 is directed, each edge appears once.
SLIDE 17 Time Complexity
code time edge query
j in adj[i]
Θ(degree(𝑗)) degree(𝑗)
len(adj[i])
Θ(1)
SLIDE 18
Space Requirements
▶ Need Θ(|𝑊|) space for outer list. ▶ Plus Θ(|𝐹|) space for inner lists. ▶ In total: Θ(|𝑊| + |𝐹|) space.
SLIDE 19
Example: Facebook
▶ Facebook has 2 billion users, 400 billion friendships. ▶ If each edge requires 32 bits: (2 bits × 200 × (2 billion) = 64 × 400 × 109 bits = 3.2 terabytes = 0.04 years of HD video
SLIDE 20
Lecture 11 | Part 3
Dictionary of Sets
SLIDE 21
Tradeofgs
▶ Adjacency matrix: fast edge query, lots of space. ▶ Adjacency list: slower edge query, space effjcient. ▶ Can we have the best of both?
SLIDE 22
Idea
▶ Use hash tables. ▶ Replace inner edge lists by sets. ▶ Replace outer list with dict.
▶ Doesn’t speed things up, but allows nodes to have arbitrary labels.
SLIDE 23
Example
SLIDE 24 Time Complexity
code time edge query
j in adj[i]
Θ(1) average degree(𝑗)
len(adj[i])
Θ(1) average
SLIDE 25
Space Requirements
▶ Requires only Θ(𝐹). ▶ But there is overhead to using hash tables.
SLIDE 26
Dict-of-sets implementation
On datahub:
import dsc40graph
Or download from the course page.