Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/vitality.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 """ | |
2 Vitality measures. | |
3 """ | |
4 from functools import partial | |
5 | |
6 import networkx as nx | |
7 | |
8 __all__ = ["closeness_vitality"] | |
9 | |
10 | |
11 def closeness_vitality(G, node=None, weight=None, wiener_index=None): | |
12 """Returns the closeness vitality for nodes in the graph. | |
13 | |
14 The *closeness vitality* of a node, defined in Section 3.6.2 of [1], | |
15 is the change in the sum of distances between all node pairs when | |
16 excluding that node. | |
17 | |
18 Parameters | |
19 ---------- | |
20 G : NetworkX graph | |
21 A strongly-connected graph. | |
22 | |
23 weight : string | |
24 The name of the edge attribute used as weight. This is passed | |
25 directly to the :func:`~networkx.wiener_index` function. | |
26 | |
27 node : object | |
28 If specified, only the closeness vitality for this node will be | |
29 returned. Otherwise, a dictionary mapping each node to its | |
30 closeness vitality will be returned. | |
31 | |
32 Other parameters | |
33 ---------------- | |
34 wiener_index : number | |
35 If you have already computed the Wiener index of the graph | |
36 `G`, you can provide that value here. Otherwise, it will be | |
37 computed for you. | |
38 | |
39 Returns | |
40 ------- | |
41 dictionary or float | |
42 If `node` is None, this function returns a dictionary | |
43 with nodes as keys and closeness vitality as the | |
44 value. Otherwise, it returns only the closeness vitality for the | |
45 specified `node`. | |
46 | |
47 The closeness vitality of a node may be negative infinity if | |
48 removing that node would disconnect the graph. | |
49 | |
50 Examples | |
51 -------- | |
52 >>> G = nx.cycle_graph(3) | |
53 >>> nx.closeness_vitality(G) | |
54 {0: 2.0, 1: 2.0, 2: 2.0} | |
55 | |
56 See Also | |
57 -------- | |
58 closeness_centrality | |
59 | |
60 References | |
61 ---------- | |
62 .. [1] Ulrik Brandes, Thomas Erlebach (eds.). | |
63 *Network Analysis: Methodological Foundations*. | |
64 Springer, 2005. | |
65 <http://books.google.com/books?id=TTNhSm7HYrIC> | |
66 | |
67 """ | |
68 if wiener_index is None: | |
69 wiener_index = nx.wiener_index(G, weight=weight) | |
70 if node is not None: | |
71 after = nx.wiener_index(G.subgraph(set(G) - {node}), weight=weight) | |
72 return wiener_index - after | |
73 vitality = partial(closeness_vitality, G, weight=weight, wiener_index=wiener_index) | |
74 # TODO This can be trivially parallelized. | |
75 return {v: vitality(node=v) for v in G} |