Skip to content

Instantly share code, notes, and snippets.

@clayadavis
Last active October 26, 2018 21:51
Show Gist options
  • Save clayadavis/724061da0f989bff6e7f25cbc21e63fa to your computer and use it in GitHub Desktop.
Save clayadavis/724061da0f989bff6e7f25cbc21e63fa to your computer and use it in GitHub Desktop.
Compute the multiscale backbone edge probabilities of a NetworkX graph
def compute_p(G, weight='weight', p='p'):
# Undirected
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]
p_ij = float(edge_weight)/sum_w
G[node][neighbor][p] = (1-p_ij)**(k_n-1)
def alpha_cut(G, alpha, p='p'):
C = G.copy()
C.remove_edges_from((u,v) for u,v,d in G.edges(data=True) if d['p'] < alpha)
return C
@yzjing
Copy link

yzjing commented Oct 26, 2018

In networkx 2.2 should be:
C.remove_edges_from((u,v) for u,v,d in G.edges.data() if d['p'] < alpha)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment