def closeness_centrality(G, u=None, distance=None, wf_improved=True):
    if G.is_directed():
        G = G.reverse()
    if (distance is not None):
        path_length = functools.partial(nx.single_source_dijkstra_path_length, weight=distance)
    else:
        path_length = nx.single_source_shortest_path_length
    if (u is None):
        nodes = G.nodes
    else:
        nodes = [u]
    closeness_centrality = {}
    for n in nodes:
        sp = path_length(G, n)
        totsp = sum(sp.values())
        len_G = len(G)
        _closeness_centrality = 0.0
        if ((totsp > 0.0) and (len_G > 1)):
            _closeness_centrality = ((len(sp) - 1.0) / totsp)
            if wf_improved:
                s = ((len(sp) - 1.0) / (len_G - 1))
                _closeness_centrality *= s
        closeness_centrality[n] = _closeness_centrality
    if (u is not None):
        return closeness_centrality[u]
    else:
        return closeness_centrality
def closeness_centrality(G, u=None, distance=None, wf_improved=True):
    if G.is_directed():
        G = G.reverse()
    if (distance is not None):
        path_length = functools.partial(nx.single_source_dijkstra_path_length, weight=distance)
    else:
        path_length = nx.single_source_shortest_path_length
    if (u is None):
        nodes = G.nodes
    else:
        nodes = [u]
    closeness_centrality = {}
    for n in nodes:
        sp = path_length(G, n)
        totsp = sum(sp.values())
        len_G = len(G)
        _closeness_centrality = 0.0
        if ((totsp > 0.0) and (len_G > 1)):
            _closeness_centrality = ((len(sp) - 1.0) / totsp)
            if wf_improved:
                s = ((len(sp) - 1.0) / (len_G - 1))
                _closeness_centrality *= s
        closeness_centrality[n] = _closeness_centrality
    if (u is not None):
        return closeness_centrality[u]
    else:
        return closeness_centrality