Skip to content

Instantly share code, notes, and snippets.

@brianckeegan
Last active August 2, 2023 19:26
Show Gist options
  • Save brianckeegan/8846206 to your computer and use it in GitHub Desktop.
Save brianckeegan/8846206 to your computer and use it in GitHub Desktop.
Given a networkx graph containing weighted edges and a threshold parameter alpha, this code will return another networkx graph with the "backbone" of the graph containing a subset of weighted edges that fall above the threshold following the method in Serrano et al. 2008.
# Serrano, Boguna, Vespigani backbone extractor
# from http://www.pnas.org/content/106/16/6483.abstract
# Thanks to Michael Conover and Qian Zhang at Indiana with help on earlier versions
# Thanks to Clay Davis for pointing out an error
import networkx as nx
import numpy as np
def extract_backbone(g, weight='weight', alpha=.05):
backbone_graph = nx.Graph()
for node in g:
k_n = len(g[node])
if k_n > 1:
sum_w = sum( g[node][neighbor][weight] for neighbor in g[node] )
for neighbor in g[node]:
edge_weight = g[node][neighbor][weight]
pij = float(edge_weight)/sum_w
if (1-pij)**(k_n-1) < alpha: # equation 2
backbone_graph.add_edge( node,neighbor, weight = edge_weight)
return backbone_graph
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment