Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/networkx/algorithms/centrality/degree_alg.py: 44%

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

27 statements  

1"""Degree centrality measures.""" 

2 

3import networkx as nx 

4from networkx.utils.decorators import not_implemented_for 

5 

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

7 

8 

9@nx._dispatchable 

10def degree_centrality(G): 

11 """Compute the degree centrality for nodes. 

12 

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

14 is connected to. 

15 

16 Parameters 

17 ---------- 

18 G : graph 

19 A networkx graph 

20 

21 Returns 

22 ------- 

23 nodes : dictionary 

24 Dictionary of nodes with degree centrality as the value. 

25 

26 Examples 

27 -------- 

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

29 >>> nx.degree_centrality(G) 

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

31 

32 See Also 

33 -------- 

34 betweenness_centrality, load_centrality, eigenvector_centrality 

35 

36 Notes 

37 ----- 

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

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

40 

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

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

43 are possible. 

44 """ 

45 if len(G) <= 1: 

46 return {n: 1 for n in G} 

47 

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

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

50 return centrality 

51 

52 

53@not_implemented_for("undirected") 

54@nx._dispatchable 

55def in_degree_centrality(G): 

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

57 

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

59 incoming edges are connected to. 

60 

61 Parameters 

62 ---------- 

63 G : graph 

64 A NetworkX graph 

65 

66 Returns 

67 ------- 

68 nodes : dictionary 

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

70 

71 Raises 

72 ------ 

73 NetworkXNotImplemented 

74 If G is undirected. 

75 

76 Examples 

77 -------- 

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

79 >>> nx.in_degree_centrality(G) 

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

81 

82 See Also 

83 -------- 

84 degree_centrality, out_degree_centrality 

85 

86 Notes 

87 ----- 

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

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

90 

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

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

93 are possible. 

94 """ 

95 if len(G) <= 1: 

96 return {n: 1 for n in G} 

97 

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

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

100 return centrality 

101 

102 

103@not_implemented_for("undirected") 

104@nx._dispatchable 

105def out_degree_centrality(G): 

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

107 

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

109 outgoing edges are connected to. 

110 

111 Parameters 

112 ---------- 

113 G : graph 

114 A NetworkX graph 

115 

116 Returns 

117 ------- 

118 nodes : dictionary 

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

120 

121 Raises 

122 ------ 

123 NetworkXNotImplemented 

124 If G is undirected. 

125 

126 Examples 

127 -------- 

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

129 >>> nx.out_degree_centrality(G) 

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

131 

132 See Also 

133 -------- 

134 degree_centrality, in_degree_centrality 

135 

136 Notes 

137 ----- 

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

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

140 

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

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

143 are possible. 

144 """ 

145 if len(G) <= 1: 

146 return {n: 1 for n in G} 

147 

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

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

150 return centrality