Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / vitality.py @ 5cef0f13

History | View | Annotate | Download (2.56 KB)

1
# Copyright (C) 2010 by
2
#   Aric Hagberg (hagberg@lanl.gov)
3
#   Renato Fabbri
4
# Copyright (C) 2012 by
5
#   Aric Hagberg <hagberg@lanl.gov>
6
#   Dan Schult <dschult@colgate.edu>
7
#   Pieter Swart <swart@lanl.gov>
8
# Copyright (C) 2016-2019 by NetworkX developers.
9
#
10
# All rights reserved.
11
# BSD license.
12
"""
13
Vitality measures.
14
"""
15
from functools import partial
16

    
17
import networkx as nx
18

    
19
__all__ = ['closeness_vitality']
20

    
21

    
22
def closeness_vitality(G, node=None, weight=None, wiener_index=None):
23
    """Returns the closeness vitality for nodes in the graph.
24

25
    The *closeness vitality* of a node, defined in Section 3.6.2 of [1],
26
    is the change in the sum of distances between all node pairs when
27
    excluding that node.
28

29
    Parameters
30
    ----------
31
    G : NetworkX graph
32
        A strongly-connected graph.
33

34
    weight : string
35
        The name of the edge attribute used as weight. This is passed
36
        directly to the :func:`~networkx.wiener_index` function.
37

38
    node : object
39
        If specified, only the closeness vitality for this node will be
40
        returned. Otherwise, a dictionary mapping each node to its
41
        closeness vitality will be returned.
42

43
    Other parameters
44
    ----------------
45
    wiener_index : number
46
        If you have already computed the Wiener index of the graph
47
        `G`, you can provide that value here. Otherwise, it will be
48
        computed for you.
49

50
    Returns
51
    -------
52
    dictionary or float
53
        If `node` is None, this function returns a dictionary
54
        with nodes as keys and closeness vitality as the
55
        value. Otherwise, it returns only the closeness vitality for the
56
        specified `node`.
57

58
        The closeness vitality of a node may be negative infinity if
59
        removing that node would disconnect the graph.
60

61
    Examples
62
    --------
63
    >>> G = nx.cycle_graph(3)
64
    >>> nx.closeness_vitality(G)
65
    {0: 2.0, 1: 2.0, 2: 2.0}
66

67
    See Also
68
    --------
69
    closeness_centrality
70

71
    References
72
    ----------
73
    .. [1] Ulrik Brandes, Thomas Erlebach (eds.).
74
           *Network Analysis: Methodological Foundations*.
75
           Springer, 2005.
76
           <http://books.google.com/books?id=TTNhSm7HYrIC>
77

78
    """
79
    if wiener_index is None:
80
        wiener_index = nx.wiener_index(G, weight=weight)
81
    if node is not None:
82
        after = nx.wiener_index(G.subgraph(set(G) - {node}), weight=weight)
83
        return wiener_index - after
84
    vitality = partial(closeness_vitality, G, weight=weight,
85
                       wiener_index=wiener_index)
86
    # TODO This can be trivially parallelized.
87
    return {v: vitality(node=v) for v in G}