adjacency matrices representations
play

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


  1. Lecture 11 | Part 1 Adjacency Matrices

  2. Representations memory? 1. Adjacency matrices. 2. Adjacency lists. 3. “Dictionary of sets” ▶ How do we store a graph in a computer’s ▶ Three approaches:

  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 (𝑗, 𝑘) ∉ 𝐹

  4. Example

  5. Example

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

  7. Time Complexity operation code time edge query adj[i,j] == 1 Θ(1) degree( 𝑗 ) np.sum(adj[i,:]) Θ(|𝑊|)

  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.

  9. Example: Facebook = 500 petabits ≈ 6500 years of video at 1080p ≈ 60 copies of the internet as it was in 2000 ▶ Facebook has 2 billion users. (2 × 10 9 ) 2 = 4 × 10 18 bits

  10. Adjacency Matrices and Math of length 2 between 𝑗 and 𝑘 . ▶ Adjacency matrices are useful mathematically. ▶ Example: (𝑗, 𝑘) entry of 𝐵 2 gives number of hops

  11. Lecture 11 | Part 2 Adjacency Lists

  12. What’s Wrong with Adjacency Matrices? ▶ Requires Θ(|𝑊| 2 ) storage. ▶ Even if the graph has no edges. ▶ Idea : only store the edges that exist.

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

  14. Example

  15. Example

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

  17. Time Complexity operation code time edge query j in adj[i] Θ(degree(𝑗)) degree( 𝑗 ) len(adj[i]) Θ(1)

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

  19. Example: Facebook friendships. (2 bits × 200 × (2 billion ) = 3.2 terabytes = 0.04 years of HD video ▶ Facebook has 2 billion users, 400 billion ▶ If each edge requires 32 bits: = 64 × 400 × 10 9 bits

  20. Lecture 11 | Part 3 Dictionary of Sets

  21. Tradeofgs ▶ Adjacency matrix: fast edge query, lots of space. ▶ Adjacency list: slower edge query, space effjcient. ▶ Can we have the best of both?

  22. Idea arbitrary labels. ▶ Use hash tables . ▶ Replace inner edge lists by set s. ▶ Replace outer list with dict . ▶ Doesn’t speed things up, but allows nodes to have

  23. Example

  24. Time Complexity operation code time edge query j in adj[i] degree( 𝑗 ) len(adj[i]) Θ(1) average Θ(1) average

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

  26. Dict-of-sets implementation On datahub: import dsc40graph Or download from the course page.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend