Bipartite graphs

Intermediate Network Analysis in Python

Eric Ma

Data Carpentry instructor and author of nxviz package

Bipartite graphs

  • A graph that is partitioned into two sets
  • Nodes are only connected to nodes in other partitions
  • Contrast: "unipartite"
Intermediate Network Analysis in Python

Bipartite graphs: Example

ch1-2.007.png

Intermediate Network Analysis in Python

Bipartite graphs in NetworkX

import networkx as nx
G = nx.Graph()

numbers = range(3) G.add_nodes_from(numbers, bipartite='customers')
letters = ['a', 'b'] G.add_nodes_from(letters, bipartite='products')
Intermediate Network Analysis in Python

Bipartite graphs in NetworkX

list(G.nodes(data=True))
[(0, {'bipartite': 'customers'}),
 (1, {'bipartite': 'customers'}),
 (2, {'bipartite': 'customers'}),
 ('b', {'bipartite': 'products'}),
 ('a', {'bipartite': 'products'})]
Intermediate Network Analysis in Python

Degree centrality

  • Definition: $$\frac{\text{number of neighbors}}{\text{number of possible neighbors}}$$
  • Number of possible neighbors depends on graph type
Intermediate Network Analysis in Python

Bipartite centrality metrics

  • Denominator: number of nodes in opposite partition, rather than all other nodes

ch1-2.021.png

Intermediate Network Analysis in Python

Filtering graphs

cust_nodes = [n for n, d in G.nodes(data=True) if  
                  d['bipartite'] == 'customers']
cust_nodes
[(0, {'bipartite': 'customers'}),
 (1, {'bipartite': 'customers'}),
 (2, {'bipartite': 'customers'})]
nx.bipartite.degree_centrality(G, cust_nodes)
{0: 0.5,
 1: 0.5,
 2: 1.0,
 'a': 0.333,
 'b': 1.0}
Intermediate Network Analysis in Python

Let's practice!

Intermediate Network Analysis in Python

Preparing Video For Download...