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 triangles and clustering algorithms #4093

Merged
merged 7 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ def is_bipartite(G):
G = _to_graph(G)
# Counting triangles may not be the fastest way to do this, but it is simple.
node_ids, triangles, is_single_node = _triangles(
G, None, symmetrize="union" if G.is_directed else None
G, None, symmetrize="union" if G.is_directed() else None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this discovered by the new tests? :)

)
return int(cp.count_nonzero(triangles)) == 0
9 changes: 3 additions & 6 deletions python/nx-cugraph/nx_cugraph/algorithms/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def clustering(G, nodes=None, weight=None):
return 0
degree = int((G.src_indices == nodes).sum())
return 2 * numer / (degree * (degree - 1))
# What about self-edges?
degrees = G._degrees_array()[node_ids]
degrees = G._degrees_array(ignore_selfloops=True)[node_ids]
denom = degrees * (degrees - 1)
results = 2 * triangles / denom
results = cp.where(denom, results, 0) # 0 where we divided by 0
Expand All @@ -91,8 +90,7 @@ def average_clustering(G, nodes=None, weight=None, count_zeros=True):
node_ids, triangles, is_single_node = _triangles(G, nodes)
if len(G) == 0:
raise ZeroDivisionError
# What about self-edges?
degrees = G._degrees_array()[node_ids]
degrees = G._degrees_array(ignore_selfloops=True)[node_ids]
if not count_zeros:
mask = triangles != 0
triangles = triangles[mask]
Expand Down Expand Up @@ -127,8 +125,7 @@ def transitivity(G):
numer = int(triangles.sum())
if numer == 0:
return 0
# What about self-edges?
degrees = G._degrees_array()[node_ids]
degrees = G._degrees_array(ignore_selfloops=True)[node_ids]
denom = int((degrees * (degrees - 1)).sum())
return 2 * numer / denom

Expand Down
18 changes: 13 additions & 5 deletions python/nx-cugraph/nx_cugraph/classes/digraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,16 @@ def to_undirected(self, reciprocal=False, as_view=False):
# Private methods #
###################

def _in_degrees_array(self):
return cp.bincount(self.dst_indices, minlength=self._N)

def _out_degrees_array(self):
return cp.bincount(self.src_indices, minlength=self._N)
def _in_degrees_array(self, *, ignore_selfloops=False):
dst_indices = self.dst_indices
if ignore_selfloops:
not_selfloops = self.src_indices != dst_indices
dst_indices = dst_indices[not_selfloops]
return cp.bincount(dst_indices, minlength=self._N)

def _out_degrees_array(self, *, ignore_selfloops=False):
src_indices = self.src_indices
if ignore_selfloops:
not_selfloops = src_indices != self.dst_indices
src_indices = src_indices[not_selfloops]
return cp.bincount(src_indices, minlength=self._N)
13 changes: 10 additions & 3 deletions python/nx-cugraph/nx_cugraph/classes/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,10 +732,17 @@ def _become(self, other: Graph):
self.graph = graph
return self

def _degrees_array(self):
degrees = cp.bincount(self.src_indices, minlength=self._N)
def _degrees_array(self, *, ignore_selfloops=False):
src_indices = self.src_indices
dst_indices = self.dst_indices
if ignore_selfloops:
not_selfloops = src_indices != dst_indices
src_indices = src_indices[not_selfloops]
if self.is_directed():
dst_indices = dst_indices[not_selfloops]
degrees = cp.bincount(src_indices, minlength=self._N)
if self.is_directed():
degrees += cp.bincount(self.dst_indices, minlength=self._N)
degrees += cp.bincount(dst_indices, minlength=self._N)
return degrees

_in_degrees_array = _degrees_array
Expand Down
48 changes: 48 additions & 0 deletions python/nx-cugraph/nx_cugraph/tests/test_cluster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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 networkx as nx
import pytest
from packaging.version import parse

nxver = parse(nx.__version__)

if nxver.major == 3 and nxver.minor < 2:
pytest.skip("Need NetworkX >=3.2 to test clustering", allow_module_level=True)


def test_selfloops():
G = nx.complete_graph(5)
H = nx.complete_graph(5)
H.add_edge(0, 0)
H.add_edge(1, 1)
H.add_edge(2, 2)
# triangles
expected = nx.triangles(G)
assert expected == nx.triangles(H)
assert expected == nx.triangles(G, backend="cugraph")
assert expected == nx.triangles(H, backend="cugraph")
# average_clustering
expected = nx.average_clustering(G)
assert expected == nx.average_clustering(H)
assert expected == nx.average_clustering(G, backend="cugraph")
assert expected == nx.average_clustering(H, backend="cugraph")
# clustering
expected = nx.clustering(G)
assert expected == nx.clustering(H)
assert expected == nx.clustering(G, backend="cugraph")
assert expected == nx.clustering(H, backend="cugraph")
# transitivity
expected = nx.transitivity(G)
assert expected == nx.transitivity(H)
assert expected == nx.transitivity(G, backend="cugraph")
assert expected == nx.transitivity(H, backend="cugraph")
Loading