-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add {all_pairs,single_source}_bellman_ford_path_length
#44
Merged
eriknw
merged 13 commits into
python-graphblas:main
from
eriknw:bellman_ford_path_length
Feb 17, 2023
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c9ed0ab
Add `{all_pairs,single_source}_bellman_ford_path_length`
eriknw caa3e83
Faster (and clearer)
eriknw 343fcb8
Implement `algorithms.bellman_ford_path_lengths` to compute in chunks
eriknw 2ca6b0d
Allow to compute all at once
eriknw 704937e
Ignore diagonals during Bellman-Ford
eriknw 4cbbfe9
Add comment, and use `offdiag` more places.
eriknw a644bf8
Do level BFS for Bellman-Ford when iso-valued (and non-negative)
eriknw aaa19c1
Use `"iso_value"` property more places instead of `A.ss.iso_value`
eriknw b82c8af
Fail fast in these unlikely, but easily detected, cases
eriknw ba337d4
Allow garbage collector to be enabled during benchmarks
eriknw 4370b2d
Automatically choose appropriate chunksize
eriknw 7268d80
Use `nsplits="auto"` in square_clustering (default to 256 MB chunks)
eriknw bbcdd07
Add note about `A.ss.is_iso`
eriknw 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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .dense import * | ||
from .generic import * | ||
from .weighted import * |
100 changes: 100 additions & 0 deletions
100
graphblas_algorithms/algorithms/shortest_paths/weighted.py
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,100 @@ | ||
import numpy as np | ||
from graphblas import Matrix, Vector, binary, monoid, replace, unary | ||
from graphblas.semiring import min_plus | ||
|
||
from ..exceptions import Unbounded | ||
|
||
__all__ = [ | ||
"single_source_bellman_ford_path_length", | ||
"bellman_ford_path_lengths", | ||
] | ||
|
||
|
||
def single_source_bellman_ford_path_length(G, source): | ||
# No need for `is_weighted=` keyword, b/c this is assumed to be weighted (I think) | ||
index = G._key_to_id[source] | ||
A = G._A | ||
if A.dtype == bool: | ||
# Should we upcast e.g. INT8 to INT64 as well? | ||
dtype = int | ||
else: | ||
dtype = A.dtype | ||
n = A.nrows | ||
d = Vector(dtype, n, name="single_source_bellman_ford_path_length") | ||
d[index] = 0 | ||
cur = d.dup(name="cur") | ||
mask = Vector(bool, n, name="mask") | ||
one = unary.one[bool] | ||
for _i in range(n - 1): | ||
# This is a slightly modified Bellman-Ford algorithm. | ||
# `cur` is the current frontier of values that improved in the previous iteration. | ||
# This means that in this iteration we drop values from `cur` that are not better. | ||
cur << min_plus(cur @ A) | ||
|
||
# Mask is True where cur not in d or cur < d | ||
mask << one(cur) | ||
mask(binary.second) << binary.lt(cur & d) | ||
|
||
# Drop values from `cur` that didn't improve | ||
cur(mask.V, replace) << cur | ||
if cur.nvals == 0: | ||
break | ||
# Update `d` with values that improved | ||
d(cur.S) << cur | ||
else: | ||
# Check for negative cycle when for loop completes without breaking | ||
cur << min_plus(cur @ A) | ||
mask << binary.lt(cur & d) | ||
if mask.reduce(monoid.lor): | ||
raise Unbounded("Negative cycle detected.") | ||
return d | ||
|
||
|
||
def bellman_ford_path_lengths(G, nodes=None, *, expand_output=False): | ||
""" | ||
|
||
Parameters | ||
---------- | ||
expand_output : bool, default False | ||
When False, the returned Matrix has one row per node in nodes. | ||
When True, the returned Matrix has the same shape as the input Matrix. | ||
""" | ||
# Same algorithms as in `single_source_bellman_ford_path_length`, but with | ||
# `Cur` as a Matrix with each row corresponding to a source node. | ||
A = G._A | ||
if A.dtype == bool: | ||
dtype = int | ||
else: | ||
dtype = A.dtype | ||
n = A.nrows | ||
if nodes is None: | ||
# TODO: `D = Vector.from_iso_value(0, n, dtype).diag()` | ||
D = Vector(dtype, n, name="bellman_ford_path_lengths_vector") | ||
D << 0 | ||
D = D.diag(name="bellman_ford_path_lengths") | ||
else: | ||
ids = G.list_to_ids(nodes) | ||
D = Matrix.from_coo( | ||
np.arange(len(ids), dtype=np.uint64), ids, 0, dtype, nrows=len(ids), ncols=n | ||
) | ||
Cur = D.dup(name="Cur") | ||
Mask = Matrix(bool, D.nrows, D.ncols, name="Mask") | ||
one = unary.one[bool] | ||
for _i in range(n - 1): | ||
Cur << min_plus(Cur @ A) | ||
Mask << one(Cur) | ||
Mask(binary.second) << binary.lt(Cur & D) | ||
Cur(Mask.V, replace) << Cur | ||
if Cur.nvals == 0: | ||
break | ||
D(Cur.S) << Cur | ||
else: | ||
Cur << min_plus(Cur @ A) | ||
Mask << binary.lt(Cur & D) | ||
if Mask.reduce_scalar(monoid.lor): | ||
raise Unbounded("Negative cycle detected.") | ||
if nodes is not None and expand_output: | ||
rv = Matrix(D.dtype, n, n, name=D.name) | ||
rv[ids, :] = D | ||
return rv | ||
return D |
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
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
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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .dense import * | ||
from .generic import * | ||
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,47 @@ | ||
from graphblas_algorithms import algorithms | ||
from graphblas_algorithms.classes.digraph import to_graph | ||
|
||
from ..exception import NetworkXUnbounded, NodeNotFound | ||
|
||
__all__ = [ | ||
"all_pairs_bellman_ford_path_length", | ||
"single_source_bellman_ford_path_length", | ||
] | ||
|
||
|
||
def all_pairs_bellman_ford_path_length(G, weight="weight", *, chunksize=128): | ||
# Larger chunksize offers more parallelism, but uses more memory. | ||
G = to_graph(G, weight=weight) | ||
if chunksize < 2: | ||
for source in G: | ||
try: | ||
d = algorithms.single_source_bellman_ford_path_length(G, source) | ||
except algorithms.exceptions.Unbounded as e: | ||
raise NetworkXUnbounded(*e.args) from e | ||
yield (source, G.vector_to_nodemap(d)) | ||
else: | ||
# We should probably make a utility function for chunking | ||
nodes = list(G) | ||
for start, stop in zip( | ||
range(0, len(nodes), chunksize), range(chunksize, len(nodes) + chunksize, chunksize) | ||
): | ||
cur_nodes = nodes[start:stop] | ||
try: | ||
D = algorithms.bellman_ford_path_lengths(G, cur_nodes) | ||
except algorithms.exceptions.Unbounded as e: | ||
raise NetworkXUnbounded(*e.args) from e | ||
for i, source in enumerate(cur_nodes): | ||
d = D[i, :].new(name=f"all_pairs_bellman_ford_path_length_{i}") | ||
yield (source, G.vector_to_nodemap(d)) | ||
|
||
|
||
def single_source_bellman_ford_path_length(G, source, weight="weight"): | ||
# TODO: what if weight is a function? | ||
G = to_graph(G, weight=weight) | ||
try: | ||
d = algorithms.single_source_bellman_ford_path_length(G, source) | ||
except algorithms.exceptions.Unbounded as e: | ||
raise NetworkXUnbounded(*e.args) from e | ||
except KeyError as e: | ||
raise NodeNotFound(*e.args) from e | ||
return G.vector_to_nodemap(d) |
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
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're adding a new keyword-only argument to
nxapi
. @jim22k, do you likechunksize
for this?We have a similar extra keyword-argument
nchunks
innxapi.square_clustering
that controls how much memory is used during calculation.Neither one,
nchunks
orchunksize
, changes the result; they're only used to control memory use and performance via available parallelism.