Bipartite graphs as matrices

Intermediate Network Analysis in Python

Eric Ma

Data Carpentry instructor and author of nxviz package

Matrix representation

  • Rows: nodes on one partition
  • Columns: nodes on other partition
  • Cells: 1 if edge present, else 0
Intermediate Network Analysis in Python

Matrix representation

ch2-2.006.png

Intermediate Network Analysis in Python

Example code

cust_nodes = [n for n in G.nodes() if G.nodes[n]
                 ['bipartite'] == 'customers']
prod_nodes = [n for n in G.nodes() if G.nodes[n]
                 ['bipartite'] == 'products']

mat = nx.bipartite.biadjacency_matrix(G, row_order=cust_nodes, column_order=prod_nodes)
mat
<3x2 sparse matrix of type '<class 'numpy.int64'>'
with 3 stored elements in Compressed Sparse Row format>
Intermediate Network Analysis in Python

Matrix projection

  • Projection computable using matrix multiplication

ch2-2.014.png

Intermediate Network Analysis in Python

Matrix projection

  • Projection computable using matrix multiplication

ch2-2.015.png

Intermediate Network Analysis in Python

Matrix projection

  • Projection computable using matrix multiplication

ch2-2.016.png

Intermediate Network Analysis in Python

Matrix projection

  • Projection computable using matrix multiplication

ch2-2.017.png

Intermediate Network Analysis in Python

Matrix projection

  • Projection computable using matrix multiplication

ch2-2.018.png

Intermediate Network Analysis in Python

Matrix projection

  • Projection computable using matrix multiplication

ch2-2.019.png

Intermediate Network Analysis in Python

Matrix projection

  • Projection computable using matrix multiplication

ch2-2.020.png

Intermediate Network Analysis in Python

Matrix projection

  • Projection computable using matrix multiplication

ch2-2.021.png

Intermediate Network Analysis in Python

Matrix projection

  • Projection computable using matrix multiplication

ch2-2.022.png

Intermediate Network Analysis in Python

Matrix projection

  • Projection computable using matrix multiplication

ch2-2.023.png

Intermediate Network Analysis in Python

Matrix projection

  • Projection computable using matrix multiplication

ch2-2.024.png

Intermediate Network Analysis in Python

Matrix projection

  • Projection computable using matrix multiplication

ch2-2.025.png

Intermediate Network Analysis in Python

Matrix multiplication in Python

mat @ mat.T
<5x5 sparse matrix of type '<class 'numpy.int64'>'
 with 23 stored elements in Compressed Sparse Row format>
mat.T @ mat
<10x10 sparse matrix of type '<class 'numpy.int64'>'
 with 50 stored elements in Compressed Sparse Column format>
Intermediate Network Analysis in Python

Let's practice!

Intermediate Network Analysis in Python

Preparing Video For Download...