-
-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
parallel implementation for all_pairs_bellman_ford_path #14
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8e48408
parallel implementation for all_pairs_bellman_ford_path
Schefflera-Arboricola 614ada3
Merge branch 'networkx:main' into main
Schefflera-Arboricola a79de33
changed return type from dict to generator
Schefflera-Arboricola f8f512f
updated all_pairs_bellman_ford_path and timing_individual_function.py
Schefflera-Arboricola 6c5e0d4
added examples to all_pairs_bellman_ford_path()
Schefflera-Arboricola 2abd434
applied black and modified doc in weighted.py
Schefflera-Arboricola aaa36b7
modified doc in weighted.py
Schefflera-Arboricola File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .weighted import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from joblib import Parallel, delayed | ||
from networkx.algorithms.shortest_paths.weighted import ( | ||
single_source_bellman_ford_path | ||
) | ||
|
||
import nx_parallel as nxp | ||
|
||
__all__ = ["all_pairs_bellman_ford_path"] | ||
|
||
|
||
def all_pairs_bellman_ford_path(G, weight="weight"): | ||
"""Compute shortest paths between all nodes in a weighted graph. | ||
|
||
Parameters | ||
---------- | ||
G : NetworkX graph | ||
|
||
weight : string or function (default="weight") | ||
If this is a string, then edge weights will be accessed via the | ||
edge attribute with this key (that is, the weight of the edge | ||
joining `u` to `v` will be ``G.edges[u, v][weight]``). If no | ||
such edge attribute exists, the weight of the edge is assumed to | ||
be one. | ||
|
||
If this is a function, the weight of an edge is the value | ||
returned by the function. The function must accept exactly three | ||
positional arguments: the two endpoints of an edge and the | ||
dictionary of edge attributes for that edge. The function must | ||
return a number. | ||
|
||
Returns | ||
------- | ||
all_paths : | ||
dictionary keyed by source with value as another dictionary keyed | ||
by target and shortest path as its key value. | ||
|
||
Notes | ||
----- | ||
Edge weight attributes must be numerical. | ||
Distances are calculated as sums of weighted edges traversed. | ||
|
||
""" | ||
if hasattr(G, "graph_object"): | ||
G = G.graph_object | ||
|
||
nodes = G.nodes | ||
|
||
total_cores = nxp.cpu_count() | ||
|
||
num_in_chunk = max(len(nodes) // total_cores, 1) | ||
node_chunks = nxp.chunks(nodes, num_in_chunk) | ||
|
||
paths_list = Parallel(n_jobs=total_cores)(delayed(_calculate_shortest_paths_subset)(G, chunk, weight) for chunk in node_chunks) | ||
|
||
|
||
all_paths = {} | ||
for result in paths_list: | ||
for source, paths in result.items(): | ||
all_paths[source]=paths | ||
return all_paths | ||
|
||
|
||
# Helper function | ||
def _calculate_shortest_paths_subset(G, chunk, weight): | ||
result = {} | ||
for source in chunk: | ||
paths = single_source_bellman_ford_path(G, source, weight=weight) | ||
result[source] = paths | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be moving this to the function signature and match something like what scikit-learn does (
n_jobs
) but not a blocker here.