Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/networkx/algorithms/vitality.py: 42%
12 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-20 07:00 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-20 07:00 +0000
1"""
2Vitality measures.
3"""
4from functools import partial
6import networkx as nx
8__all__ = ["closeness_vitality"]
11@nx._dispatch(edge_attrs="weight")
12def closeness_vitality(G, node=None, weight=None, wiener_index=None):
13 """Returns the closeness vitality for nodes in the graph.
15 The *closeness vitality* of a node, defined in Section 3.6.2 of [1],
16 is the change in the sum of distances between all node pairs when
17 excluding that node.
19 Parameters
20 ----------
21 G : NetworkX graph
22 A strongly-connected graph.
24 weight : string
25 The name of the edge attribute used as weight. This is passed
26 directly to the :func:`~networkx.wiener_index` function.
28 node : object
29 If specified, only the closeness vitality for this node will be
30 returned. Otherwise, a dictionary mapping each node to its
31 closeness vitality will be returned.
33 Other parameters
34 ----------------
35 wiener_index : number
36 If you have already computed the Wiener index of the graph
37 `G`, you can provide that value here. Otherwise, it will be
38 computed for you.
40 Returns
41 -------
42 dictionary or float
43 If `node` is None, this function returns a dictionary
44 with nodes as keys and closeness vitality as the
45 value. Otherwise, it returns only the closeness vitality for the
46 specified `node`.
48 The closeness vitality of a node may be negative infinity if
49 removing that node would disconnect the graph.
51 Examples
52 --------
53 >>> G = nx.cycle_graph(3)
54 >>> nx.closeness_vitality(G)
55 {0: 2.0, 1: 2.0, 2: 2.0}
57 See Also
58 --------
59 closeness_centrality
61 References
62 ----------
63 .. [1] Ulrik Brandes, Thomas Erlebach (eds.).
64 *Network Analysis: Methodological Foundations*.
65 Springer, 2005.
66 <http://books.google.com/books?id=TTNhSm7HYrIC>
68 """
69 if wiener_index is None:
70 wiener_index = nx.wiener_index(G, weight=weight)
71 if node is not None:
72 after = nx.wiener_index(G.subgraph(set(G) - {node}), weight=weight)
73 return wiener_index - after
74 vitality = partial(closeness_vitality, G, weight=weight, wiener_index=wiener_index)
75 # TODO This can be trivially parallelized.
76 return {v: vitality(node=v) for v in G}