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: add complement and reverse #4103

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions python/nx-cugraph/_nx_cugraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"chvatal_graph",
"circular_ladder_graph",
"clustering",
"complement",
"complete_bipartite_graph",
"complete_graph",
"complete_multipartite_graph",
Expand Down Expand Up @@ -105,6 +106,7 @@
"path_graph",
"petersen_graph",
"reciprocity",
"reverse",
"sedgewick_maze_graph",
"single_source_shortest_path_length",
"single_target_shortest_path_length",
Expand Down
2 changes: 2 additions & 0 deletions python/nx-cugraph/nx_cugraph/algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
community,
components,
link_analysis,
operators,
shortest_paths,
traversal,
tree,
Expand All @@ -29,6 +30,7 @@
from .dag import *
from .isolate import *
from .link_analysis import *
from .operators import *
from .reciprocity import *
from .shortest_paths import *
from .traversal import *
Expand Down
13 changes: 13 additions & 0 deletions python/nx-cugraph/nx_cugraph/algorithms/operators/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 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 .unary import *
55 changes: 55 additions & 0 deletions python/nx-cugraph/nx_cugraph/algorithms/operators/unary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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 numpy as np

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

__all__ = ["complement", "reverse"]


@networkx_algorithm(version_added="24.02")
def complement(G):
G = _to_graph(G)
N = G._N
# Upcast to int64 so indices don't overflow.
edges_a_b = N * G.src_indices.astype(np.int64) + G.dst_indices
# Now compute flattened indices for all edges except self-loops
# Alt (slower):
# edges_full = np.arange(N * N)
# edges_full = edges_full[(edges_full % (N + 1)).astype(bool)]
edges_full = cp.arange(1, N * (N - 1) + 1) + cp.repeat(cp.arange(N - 1), N)
edges_comp = cp.setdiff1d(
edges_full,
edges_a_b,
assume_unique=not G.is_multigraph(),
)
src_indices, dst_indices = cp.divmod(edges_comp, N)
return G.__class__.from_coo(
N,
src_indices.astype(index_dtype),
dst_indices.astype(index_dtype),
key_to_id=G.key_to_id,
)


@networkx_algorithm(version_added="24.02")
def reverse(G, copy=True):
if not G.is_directed():
raise nx.NetworkXError("Cannot reverse an undirected graph.")
if isinstance(G, nx.Graph):
G = nxcg.from_networkx(G, preserve_all_attrs=True)
return G.reverse(copy=copy)
Loading