Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/networkx/algorithms/centrality/degree_alg.py: 42%

26 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-20 07:00 +0000

1"""Degree centrality measures.""" 

2import networkx as nx 

3from networkx.utils.decorators import not_implemented_for 

4 

5__all__ = ["degree_centrality", "in_degree_centrality", "out_degree_centrality"] 

6 

7 

8@nx._dispatch 

9def degree_centrality(G): 

10 """Compute the degree centrality for nodes. 

11 

12 The degree centrality for a node v is the fraction of nodes it 

13 is connected to. 

14 

15 Parameters 

16 ---------- 

17 G : graph 

18 A networkx graph 

19 

20 Returns 

21 ------- 

22 nodes : dictionary 

23 Dictionary of nodes with degree centrality as the value. 

24 

25 Examples 

26 -------- 

27 >>> G = nx.Graph([(0, 1), (0, 2), (0, 3), (1, 2), (1, 3)]) 

28 >>> nx.degree_centrality(G) 

29 {0: 1.0, 1: 1.0, 2: 0.6666666666666666, 3: 0.6666666666666666} 

30 

31 See Also 

32 -------- 

33 betweenness_centrality, load_centrality, eigenvector_centrality 

34 

35 Notes 

36 ----- 

37 The degree centrality values are normalized by dividing by the maximum 

38 possible degree in a simple graph n-1 where n is the number of nodes in G. 

39 

40 For multigraphs or graphs with self loops the maximum degree might 

41 be higher than n-1 and values of degree centrality greater than 1 

42 are possible. 

43 """ 

44 if len(G) <= 1: 

45 return {n: 1 for n in G} 

46 

47 s = 1.0 / (len(G) - 1.0) 

48 centrality = {n: d * s for n, d in G.degree()} 

49 return centrality 

50 

51 

52@not_implemented_for("undirected") 

53@nx._dispatch 

54def in_degree_centrality(G): 

55 """Compute the in-degree centrality for nodes. 

56 

57 The in-degree centrality for a node v is the fraction of nodes its 

58 incoming edges are connected to. 

59 

60 Parameters 

61 ---------- 

62 G : graph 

63 A NetworkX graph 

64 

65 Returns 

66 ------- 

67 nodes : dictionary 

68 Dictionary of nodes with in-degree centrality as values. 

69 

70 Raises 

71 ------ 

72 NetworkXNotImplemented 

73 If G is undirected. 

74 

75 Examples 

76 -------- 

77 >>> G = nx.DiGraph([(0, 1), (0, 2), (0, 3), (1, 2), (1, 3)]) 

78 >>> nx.in_degree_centrality(G) 

79 {0: 0.0, 1: 0.3333333333333333, 2: 0.6666666666666666, 3: 0.6666666666666666} 

80 

81 See Also 

82 -------- 

83 degree_centrality, out_degree_centrality 

84 

85 Notes 

86 ----- 

87 The degree centrality values are normalized by dividing by the maximum 

88 possible degree in a simple graph n-1 where n is the number of nodes in G. 

89 

90 For multigraphs or graphs with self loops the maximum degree might 

91 be higher than n-1 and values of degree centrality greater than 1 

92 are possible. 

93 """ 

94 if len(G) <= 1: 

95 return {n: 1 for n in G} 

96 

97 s = 1.0 / (len(G) - 1.0) 

98 centrality = {n: d * s for n, d in G.in_degree()} 

99 return centrality 

100 

101 

102@not_implemented_for("undirected") 

103@nx._dispatch 

104def out_degree_centrality(G): 

105 """Compute the out-degree centrality for nodes. 

106 

107 The out-degree centrality for a node v is the fraction of nodes its 

108 outgoing edges are connected to. 

109 

110 Parameters 

111 ---------- 

112 G : graph 

113 A NetworkX graph 

114 

115 Returns 

116 ------- 

117 nodes : dictionary 

118 Dictionary of nodes with out-degree centrality as values. 

119 

120 Raises 

121 ------ 

122 NetworkXNotImplemented 

123 If G is undirected. 

124 

125 Examples 

126 -------- 

127 >>> G = nx.DiGraph([(0, 1), (0, 2), (0, 3), (1, 2), (1, 3)]) 

128 >>> nx.out_degree_centrality(G) 

129 {0: 1.0, 1: 0.6666666666666666, 2: 0.0, 3: 0.0} 

130 

131 See Also 

132 -------- 

133 degree_centrality, in_degree_centrality 

134 

135 Notes 

136 ----- 

137 The degree centrality values are normalized by dividing by the maximum 

138 possible degree in a simple graph n-1 where n is the number of nodes in G. 

139 

140 For multigraphs or graphs with self loops the maximum degree might 

141 be higher than n-1 and values of degree centrality greater than 1 

142 are possible. 

143 """ 

144 if len(G) <= 1: 

145 return {n: 1 for n in G} 

146 

147 s = 1.0 / (len(G) - 1.0) 

148 centrality = {n: d * s for n, d in G.out_degree()} 

149 return centrality