Skip to content

Commit

Permalink
nx-cugraph: add weakly connected components (PLC needs updated!)
Browse files Browse the repository at this point in the history
  • Loading branch information
eriknw committed Dec 28, 2023
1 parent 8d5bba3 commit 1879f63
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python/nx-cugraph/_nx_cugraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"in_degree_centrality",
"is_connected",
"is_isolate",
"is_weakly_connected",
"isolates",
"k_truss",
"karate_club_graph",
Expand All @@ -85,6 +86,7 @@
"number_connected_components",
"number_of_isolates",
"number_of_selfloops",
"number_weakly_connected_components",
"octahedral_graph",
"out_degree_centrality",
"pagerank",
Expand All @@ -102,6 +104,7 @@
"truncated_tetrahedron_graph",
"turan_graph",
"tutte_graph",
"weakly_connected_components",
"wheel_graph",
# END: functions
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
13 changes: 13 additions & 0 deletions python/nx-cugraph/nx_cugraph/algorithms/components/connected.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)))]
Expand Down Expand Up @@ -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."
Expand Down
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 1879f63

Please sign in to comment.