- Notifications
You must be signed in to change notification settings - Fork5
Add a few more BFS-based algorithms#51
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
c33df8c Add a few more BFS-based algorithms
eriknwf9b5b14 Fix `partition(evently=True)`
eriknw5949ff2 update packages list in pyproject.toml
eriknwcef138a Bump ruff
eriknw4742675 Remove autoflake and flake8-comprehensions (use ruff instead)
eriknw806911d bump
eriknw029a639 bump
eriknwFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
33 changes: 25 additions & 8 deletions.pre-commit-config.yaml
This file contains hidden or 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
15 changes: 13 additions & 2 deletionsREADME.md
This file contains hidden or 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
2 changes: 2 additions & 0 deletionsgraphblas_algorithms/algorithms/__init__.py
This file contains hidden or 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
2 changes: 1 addition & 1 deletiongraphblas_algorithms/algorithms/centrality/eigenvector.py
This file contains hidden or 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
2 changes: 1 addition & 1 deletiongraphblas_algorithms/algorithms/centrality/katz.py
This file contains hidden or 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
2 changes: 2 additions & 0 deletionsgraphblas_algorithms/algorithms/components/__init__.py
This file contains hidden or 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,2 @@ | ||
| from .connected import * | ||
| from .weakly_connected import * |
31 changes: 31 additions & 0 deletionsgraphblas_algorithms/algorithms/components/connected.py
This file contains hidden or 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,31 @@ | ||
| from graphblas import Vector, replace | ||
| from graphblas.semiring import any_pair | ||
| from graphblas_algorithms.algorithms.exceptions import PointlessConcept | ||
| def is_connected(G): | ||
| if len(G) == 0: | ||
| raise PointlessConcept("Connectivity is undefined for the null graph.") | ||
| return _plain_bfs(G, next(iter(G))).nvals == len(G) | ||
| def node_connected_component(G, n): | ||
| return _plain_bfs(G, n) | ||
| def _plain_bfs(G, source): | ||
| index = G._key_to_id[source] | ||
| A = G.get_property("offdiag") | ||
| n = A.nrows | ||
| v = Vector(bool, n, name="bfs_plain") | ||
| q = Vector(bool, n, name="q") | ||
| v[index] = True | ||
| q[index] = True | ||
| any_pair_bool = any_pair[bool] | ||
| for _i in range(1, n): | ||
| q(~v.S, replace) << any_pair_bool(q @ A) | ||
| if q.nvals == 0: | ||
| break | ||
| v(q.S) << True | ||
| return v |
77 changes: 77 additions & 0 deletionsgraphblas_algorithms/algorithms/components/weakly_connected.py
This file contains hidden or 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,77 @@ | ||
| from graphblas import Vector, binary, replace | ||
| from graphblas.semiring import any_pair | ||
| from graphblas_algorithms.algorithms.exceptions import PointlessConcept | ||
| def is_weakly_connected(G): | ||
| if len(G) == 0: | ||
| raise PointlessConcept("Connectivity is undefined for the null graph.") | ||
| return _plain_bfs(G, next(iter(G))).nvals == len(G) | ||
| # TODO: benchmark this and the version commented out below | ||
| def _plain_bfs(G, source): | ||
| # Bi-directional BFS w/o symmetrizing the adjacency matrix | ||
| index = G._key_to_id[source] | ||
| A = G.get_property("offdiag") | ||
| # XXX: should we use `AT` if available? | ||
| n = A.nrows | ||
| v = Vector(bool, n, name="bfs_plain") | ||
| q_out = Vector(bool, n, name="q_out") | ||
| q_in = Vector(bool, n, name="q_in") | ||
| v[index] = True | ||
| q_in[index] = True | ||
| any_pair_bool = any_pair[bool] | ||
| is_out_empty = True | ||
| is_in_empty = False | ||
| for _i in range(1, n): | ||
| # Traverse out-edges from the most recent `q_in` and `q_out` | ||
| if is_out_empty: | ||
| q_out(~v.S) << any_pair_bool(q_in @ A) | ||
| else: | ||
| q_out << binary.any(q_out | q_in) | ||
| q_out(~v.S, replace) << any_pair_bool(q_out @ A) | ||
| is_out_empty = q_out.nvals == 0 | ||
| if not is_out_empty: | ||
| v(q_out.S) << True | ||
| elif is_in_empty: | ||
| break | ||
| # Traverse in-edges from the most recent `q_in` and `q_out` | ||
| if is_in_empty: | ||
| q_in(~v.S) << any_pair_bool(A @ q_out) | ||
| else: | ||
| q_in << binary.any(q_out | q_in) | ||
| q_in(~v.S, replace) << any_pair_bool(A @ q_in) | ||
| is_in_empty = q_in.nvals == 0 | ||
| if not is_in_empty: | ||
| v(q_in.S) << True | ||
| elif is_out_empty: | ||
| break | ||
| return v | ||
| """ | ||
| def _plain_bfs(G, source): | ||
| # Bi-directional BFS w/o symmetrizing the adjacency matrix | ||
| index = G._key_to_id[source] | ||
| A = G.get_property("offdiag") | ||
| n = A.nrows | ||
| v = Vector(bool, n, name="bfs_plain") | ||
| q = Vector(bool, n, name="q") | ||
| q2 = Vector(bool, n, name="q_2") | ||
| v[index] = True | ||
| q[index] = True | ||
| any_pair_bool = any_pair[bool] | ||
| for _i in range(1, n): | ||
| q2(~v.S, replace) << any_pair_bool(q @ A) | ||
| v(q2.S) << True | ||
| q(~v.S, replace) << any_pair_bool(A @ q) | ||
| if q.nvals == 0: | ||
| if q2.nvals == 0: | ||
| break | ||
| q, q2 = q2, q | ||
| elif q2.nvals != 0: | ||
| q << binary.any(q | q2) | ||
| return v | ||
| """ |
18 changes: 11 additions & 7 deletionsgraphblas_algorithms/algorithms/dag.py
This file contains hidden or 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
4 changes: 2 additions & 2 deletionsgraphblas_algorithms/algorithms/dominating.py
This file contains hidden or 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,8 +1,8 @@ | ||
| from graphblas.semiring importany_pair | ||
| __all__ = ["is_dominating_set"] | ||
| def is_dominating_set(G, nbunch): | ||
| nbrs =any_pair[bool](nbunch @ G._A).new(mask=~nbunch.S) # A or A.T? | ||
| return nbrs.size - nbunch.nvals - nbrs.nvals == 0 |
4 changes: 4 additions & 0 deletionsgraphblas_algorithms/algorithms/exceptions.py
This file contains hidden or 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
4 changes: 2 additions & 2 deletionsgraphblas_algorithms/algorithms/link_analysis/hits_alg.py
This file contains hidden or 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
2 changes: 1 addition & 1 deletiongraphblas_algorithms/algorithms/link_analysis/pagerank_alg.py
This file contains hidden or 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
1 change: 1 addition & 0 deletionsgraphblas_algorithms/algorithms/shortest_paths/__init__.py
This file contains hidden or 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,3 +1,4 @@ | ||
| from .dense import * | ||
| from .generic import * | ||
| from .unweighted import * | ||
| from .weighted import * |
23 changes: 13 additions & 10 deletionsgraphblas_algorithms/algorithms/shortest_paths/generic.py
This file contains hidden or 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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.