Skip to content
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: adds ancestors, descendants, and BFS algos #4029

Merged
merged 10 commits into from
Dec 21, 2023
14 changes: 14 additions & 0 deletions python/nx-cugraph/_nx_cugraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@
# "description": "TODO",
"functions": {
# BEGIN: functions
"ancestors",
"barbell_graph",
"betweenness_centrality",
"bfs_edges",
"bfs_layers",
"bfs_predecessors",
"bfs_successors",
"bfs_tree",
"bull_graph",
"caveman_graph",
"chvatal_graph",
Expand All @@ -44,6 +50,8 @@
"davis_southern_women_graph",
"degree_centrality",
"desargues_graph",
"descendants",
"descendants_at_distance",
"diamond_graph",
"dodecahedral_graph",
"edge_betweenness_centrality",
Expand All @@ -53,6 +61,7 @@
"from_pandas_edgelist",
"from_scipy_sparse_array",
"frucht_graph",
"generic_bfs_edges",
"heawood_graph",
"hits",
"house_graph",
Expand Down Expand Up @@ -99,9 +108,14 @@
"extra_docstrings": {
# BEGIN: extra_docstrings
"betweenness_centrality": "`weight` parameter is not yet supported.",
"bfs_edges": "`sort_neighbors` parameter is not yet supported.",
"bfs_predecessors": "`sort_neighbors` parameter is not yet supported.",
"bfs_successors": "`sort_neighbors` parameter is not yet supported.",
"bfs_tree": "`sort_neighbors` parameter is not yet supported.",
"edge_betweenness_centrality": "`weight` parameter is not yet supported.",
"eigenvector_centrality": "`nstart` parameter is not used, but it is checked for validity.",
"from_pandas_edgelist": "cudf.DataFrame inputs also supported.",
"generic_bfs_edges": "`neighbors` parameter is not yet supported.",
"k_truss": (
"Currently raises `NotImplementedError` for graphs with more than one connected\n"
"component when k >= 3. We expect to fix this soon."
Expand Down
7 changes: 5 additions & 2 deletions python/nx-cugraph/nx_cugraph/algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
centrality,
community,
components,
shortest_paths,
link_analysis,
shortest_paths,
traversal,
)
from .bipartite import complete_bipartite_graph
from .centrality import *
from .components import *
from .core import *
from .dag import *
from .isolate import *
from .shortest_paths import *
from .link_analysis import *
from .shortest_paths import *
from .traversal import *
6 changes: 5 additions & 1 deletion python/nx-cugraph/nx_cugraph/algorithms/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ def k_truss(G, k):
if is_nx := isinstance(G, nx.Graph):
G = nxcg.from_networkx(G, preserve_all_attrs=True)
if nxcg.number_of_selfloops(G) > 0:
raise nx.NetworkXError(
if nx.__version__[:3] <= "3.2":
exc_class = nx.NetworkXError
else:
exc_class = nx.NetworkXNotImplemented
raise exc_class(
"Input graph has self loops which is not permitted; "
"Consider using G.remove_edges_from(nx.selfloop_edges(G))."
)
Expand Down
55 changes: 55 additions & 0 deletions python/nx-cugraph/nx_cugraph/algorithms/dag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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.
import cupy as cp
import networkx as nx
import numpy as np
import pylibcugraph as plc

from nx_cugraph.convert import _to_graph
from nx_cugraph.utils import index_dtype, networkx_algorithm

__all__ = [
"descendants",
"ancestors",
]


def _ancestors_and_descendants(G, source, *, is_ancestors):
G = _to_graph(G)
if source not in G:
hash(source) # To raise TypeError if appropriate
rlratzel marked this conversation as resolved.
Show resolved Hide resolved
raise nx.NetworkXError(
f"The node {source} is not in the {G.__class__.__name__.lower()}."
)
src_index = source if G.key_to_id is None else G.key_to_id[source]
distances, predecessors, node_ids = plc.bfs(
handle=plc.ResourceHandle(),
graph=G._get_plc_graph(switch_indices=is_ancestors),
sources=cp.array([src_index], dtype=index_dtype),
direction_optimizing=False,
depth_limit=-1,
compute_predecessors=False,
do_expensive_check=False,
)
mask = (distances != np.iinfo(distances.dtype).max) & (distances != 0)
return G._nodearray_to_set(node_ids[mask])


@networkx_algorithm
def descendants(G, source):
return _ancestors_and_descendants(G, source, is_ancestors=False)


@networkx_algorithm
def ancestors(G, source):
return _ancestors_and_descendants(G, source, is_ancestors=True)
13 changes: 13 additions & 0 deletions python/nx-cugraph/nx_cugraph/algorithms/traversal/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 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 .breadth_first_search import *
Loading
Loading