def harmonic_centrality(G, nbunch=None, distance=None):
 if G.is_directed():
  G = G.reverse()
 spl = partial(nx.shortest_path_length, G, weight=distance)
 return {u: sum((((1 / d) if (d > 0) else 0) for (v, d) in spl(source=u))) for u in G.nbunch_iter(nbunch)}

def harmonic_centrality ( G , nbunch = None , distance = None ) : if G . is_directed ( ) : G = G . reverse ( ) spl = partial ( nx . shortest_path_length , G , weight = distance ) return { u : sum ( ( ( ( 1 / d ) if ( d > 0 ) else 0 ) for ( v , d ) in spl ( source = u ) ) ) for u in G . nbunch_iter ( nbunch ) }
compute harmonic centrality for nodes .