diff --git a/python/nx-cugraph/_nx_cugraph/__init__.py b/python/nx-cugraph/_nx_cugraph/__init__.py index d02c9c3e940..045cdd2944a 100644 --- a/python/nx-cugraph/_nx_cugraph/__init__.py +++ b/python/nx-cugraph/_nx_cugraph/__init__.py @@ -70,6 +70,7 @@ "in_degree_centrality", "is_connected", "is_isolate", + "is_weakly_connected", "isolates", "k_truss", "karate_club_graph", @@ -85,6 +86,7 @@ "number_connected_components", "number_of_isolates", "number_of_selfloops", + "number_weakly_connected_components", "octahedral_graph", "out_degree_centrality", "pagerank", @@ -102,6 +104,7 @@ "truncated_tetrahedron_graph", "turan_graph", "tutte_graph", + "weakly_connected_components", "wheel_graph", # END: functions }, diff --git a/python/nx-cugraph/nx_cugraph/algorithms/components/__init__.py b/python/nx-cugraph/nx_cugraph/algorithms/components/__init__.py index 26816ef3692..eddeba4528a 100644 --- a/python/nx-cugraph/nx_cugraph/algorithms/components/__init__.py +++ b/python/nx-cugraph/nx_cugraph/algorithms/components/__init__.py @@ -11,3 +11,4 @@ # See the License for the specific language governing permissions and # limitations under the License. from .connected import * +from .weakly_connected import * diff --git a/python/nx-cugraph/nx_cugraph/algorithms/components/connected.py b/python/nx-cugraph/nx_cugraph/algorithms/components/connected.py index 41f3457d542..36d25ea8471 100644 --- a/python/nx-cugraph/nx_cugraph/algorithms/components/connected.py +++ b/python/nx-cugraph/nx_cugraph/algorithms/components/connected.py @@ -32,6 +32,11 @@ @not_implemented_for("directed") @networkx_algorithm def number_connected_components(G): + G = _to_undirected_graph(G) + return _number_connected_components(G) + + +def _number_connected_components(G): return sum(1 for _ in connected_components(G)) # PREFERRED IMPLEMENTATION, BUT PLC DOES NOT HANDLE ISOLATED VERTICES WELL # G = _to_undirected_graph(G) @@ -60,6 +65,10 @@ def _(G): @networkx_algorithm def connected_components(G): G = _to_undirected_graph(G) + return _connected_components(G) + + +def _connected_components(G): if G.src_indices.size == 0: # TODO: PLC doesn't handle empty graphs (or isolated nodes) gracefully! return [{key} for key in G._nodeiter_to_iter(range(len(G)))] @@ -89,6 +98,10 @@ def connected_components(G): @networkx_algorithm def is_connected(G): G = _to_undirected_graph(G) + return _is_connected(G) + + +def _is_connected(G): if len(G) == 0: raise nx.NetworkXPointlessConcept( "Connectivity is undefined for the null graph." diff --git a/python/nx-cugraph/nx_cugraph/algorithms/components/weakly_connected.py b/python/nx-cugraph/nx_cugraph/algorithms/components/weakly_connected.py new file mode 100644 index 00000000000..2087c43bf69 --- /dev/null +++ b/python/nx-cugraph/nx_cugraph/algorithms/components/weakly_connected.py @@ -0,0 +1,49 @@ +# Copyright (c) 2023, 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 +def weakly_connected_components(G): + G = _to_directed_graph(G) + return _connected_components(G) + + +@not_implemented_for("undirected") +@networkx_algorithm +def number_weakly_connected_components(G): + G = _to_directed_graph(G) + return _number_connected_components(G) + + +@not_implemented_for("undirected") +@networkx_algorithm +def is_weakly_connected(G): + G = _to_directed_graph(G) + return _is_connected(G)