Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (4.22 KB)

1
# coding=utf8
2
#    Copyright (C) 2018 by
3
#    Pranay Kanwar <pranay.kanwar@gmail.com>
4
#    All rights reserved.
5
#    BSD license.
6
#
7
"""Percolation centrality measures."""
8
from __future__ import division
9

    
10
__author__ = """\n""".join(['Pranay Kanwar <pranay.kanwar@gmail.com>'])
11

    
12
import networkx as nx
13

    
14
from networkx.algorithms.centrality.betweenness import\
15
    _single_source_dijkstra_path_basic as dijkstra
16
from networkx.algorithms.centrality.betweenness import\
17
    _single_source_shortest_path_basic as shortest_path
18

    
19
__all__ = ['percolation_centrality']
20

    
21

    
22
def percolation_centrality(G, attribute='percolation',
23
                           states=None, weight=None):
24
    r"""Compute the percolation centrality for nodes.
25

26
    Percolation centrality of a node $v$, at a given time, is defined
27
    as the proportion of ‘percolated paths’ that go through that node.
28

29
    This measure quantifies relative impact of nodes based on their
30
    topological connectivity, as well as their percolation states.
31

32
    Percolation states of nodes are used to depict network percolation
33
    scenarios (such as during infection transmission in a social network
34
    of individuals, spreading of computer viruses on computer networks, or
35
    transmission of disease over a network of towns) over time. In this
36
    measure usually the percolation state is expressed as a decimal
37
    between 0.0 and 1.0.
38

39
    When all nodes are in the same percolated state this measure is
40
    equivalent to betweenness centrality.
41

42
    Parameters
43
    ----------
44
    G : graph
45
      A NetworkX graph.
46

47
    attribute : None or string, optional (default='percolation')
48
      Name of the node attribute to use for percolation state, used
49
      if `states` is None.
50

51
    states : None or dict, optional (default=None)
52
      Specify percolation states for the nodes, nodes as keys states
53
      as values.
54

55
    weight : None or string, optional (default=None)
56
      If None, all edge weights are considered equal.
57
      Otherwise holds the name of the edge attribute used as weight.
58

59
    Returns
60
    -------
61
    nodes : dictionary
62
       Dictionary of nodes with percolation centrality as the value.
63

64
    See Also
65
    --------
66
    betweenness_centrality
67

68
    Notes
69
    -----
70
    The algorithm is from Mahendra Piraveenan, Mikhail Prokopenko, and
71
    Liaquat Hossain [1]_
72
    Pair dependecies are calculated and accumulated using [2]_
73

74
    For weighted graphs the edge weights must be greater than zero.
75
    Zero edge weights can produce an infinite number of equal length
76
    paths between pairs of nodes.
77

78
    References
79
    ----------
80
    .. [1] Mahendra Piraveenan, Mikhail Prokopenko, Liaquat Hossain
81
       Percolation Centrality: Quantifying Graph-Theoretic Impact of Nodes
82
       during Percolation in Networks
83
       http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0053095
84
    .. [2] Ulrik Brandes:
85
       A Faster Algorithm for Betweenness Centrality.
86
       Journal of Mathematical Sociology 25(2):163-177, 2001.
87
       http://www.inf.uni-konstanz.de/algo/publications/b-fabc-01.pdf
88
    """
89
    percolation = dict.fromkeys(G, 0.0)  # b[v]=0 for v in G
90

    
91
    nodes = G
92

    
93
    if states is None:
94
        states = nx.get_node_attributes(nodes, attribute)
95

    
96
    # sum of all percolation states
97
    p_sigma_x_t = 0.0
98
    for v in states.values():
99
        p_sigma_x_t += v
100

    
101
    for s in nodes:
102
        # single source shortest paths
103
        if weight is None:  # use BFS
104
            S, P, sigma = shortest_path(G, s)
105
        else:  # use Dijkstra's algorithm
106
            S, P, sigma = dijkstra(G, s, weight)
107
        # accumulation
108
        percolation = _accumulate_percolation(percolation, G, S, P, sigma, s,
109
                                              states, p_sigma_x_t)
110

    
111
    n = len(G)
112

    
113
    for v in percolation:
114
        percolation[v] *= 1 / (n - 2)
115

    
116
    return percolation
117

    
118

    
119
def _accumulate_percolation(percolation, G, S, P, sigma, s,
120
                            states, p_sigma_x_t):
121
    delta = dict.fromkeys(S, 0)
122
    while S:
123
        w = S.pop()
124
        coeff = (1 + delta[w]) / sigma[w]
125
        for v in P[w]:
126
            delta[v] += sigma[v] * coeff
127
        if w != s:
128
            # percolation weight
129
            pw_s_w = states[s] / (p_sigma_x_t - states[w])
130
            percolation[w] += delta[w] * pw_s_w
131
    return percolation