-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
weighted.py
301 lines (238 loc) · 10.5 KB
/
weighted.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
"""
Shortest path parallel algorithms for weighted graphs.
"""
from joblib import Parallel, delayed
import nx_parallel as nxp
from networkx.algorithms.shortest_paths.weighted import (
single_source_dijkstra,
single_source_dijkstra_path_length,
single_source_dijkstra_path,
single_source_bellman_ford_path,
single_source_bellman_ford_path_length,
_weight_function,
_dijkstra,
_bellman_ford,
)
__all__ = [
"all_pairs_dijkstra",
"all_pairs_dijkstra_path_length",
"all_pairs_dijkstra_path",
"all_pairs_bellman_ford_path_length",
"all_pairs_bellman_ford_path",
"johnson",
]
@nxp._configure_if_nx_active()
def all_pairs_dijkstra(G, cutoff=None, weight="weight", get_chunks="chunks"):
"""The parallel implementation first divides the nodes into chunks and then
creates a generator to lazily compute shortest paths and lengths for each
`node_chunk`, and then employs joblib's `Parallel` function to execute these
computations in parallel across `n_jobs` number of CPU cores.
networkx.all_pairs_dijkstra : https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.weighted.all_pairs_dijkstra.html#all-pairs-dijkstra
Parameters
----------
get_chunks : str, function (default = "chunks")
A function that takes in an iterable of all the nodes as input and returns
an iterable `node_chunks`. The default chunking is done by slicing the
`G.nodes` into `n_jobs` number of chunks.
"""
def _process_node_chunk(node_chunk):
return [
(node, (single_source_dijkstra(G, node, cutoff=cutoff, weight=weight)))
for node in node_chunk
]
if hasattr(G, "graph_object"):
G = G.graph_object
nodes = G.nodes
n_jobs = nxp.get_n_jobs()
if get_chunks == "chunks":
num_in_chunk = max(len(nodes) // n_jobs, 1)
node_chunks = nxp.chunks(nodes, num_in_chunk)
else:
node_chunks = get_chunks(nodes)
paths_chunk_generator = (
delayed(_process_node_chunk)(node_chunk) for node_chunk in node_chunks
)
for path_chunk in Parallel()(paths_chunk_generator):
for path in path_chunk:
yield path
@nxp._configure_if_nx_active()
def all_pairs_dijkstra_path_length(
G, cutoff=None, weight="weight", get_chunks="chunks"
):
"""The parallel implementation first divides the nodes into chunks and then
creates a generator to lazily compute shortest paths lengths for each node in
`node_chunk`, and then employs joblib's `Parallel` function to execute these
computations in parallel across `n_jobs` number of CPU cores.
networkx.all_pairs_dijkstra_path_length : https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.weighted.all_pairs_dijkstra_path_length.html
Parameters
----------
get_chunks : str, function (default = "chunks")
A function that takes in an iterable of all the nodes as input and returns
an iterable `node_chunks`. The default chunking is done by slicing the
`G.nodes` into `n_jobs` number of chunks.
"""
def _process_node_chunk(node_chunk):
return [
(
node,
single_source_dijkstra_path_length(
G, node, cutoff=cutoff, weight=weight
),
)
for node in node_chunk
]
if hasattr(G, "graph_object"):
G = G.graph_object
nodes = G.nodes
n_jobs = nxp.get_n_jobs()
if get_chunks == "chunks":
num_in_chunk = max(len(nodes) // n_jobs, 1)
node_chunks = nxp.chunks(nodes, num_in_chunk)
else:
node_chunks = get_chunks(nodes)
paths_chunk_generator = (
delayed(_process_node_chunk)(node_chunk) for node_chunk in node_chunks
)
for path_chunk in Parallel()(paths_chunk_generator):
for path in path_chunk:
yield path
@nxp._configure_if_nx_active()
def all_pairs_dijkstra_path(G, cutoff=None, weight="weight", get_chunks="chunks"):
"""The parallel implementation first divides the nodes into chunks and then
creates a generator to lazily compute shortest paths for each `node_chunk`, and
then employs joblib's `Parallel` function to execute these computations in
parallel across `n_jobs` number of CPU cores.
networkx.all_pairs_dijkstra_path : https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.weighted.all_pairs_dijkstra_path.html
Parameters
----------
get_chunks : str, function (default = "chunks")
A function that takes in an iterable of all the nodes as input and returns
an iterable `node_chunks`. The default chunking is done by slicing the
`G.nodes` into `n_jobs` number of chunks.
"""
def _process_node_chunk(node_chunk):
return [
(node, single_source_dijkstra_path(G, node, cutoff=cutoff, weight=weight))
for node in node_chunk
]
if hasattr(G, "graph_object"):
G = G.graph_object
nodes = G.nodes
n_jobs = nxp.get_n_jobs()
if get_chunks == "chunks":
num_in_chunk = max(len(nodes) // n_jobs, 1)
node_chunks = nxp.chunks(nodes, num_in_chunk)
else:
node_chunks = get_chunks(nodes)
paths_chunk_generator = (
delayed(_process_node_chunk)(node_chunk) for node_chunk in node_chunks
)
for path_chunk in Parallel()(paths_chunk_generator):
for path in path_chunk:
yield path
@nxp._configure_if_nx_active()
def all_pairs_bellman_ford_path_length(G, weight="weight", get_chunks="chunks"):
"""The parallel implementation first divides the nodes into chunks and then
creates a generator to lazily compute shortest paths lengths for each node in
`node_chunk`, and then employs joblib's `Parallel` function to execute these
computations in parallel across `n_jobs` number of CPU cores.
networkx.all_pairs_bellman_ford_path_length : https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.weighted.all_pairs_bellman_ford_path_length.html
Parameters
----------
get_chunks : str, function (default = "chunks")
A function that takes in an iterable of all the nodes as input and returns
an iterable `node_chunks`. The default chunking is done by slicing the
`G.nodes` into `n_jobs` number of chunks.
"""
def _process_node_chunk(node_chunk):
return [
(node, single_source_bellman_ford_path_length(G, node, weight=weight))
for node in node_chunk
]
if hasattr(G, "graph_object"):
G = G.graph_object
nodes = G.nodes
n_jobs = nxp.get_n_jobs()
if get_chunks == "chunks":
num_in_chunk = max(len(nodes) // n_jobs, 1)
node_chunks = nxp.chunks(nodes, num_in_chunk)
else:
node_chunks = get_chunks(nodes)
path_lengths_chunk_generator = (
delayed(_process_node_chunk)(node_chunk) for node_chunk in node_chunks
)
for path_length_chunk in Parallel()(path_lengths_chunk_generator):
for path_length in path_length_chunk:
yield path_length
@nxp._configure_if_nx_active()
def all_pairs_bellman_ford_path(G, weight="weight", get_chunks="chunks"):
"""The parallel implementation first divides the nodes into chunks and then
creates a generator to lazily compute shortest paths for each node_chunk, and
then employs joblib's `Parallel` function to execute these computations in
parallel across `n_jobs` number of CPU cores.
networkx.all_pairs_bellman_ford_path : https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.weighted.all_pairs_bellman_ford_path.html
Parameters
----------
get_chunks : str, function (default = "chunks")
A function that takes in an iterable of all the nodes as input and returns
an iterable `node_chunks`. The default chunking is done by slicing the
`G.nodes` into `n_jobs` number of chunks.
"""
def _process_node_chunk(node_chunk):
return [
(node, single_source_bellman_ford_path(G, node, weight=weight))
for node in node_chunk
]
if hasattr(G, "graph_object"):
G = G.graph_object
nodes = G.nodes
n_jobs = nxp.get_n_jobs()
if get_chunks == "chunks":
num_in_chunk = max(len(nodes) // n_jobs, 1)
node_chunks = nxp.chunks(nodes, num_in_chunk)
else:
node_chunks = get_chunks(nodes)
paths_chunk_generator = (
delayed(_process_node_chunk)(node_chunk) for node_chunk in node_chunks
)
for path_chunk in Parallel()(paths_chunk_generator):
for path in path_chunk:
yield path
@nxp._configure_if_nx_active()
def johnson(G, weight="weight", get_chunks="chunks"):
"""The parallel computation is implemented by dividing the
nodes into chunks and computing the shortest paths using Johnson's Algorithm
for each chunk in parallel.
networkx.johnson : https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.weighted.johnson.html
Parameters
----------
get_chunks : str, function (default = "chunks")
A function that takes in an iterable of all the nodes as input and returns
an iterable `node_chunks`. The default chunking is done by slicing the
`G.nodes` into `n_jobs` number of chunks.
"""
if hasattr(G, "graph_object"):
G = G.graph_object
dist = {v: 0 for v in G}
pred = {v: [] for v in G}
weight = _weight_function(G, weight)
# Calculate distance of shortest paths
dist_bellman = _bellman_ford(G, list(G), weight, pred=pred, dist=dist)
# Update the weight function to take into account the Bellman--Ford
# relaxation distances.
def new_weight(u, v, d):
return weight(u, v, d) + dist_bellman[u] - dist_bellman[v]
def dist_path(v):
paths = {v: [v]}
_dijkstra(G, v, new_weight, paths=paths)
return paths
def _johnson_subset(chunk):
return {node: dist_path(node) for node in chunk}
n_jobs = nxp.get_n_jobs()
if get_chunks == "chunks":
num_in_chunk = max(len(G.nodes) // n_jobs, 1)
node_chunks = nxp.chunks(G.nodes, num_in_chunk)
else:
node_chunks = get_chunks(G.nodes)
results = Parallel()(delayed(_johnson_subset)(chunk) for chunk in node_chunks)
return {v: d_path for result_chunk in results for v, d_path in result_chunk.items()}