Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/networkx/algorithms/vitality.py: 46%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

13 statements  

1""" 

2Vitality measures. 

3""" 

4 

5from functools import partial 

6 

7import networkx as nx 

8 

9__all__ = ["closeness_vitality"] 

10 

11 

12@nx._dispatchable(edge_attrs="weight") 

13def closeness_vitality(G, node=None, weight=None, wiener_index=None): 

14 """Returns the closeness vitality for nodes in the graph. 

15 

16 The *closeness vitality* of a node, defined in Section 3.6.2 of [1], 

17 is the change in the sum of distances between all node pairs when 

18 excluding that node. 

19 

20 Parameters 

21 ---------- 

22 G : NetworkX graph 

23 A strongly-connected graph. 

24 

25 weight : string 

26 The name of the edge attribute used as weight. This is passed 

27 directly to the :func:`~networkx.wiener_index` function. 

28 

29 node : object 

30 If specified, only the closeness vitality for this node will be 

31 returned. Otherwise, a dictionary mapping each node to its 

32 closeness vitality will be returned. 

33 

34 Other parameters 

35 ---------------- 

36 wiener_index : number 

37 If you have already computed the Wiener index of the graph 

38 `G`, you can provide that value here. Otherwise, it will be 

39 computed for you. 

40 

41 Returns 

42 ------- 

43 dictionary or float 

44 If `node` is None, this function returns a dictionary 

45 with nodes as keys and closeness vitality as the 

46 value. Otherwise, it returns only the closeness vitality for the 

47 specified `node`. 

48 

49 The closeness vitality of a node may be negative infinity if 

50 removing that node would disconnect the graph. 

51 

52 Examples 

53 -------- 

54 >>> G = nx.cycle_graph(3) 

55 >>> nx.closeness_vitality(G) 

56 {0: 2.0, 1: 2.0, 2: 2.0} 

57 

58 See Also 

59 -------- 

60 closeness_centrality 

61 

62 References 

63 ---------- 

64 .. [1] Ulrik Brandes, Thomas Erlebach (eds.). 

65 *Network Analysis: Methodological Foundations*. 

66 Springer, 2005. 

67 <http://books.google.com/books?id=TTNhSm7HYrIC> 

68 

69 """ 

70 if wiener_index is None: 

71 wiener_index = nx.wiener_index(G, weight=weight) 

72 if node is not None: 

73 after = nx.wiener_index(G.subgraph(set(G) - {node}), weight=weight) 

74 return wiener_index - after 

75 vitality = partial(closeness_vitality, G, weight=weight, wiener_index=wiener_index) 

76 return {v: vitality(node=v) for v in G}