-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathbetweenness.py
More file actions
174 lines (143 loc) · 5.14 KB
/
betweenness.py
File metadata and controls
174 lines (143 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from joblib import Parallel, delayed
from networkx.algorithms.centrality.betweenness import (
_accumulate_basic,
_accumulate_endpoints,
_single_source_dijkstra_path_basic,
_single_source_shortest_path_basic,
_rescale,
_add_edge_keys,
_accumulate_edges,
)
from networkx.utils import py_random_state
import nx_parallel as nxp
__all__ = ["betweenness_centrality", "edge_betweenness_centrality"]
@nxp._configure_if_nx_active()
@py_random_state(5)
def betweenness_centrality(
G,
k=None,
normalized=True,
weight=None,
endpoints=False,
seed=None,
get_chunks="chunks",
):
"""The parallel computation is implemented by dividing the nodes into chunks and
computing betweenness centrality for each chunk concurrently.
networkx.betweenness_centrality : https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.centrality.betweenness_centrality.html
Parameters
----------
get_chunks : str, function (default = "chunks")
A function that takes in a list of all the nodes as input and returns an
iterable `node_chunks`. The default chunking is done by slicing the
`nodes` into `n_jobs` number of chunks.
"""
if hasattr(G, "graph_object"):
G = G.graph_object
if not G:
return {}
if k == len(G):
k = None
if k is None:
nodes = G.nodes
else:
nodes = seed.sample(list(G.nodes), k)
n_jobs = nxp.get_n_jobs()
if get_chunks == "chunks":
node_chunks = nxp.create_iterables(G, "node", n_jobs, nodes)
else:
node_chunks = get_chunks(nodes)
bt_cs = Parallel()(
delayed(_betweenness_centrality_node_subset)(G, chunk, weight, endpoints)
for chunk in node_chunks
)
# Reducing partial solution
bt_c = bt_cs[0]
for bt in bt_cs[1:]:
for n in bt:
bt_c[n] += bt[n]
betweenness = _rescale(
bt_c,
len(G),
normalized=normalized,
directed=G.is_directed(),
endpoints=endpoints,
sampled_nodes=None if k is None else nodes,
)
return betweenness
def _betweenness_centrality_node_subset(G, nodes, weight=None, endpoints=False):
betweenness = dict.fromkeys(G, 0.0)
for s in nodes:
# single source shortest paths
if weight is None: # use BFS
S, P, sigma, _ = _single_source_shortest_path_basic(G, s)
else: # use Dijkstra's algorithm
S, P, sigma, _ = _single_source_dijkstra_path_basic(G, s, weight)
# accumulation
if endpoints:
betweenness, delta = _accumulate_endpoints(betweenness, S, P, sigma, s)
else:
betweenness, delta = _accumulate_basic(betweenness, S, P, sigma, s)
return betweenness
@nxp._configure_if_nx_active()
@py_random_state(4)
def edge_betweenness_centrality(
G, k=None, normalized=True, weight=None, seed=None, get_chunks="chunks"
):
"""The parallel computation is implemented by dividing the nodes into chunks and
computing edge betweenness centrality for each chunk concurrently.
networkx.edge_betweenness_centrality : https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.centrality.edge_betweenness_centrality.html
Parameters
----------
get_chunks : str, function (default = "chunks")
A function that takes in a list of all the nodes as input and returns an
iterable `node_chunks`. The default chunking is done by slicing the
`nodes` into `n_jobs` number of chunks.
"""
if hasattr(G, "graph_object"):
G = G.graph_object
if not G:
return {}
if k is None:
nodes = G.nodes
else:
nodes = seed.sample(list(G.nodes), k)
n_jobs = nxp.get_n_jobs()
if get_chunks == "chunks":
node_chunks = nxp.create_iterables(G, "node", n_jobs, nodes)
else:
node_chunks = get_chunks(nodes)
bt_cs = Parallel()(
delayed(_edge_betweenness_centrality_node_subset)(G, chunk, weight)
for chunk in node_chunks
)
# Reducing partial solution
bt_c = bt_cs[0]
for bt in bt_cs[1:]:
for e in bt:
bt_c[e] += bt[e]
for n in G: # remove nodes to only return edges
del bt_c[n]
betweenness = _rescale(
bt_c,
len(G),
normalized=normalized,
directed=G.is_directed(),
sampled_nodes=None if k is None else nodes,
)
if G.is_multigraph():
betweenness = _add_edge_keys(G, betweenness, weight=weight)
return betweenness
def _edge_betweenness_centrality_node_subset(G, nodes, weight=None):
betweenness = dict.fromkeys(G, 0.0) # b[v]=0 for v in G
# b[e]=0 for e in G.edges()
betweenness.update(dict.fromkeys(G.edges(), 0.0))
for s in nodes:
# single source shortest paths
if weight is None: # use BFS
S, P, sigma, _ = _single_source_shortest_path_basic(G, s)
else: # use Dijkstra's algorithm
S, P, sigma, _ = _single_source_dijkstra_path_basic(G, s, weight)
# accumulation
betweenness = _accumulate_edges(betweenness, S, P, sigma, s)
return betweenness