-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nx-cugraph: add k_truss and degree centralities
New algorithms: - `degree_centrality` - `in_degree_centrality` - `k_truss` - `number_of_selfloops` - `out_degree_centrality Also, rename `row_indices, col_indices` to `src_indices, dst_indices`
- Loading branch information
Showing
17 changed files
with
325 additions
and
125 deletions.
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
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
48 changes: 48 additions & 0 deletions
48
python/nx-cugraph/nx_cugraph/algorithms/centrality/degree_alg.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,48 @@ | ||
# 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, _to_graph | ||
from nx_cugraph.utils import networkx_algorithm, not_implemented_for | ||
|
||
__all__ = ["degree_centrality", "in_degree_centrality", "out_degree_centrality"] | ||
|
||
|
||
@networkx_algorithm | ||
def degree_centrality(G): | ||
G = _to_graph(G) | ||
if len(G) <= 1: | ||
return dict.fromkeys(G, 1) | ||
deg = G._degrees_array() | ||
centrality = deg * (1 / (len(G) - 1)) | ||
return G._nodearray_to_dict(centrality) | ||
|
||
|
||
@not_implemented_for("undirected") | ||
@networkx_algorithm | ||
def in_degree_centrality(G): | ||
G = _to_directed_graph(G) | ||
if len(G) <= 1: | ||
return dict.fromkeys(G, 1) | ||
deg = G._in_degrees_array() | ||
centrality = deg * (1 / (len(G) - 1)) | ||
return G._nodearray_to_dict(centrality) | ||
|
||
|
||
@not_implemented_for("undirected") | ||
@networkx_algorithm | ||
def out_degree_centrality(G): | ||
G = _to_directed_graph(G) | ||
if len(G) <= 1: | ||
return dict.fromkeys(G, 1) | ||
deg = G._out_degrees_array() | ||
centrality = deg * (1 / (len(G) - 1)) | ||
return G._nodearray_to_dict(centrality) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# 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 | ||
|
||
import nx_cugraph as nxcg | ||
from nx_cugraph.utils import networkx_algorithm, not_implemented_for | ||
|
||
__all__ = ["k_truss"] | ||
|
||
|
||
@not_implemented_for("directed") | ||
@not_implemented_for("multigraph") | ||
@networkx_algorithm | ||
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( | ||
"Input graph has self loops which is not permitted; " | ||
"Consider using G.remove_edges_from(nx.selfloop_edges(G))." | ||
) | ||
# TODO: create renumbering helper function(s) | ||
if k < 3: | ||
# Drop nodes with zero degree | ||
degrees = G._degrees_array() | ||
# Renumber step 0: node indices | ||
node_indices = degrees.nonzero()[0] | ||
if degrees.size == node_indices.size: | ||
# No change | ||
return G if is_nx else G.copy() | ||
src_indices = G.src_indices | ||
dst_indices = G.dst_indices | ||
# Renumber step 1: edge values (no changes needed) | ||
edge_values = {key: val.copy() for key, val in G.edge_values.items()} | ||
edge_masks = {key: val.copy() for key, val in G.edge_masks.items()} | ||
else: | ||
# int dtype for edge_indices would be preferred | ||
edge_indices = cp.arange(G.src_indices.size, dtype=np.float64) | ||
src_indices, dst_indices, edge_indices, _ = plc.k_truss_subgraph( | ||
resource_handle=plc.ResourceHandle(), | ||
graph=G._get_plc_graph(edge_array=edge_indices), | ||
k=k, | ||
do_expensive_check=False, | ||
) | ||
# Renumber step 0: node indices | ||
node_indices = cp.unique(cp.concatenate([src_indices, dst_indices])) | ||
# Renumber step 1: edge values | ||
edge_indices = edge_indices.astype(np.int64) | ||
edge_values = {key: val[edge_indices] for key, val in G.edge_values.items()} | ||
edge_masks = {key: val[edge_indices] for key, val in G.edge_masks.items()} | ||
# Renumber step 2: edge indices | ||
mapper = cp.zeros(len(G), src_indices.dtype) | ||
mapper[node_indices] = cp.arange(node_indices.size, dtype=np.int64) | ||
src_indices = mapper[src_indices] | ||
dst_indices = mapper[dst_indices] | ||
# Renumber step 3: node values | ||
node_values = {key: val[node_indices] for key, val in G.node_values.items()} | ||
node_masks = {key: val[node_indices] for key, val in G.node_masks.items()} | ||
# Renumber step 4: key_to_id | ||
if (id_to_key := G.id_to_key) is not None: | ||
key_to_id = { | ||
id_to_key[old_index]: new_index | ||
for new_index, old_index in enumerate(node_indices.tolist()) | ||
} | ||
else: | ||
key_to_id = None | ||
new_graph = G.__class__.from_coo( | ||
node_indices.size, | ||
src_indices, | ||
dst_indices, | ||
edge_values, | ||
edge_masks, | ||
node_values, | ||
node_masks, | ||
key_to_id=key_to_id, | ||
) | ||
new_graph.graph.update(G.graph) | ||
return new_graph |
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
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,23 @@ | ||
# 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_graph | ||
from nx_cugraph.utils import networkx_algorithm | ||
|
||
__all__ = ["number_of_selfloops"] | ||
|
||
|
||
@networkx_algorithm | ||
def number_of_selfloops(G): | ||
G = _to_graph(G) | ||
is_selfloop = G.src_indices == G.dst_indices | ||
return is_selfloop.sum().tolist() |
Oops, something went wrong.