-
Notifications
You must be signed in to change notification settings - Fork 309
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
nx-cugraph: add weakly connected components #4071
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1879f63
nx-cugraph: add weakly connected components (PLC needs updated!)
eriknw c1d1a40
Update WCC to symmetrize before creating PLC graph
eriknw bdf66dc
Merge branch 'branch-24.02' into wcc
eriknw be2eb80
Merge branch 'branch-24.02' into wcc
eriknw 3754094
Add version_added to wcc algos
eriknw 9f3cd27
Merge branch 'branch-24.02' into wcc
eriknw d2d61e5
Add strongly connected components (via legacy API)
eriknw 213610b
Merge branch 'branch-24.02' into wcc
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
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
85 changes: 85 additions & 0 deletions
85
python/nx-cugraph/nx_cugraph/algorithms/components/strongly_connected.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,85 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import cupy as cp | ||
import networkx as nx | ||
import pylibcugraph as plc | ||
|
||
from nx_cugraph.convert import _to_directed_graph | ||
from nx_cugraph.utils import ( | ||
_groupby, | ||
index_dtype, | ||
networkx_algorithm, | ||
not_implemented_for, | ||
) | ||
|
||
__all__ = [ | ||
"number_strongly_connected_components", | ||
"strongly_connected_components", | ||
"is_strongly_connected", | ||
] | ||
|
||
|
||
def _strongly_connected_components(G): | ||
# TODO: create utility function to convert just the indices to CSR | ||
# TODO: this uses a legacy PLC function (strongly_connected_components) | ||
N = len(G) | ||
indices = cp.lexsort(cp.vstack((G.dst_indices, G.src_indices))) | ||
dst_indices = G.dst_indices[indices] | ||
offsets = cp.searchsorted( | ||
G.src_indices, cp.arange(N + 1, dtype=index_dtype), sorter=indices | ||
).astype(index_dtype) | ||
labels = cp.zeros(N, dtype=index_dtype) | ||
plc.strongly_connected_components( | ||
offsets=offsets, | ||
indices=dst_indices, | ||
weights=None, | ||
num_verts=N, | ||
num_edges=dst_indices.size, | ||
labels=labels, | ||
) | ||
return labels | ||
|
||
|
||
@not_implemented_for("undirected") | ||
@networkx_algorithm(version_added="24.02", plc="strongly_connected_components") | ||
def strongly_connected_components(G): | ||
G = _to_directed_graph(G) | ||
if G.src_indices.size == 0: | ||
return [{key} for key in G._nodeiter_to_iter(range(len(G)))] | ||
labels = _strongly_connected_components(G) | ||
groups = _groupby(labels, cp.arange(len(G), dtype=index_dtype)) | ||
return (G._nodearray_to_set(connected_ids) for connected_ids in groups.values()) | ||
|
||
|
||
@not_implemented_for("undirected") | ||
@networkx_algorithm(version_added="24.02", plc="strongly_connected_components") | ||
def number_strongly_connected_components(G): | ||
G = _to_directed_graph(G) | ||
if G.src_indices.size == 0: | ||
return len(G) | ||
labels = _strongly_connected_components(G) | ||
return cp.unique(labels).size | ||
|
||
|
||
@not_implemented_for("undirected") | ||
@networkx_algorithm(version_added="24.02", plc="strongly_connected_components") | ||
def is_strongly_connected(G): | ||
G = _to_directed_graph(G) | ||
if len(G) == 0: | ||
raise nx.NetworkXPointlessConcept( | ||
"Connectivity is undefined for the null graph." | ||
) | ||
if G.src_indices.size == 0: | ||
return len(G) == 1 | ||
labels = _strongly_connected_components(G) | ||
return bool((labels == labels[0]).all()) |
47 changes: 47 additions & 0 deletions
47
python/nx-cugraph/nx_cugraph/algorithms/components/weakly_connected.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,47 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from nx_cugraph.convert import _to_directed_graph | ||
from nx_cugraph.utils import networkx_algorithm, not_implemented_for | ||
|
||
from .connected import ( | ||
_connected_components, | ||
_is_connected, | ||
_number_connected_components, | ||
) | ||
|
||
__all__ = [ | ||
"number_weakly_connected_components", | ||
"weakly_connected_components", | ||
"is_weakly_connected", | ||
] | ||
|
||
|
||
@not_implemented_for("undirected") | ||
@networkx_algorithm(plc="weakly_connected_components", version_added="24.02") | ||
def weakly_connected_components(G): | ||
G = _to_directed_graph(G) | ||
return _connected_components(G, symmetrize="union") | ||
|
||
|
||
@not_implemented_for("undirected") | ||
@networkx_algorithm(plc="weakly_connected_components", version_added="24.02") | ||
def number_weakly_connected_components(G): | ||
G = _to_directed_graph(G) | ||
return _number_connected_components(G, symmetrize="union") | ||
|
||
|
||
@not_implemented_for("undirected") | ||
@networkx_algorithm(plc="weakly_connected_components", version_added="24.02") | ||
def is_weakly_connected(G): | ||
G = _to_directed_graph(G) | ||
return _is_connected(G, symmetrize="union") |
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
Oops, something went wrong.
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.
just curious, no action needed: is this expected to always return 0 in this case? If so, is there a reason calling
len()
is preferred over just returning 0?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.
Maybe I should use
G.number_of_edges()
instead ofG.src_indices.size
(but for some reason the latter is easier for me to remember and reason about). Anyway, if the number of edges are zero, the the number of components is the number of nodes, hence we can't simply return 0.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.
I may update to use
number_of_edges
lots of places for clarity in a different PR. I agree this shouldn't hold up this PR.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.
Oh I see,
number_of_edges
actually does a lot more work. If we want to know if there are exactly 0 edges,G.src_indices.size
works great.