Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3.52 KB)

1
#    Copyright (C) 2004-2019 by
2
#    Aric Hagberg <hagberg@lanl.gov>
3
#    Dan Schult <dschult@colgate.edu>
4
#    Pieter Swart <swart@lanl.gov>
5
#    All rights reserved.
6
#    BSD license.
7
#
8
# Authors: Aric Hagberg (hagberg@lanl.gov)
9
#          Pieter Swart (swart@lanl.gov)
10
#          Sasha Gutfraind (ag362@cornell.edu)
11
"""Degree centrality measures."""
12
import networkx as nx
13
from networkx.utils.decorators import not_implemented_for
14

    
15
__all__ = ['degree_centrality',
16
           'in_degree_centrality',
17
           'out_degree_centrality']
18

    
19

    
20
def degree_centrality(G):
21
    """Compute the degree centrality for nodes.
22

23
    The degree centrality for a node v is the fraction of nodes it
24
    is connected to.
25

26
    Parameters
27
    ----------
28
    G : graph
29
      A networkx graph
30

31
    Returns
32
    -------
33
    nodes : dictionary
34
       Dictionary of nodes with degree centrality as the value.
35

36
    See Also
37
    --------
38
    betweenness_centrality, load_centrality, eigenvector_centrality
39

40
    Notes
41
    -----
42
    The degree centrality values are normalized by dividing by the maximum
43
    possible degree in a simple graph n-1 where n is the number of nodes in G.
44

45
    For multigraphs or graphs with self loops the maximum degree might
46
    be higher than n-1 and values of degree centrality greater than 1
47
    are possible.
48
    """
49
    if len(G) <= 1:
50
        return {n: 1 for n in G}
51

    
52
    s = 1.0 / (len(G) - 1.0)
53
    centrality = {n: d * s for n, d in G.degree()}
54
    return centrality
55

    
56

    
57
@not_implemented_for('undirected')
58
def in_degree_centrality(G):
59
    """Compute the in-degree centrality for nodes.
60

61
    The in-degree centrality for a node v is the fraction of nodes its
62
    incoming edges are connected to.
63

64
    Parameters
65
    ----------
66
    G : graph
67
        A NetworkX graph
68

69
    Returns
70
    -------
71
    nodes : dictionary
72
        Dictionary of nodes with in-degree centrality as values.
73

74
    Raises
75
    ------
76
    NetworkXNotImplemented:
77
        If G is undirected.
78

79
    See Also
80
    --------
81
    degree_centrality, out_degree_centrality
82

83
    Notes
84
    -----
85
    The degree centrality values are normalized by dividing by the maximum
86
    possible degree in a simple graph n-1 where n is the number of nodes in G.
87

88
    For multigraphs or graphs with self loops the maximum degree might
89
    be higher than n-1 and values of degree centrality greater than 1
90
    are possible.
91
    """
92
    if len(G) <= 1:
93
        return {n: 1 for n in G}
94

    
95
    s = 1.0 / (len(G) - 1.0)
96
    centrality = {n: d * s for n, d in G.in_degree()}
97
    return centrality
98

    
99

    
100
@not_implemented_for('undirected')
101
def out_degree_centrality(G):
102
    """Compute the out-degree centrality for nodes.
103

104
    The out-degree centrality for a node v is the fraction of nodes its
105
    outgoing edges are connected to.
106

107
    Parameters
108
    ----------
109
    G : graph
110
        A NetworkX graph
111

112
    Returns
113
    -------
114
    nodes : dictionary
115
        Dictionary of nodes with out-degree centrality as values.
116

117
    Raises
118
    ------
119
    NetworkXNotImplemented:
120
        If G is undirected.
121

122
    See Also
123
    --------
124
    degree_centrality, in_degree_centrality
125

126
    Notes
127
    -----
128
    The degree centrality values are normalized by dividing by the maximum
129
    possible degree in a simple graph n-1 where n is the number of nodes in G.
130

131
    For multigraphs or graphs with self loops the maximum degree might
132
    be higher than n-1 and values of degree centrality greater than 1
133
    are possible.
134
    """
135
    if len(G) <= 1:
136
        return {n: 1 for n in G}
137

    
138
    s = 1.0 / (len(G) - 1.0)
139
    centrality = {n: d * s for n, d in G.out_degree()}
140
    return centrality