Adjacency Matrices Representations memory? 1. Adjacency matrices. - - PowerPoint PPT Presentation

adjacency matrices representations
SMART_READER_LITE
LIVE PREVIEW

Adjacency Matrices Representations memory? 1. Adjacency matrices. - - PowerPoint PPT Presentation

Lecture 11 | Part 1 Adjacency Matrices Representations memory? 1. Adjacency matrices. 2. Adjacency lists. 3. Dictionary of sets How do we store a graph in a computers Three approaches: Adjacency Matrices Assume nodes are


slide-1
SLIDE 1

Lecture 11 | Part 1

Adjacency Matrices

slide-2
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
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
SLIDE 4

Example

slide-5
SLIDE 5

Example

slide-6
SLIDE 6

Observations

▶ If 𝐻 is undirected, matrix is symmetric. ▶ If 𝐻 is directed, matrix may not be symmetric.

slide-7
SLIDE 7

Time Complexity

  • peration

code time edge query

adj[i,j] == 1

Θ(1) degree(𝑗)

np.sum(adj[i,:])

Θ(|𝑊|)

slide-8
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
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
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
SLIDE 11

Lecture 11 | Part 2

Adjacency Lists

slide-12
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
SLIDE 13

Adjacency Lists

▶ Create a list adj containing |𝑊| lists. ▶ adg[i] is list containing the neighbors of node 𝑗.

slide-14
SLIDE 14

Example

slide-15
SLIDE 15

Example

slide-16
SLIDE 16

Observations

▶ If 𝐻 is undirected, each edge appears twice. ▶ If 𝐻 is directed, each edge appears once.

slide-17
SLIDE 17

Time Complexity

  • peration

code time edge query

j in adj[i]

Θ(degree(𝑗)) degree(𝑗)

len(adj[i])

Θ(1)

slide-18
SLIDE 18

Space Requirements

▶ Need Θ(|𝑊|) space for outer list. ▶ Plus Θ(|𝐹|) space for inner lists. ▶ In total: Θ(|𝑊| + |𝐹|) space.

slide-19
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
SLIDE 20

Lecture 11 | Part 3

Dictionary of Sets

slide-21
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
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
SLIDE 23

Example

slide-24
SLIDE 24

Time Complexity

  • peration

code time edge query

j in adj[i]

Θ(1) average degree(𝑗)

len(adj[i])

Θ(1) average

slide-25
SLIDE 25

Space Requirements

▶ Requires only Θ(𝐹). ▶ But there is overhead to using hash tables.

slide-26
SLIDE 26

Dict-of-sets implementation

On datahub:

import dsc40graph

Or download from the course page.