-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathbetweenness.py
171 lines (145 loc) · 5.15 KB
/
betweenness.py
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
from networkx.algorithms.centrality.betweenness import (
_accumulate_basic,
_accumulate_endpoints,
_rescale,
_single_source_dijkstra_path_basic,
_single_source_shortest_path_basic,
_rescale_e,
_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",
**kwargs,
):
"""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
def process_func(G, chunk, weight, endpoints):
return _betweenness_centrality_node_subset(
G, chunk, weight=weight, endpoints=endpoints
)
def iterator_func(G):
if k is None:
return G.nodes
else:
return seed.sample(list(G.nodes), k)
bt_cs = nxp.utils.chunk.execute_parallel(
G,
process_func=process_func,
iterator_func=iterator_func,
get_chunks=get_chunks,
weight=weight,
endpoints=endpoints,
**kwargs,
)
# Reducing partial solution
bt_c = {}
for bt in bt_cs:
for n, value in bt.items():
bt_c[n] = bt_c.get(n, 0.0) + value
betweenness = _rescale(
bt_c,
len(G),
normalized=normalized,
directed=G.is_directed(),
k=k,
endpoints=endpoints,
)
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="nodes",
**kwargs,
):
"""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
def process_func(G, chunk, weight):
return _edge_betweenness_centrality_node_subset(G, chunk, weight=weight)
def iterator_func(G):
if k is None:
return G.nodes
else:
return seed.sample(list(G.nodes), k)
bt_cs = nxp.utils.chunk.execute_parallel(
G,
process_func=process_func,
iterator_func=iterator_func,
get_chunks=get_chunks,
weight=weight,
**kwargs,
)
# Reducing partial solution
bt_c = {}
for partial_bt in bt_cs:
for edge, value in partial_bt.items():
bt_c[edge] = bt_c.get(edge, 0.0) + value
for n in G: # remove nodes to only return edges
del bt_c[n]
betweenness = _rescale_e(bt_c, len(G), normalized=normalized, k=k)
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