networkx 2.8.2 Pypi GitHub Homepage
Other Docs
NotesParametersReturnsBackRef
closeness_centrality(G, u=None, distance=None, wf_improved=True)

Closeness centrality of a node u is the reciprocal of the average shortest path distance to u over all :None:None:`n-1` reachable nodes.

$$C(u) = \frac{n - 1}{\sum_{v=1}^{n-1} d(v, u)},$$

where :None:None:`d(v, u)` is the shortest-path distance between v and u, and n is the number of nodes that can reach u. Notice that the closeness distance function computes the incoming distance to u for directed graphs. To use outward distance, act on :None:None:`G.reverse()`.

Notice that higher values of closeness indicate higher centrality.

Wasserman and Faust propose an improved formula for graphs with more than one connected component. The result is "a ratio of the fraction of actors in the group who are reachable, to the average distance" from the reachable actors . You might think this scale factor is inverted but it is not. As is, nodes from small components receive a smaller closeness value. Letting :None:None:`N` denote the number of nodes in the graph,

$$C_{WF}(u) = \frac{n-1}{N-1} \frac{n - 1}{\sum_{v=1}^{n-1} d(v, u)},$$

Notes

The closeness centrality is normalized to :None:None:`(n-1)/(|G|-1)` where n is the number of nodes in the connected part of graph containing the node. If the graph is not completely connected, this algorithm computes the closeness centrality for each connected part separately scaled by that parts size.

If the 'distance' keyword is set to an edge attribute key then the shortest-path length will be computed using Dijkstra's algorithm with that edge attribute as the edge weight.

The closeness centrality uses inward distance to a node, not outward. If you want to use outword distances apply the function to :None:None:`G.reverse()`

In NetworkX 2.2 and earlier a bug caused Dijkstra's algorithm to use the outward distance rather than the inward distance. If you use a 'distance' keyword and a DiGraph, your results will change between v2.2 and v2.3.

Parameters

G : graph

A NetworkX graph

u : node, optional

Return only the value for node u

distance : edge attribute key, optional (default=None)

Use the specified edge attribute as the edge distance in shortest path calculations

wf_improved : bool, optional (default=True)

If True, scale by the fraction of nodes reachable. This gives the Wasserman and Faust improved formula. For single component graphs it is the same as the original formula.

Returns

nodes : dictionary

Dictionary of nodes with closeness centrality as the value.

Compute closeness centrality for nodes.

See Also

betweenness_centrality
degree_centrality
eigenvector_centrality
incremental_closeness_centrality
load_centrality

Examples

See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

networkx.algorithms.centrality.closeness.incremental_closeness_centrality

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /networkx/algorithms/centrality/closeness.py#12
type: <class 'function'>
Commit: