Skip to content

Commit 3509513

Browse files
authored
Merge pull request #55 from rapidsai/branch-24.12
Forward-merge branch-24.12 into branch-25.02
2 parents bf72de7 + 138801c commit 3509513

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ Below is the list of algorithms that are currently supported in nx-cugraph.
105105

106106
<pre>
107107
<a href="https://networkx.org/documentation/stable/reference/algorithms/bipartite.html#module-networkx.algorithms.bipartite">bipartite</a>
108+
├─ <a href="https://networkx.org/documentation/stable/reference/algorithms/bipartite.html#module-networkx.algorithms.bipartite.centrality">centrality</a>
109+
│ └─ <a href="https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.bipartite.centrality.betweenness_centrality.html#networkx.algorithms.bipartite.centrality.betweenness_centrality">betweenness_centrality</a>
108110
└─ <a href="https://networkx.org/documentation/stable/reference/algorithms/bipartite.html#module-networkx.algorithms.bipartite.generators">generators</a>
109111
└─ <a href="https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.bipartite.generators.complete_bipartite_graph.html#networkx.algorithms.bipartite.generators.complete_bipartite_graph">complete_bipartite_graph</a>
110112
<a href="https://networkx.org/documentation/stable/reference/algorithms/centrality.html#module-networkx.algorithms.centrality">centrality</a>

_nx_cugraph/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"bfs_successors",
6161
"bfs_tree",
6262
"bidirectional_shortest_path",
63+
"bipartite_betweenness_centrality",
6364
"bull_graph",
6465
"caveman_graph",
6566
"chvatal_graph",

benchmarks/pytest-based/bench_algos.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,27 @@ def bench_ego_graph(benchmark, graph_obj, backend_wrapper):
875875
assert type(result) is type(G)
876876

877877

878+
def bench_bipartite_BC_n1000_m3000_k100000(benchmark, backend_wrapper):
879+
# Example how to run:
880+
# $ pytest -sv -k "bench_bipartite_BC" \
881+
# --benchmark-json="logs/None__bipartite_BC__None.json" \
882+
# bench_algos.py
883+
n = 1000
884+
m = 3000
885+
k = 100000
886+
graph_obj = nx.bipartite.generators.gnmk_random_graph(n, m, k)
887+
G = get_graph_obj_for_benchmark(graph_obj, backend_wrapper)
888+
nodes = list(range(n))
889+
result = benchmark.pedantic(
890+
target=backend_wrapper(nx.bipartite.betweenness_centrality),
891+
args=(G, nodes),
892+
rounds=rounds,
893+
iterations=iterations,
894+
warmup_rounds=warmup_rounds,
895+
)
896+
assert type(result) is dict
897+
898+
878899
@pytest.mark.skip(reason="benchmark not implemented")
879900
def bench_complete_bipartite_graph(benchmark, graph_obj, backend_wrapper):
880901
pass

nx_cugraph/algorithms/bipartite/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
13+
14+
from .centrality import *
1315
from .generators import *
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright (c) 2024, NVIDIA CORPORATION.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import cupy as cp
15+
import pylibcugraph as plc
16+
17+
from nx_cugraph.convert import _to_graph
18+
from nx_cugraph.utils import networkx_algorithm
19+
20+
__all__ = ["betweenness_centrality"]
21+
22+
23+
@networkx_algorithm(
24+
name="bipartite_betweenness_centrality",
25+
version_added="24.12",
26+
_plc="betweenness_centrality",
27+
)
28+
def betweenness_centrality(G, nodes):
29+
G = _to_graph(G)
30+
31+
node_ids, values = plc.betweenness_centrality(
32+
resource_handle=plc.ResourceHandle(),
33+
graph=G._get_plc_graph(),
34+
k=None,
35+
random_state=None,
36+
normalized=False,
37+
include_endpoints=False,
38+
do_expensive_check=False,
39+
)
40+
top_node_ids = G._nodekeys_to_nodearray(set(nodes))
41+
bottom_node_ids = cp.delete(cp.arange(G._N, dtype=top_node_ids.dtype), top_node_ids)
42+
n = top_node_ids.size
43+
m = bottom_node_ids.size
44+
s, t = divmod(n - 1, m)
45+
bet_max_top = (
46+
((m**2) * ((s + 1) ** 2))
47+
+ (m * (s + 1) * (2 * t - s - 1))
48+
- (t * ((2 * s) - t + 3))
49+
) / 2.0
50+
p, r = divmod(m - 1, n)
51+
bet_max_bot = (
52+
((n**2) * ((p + 1) ** 2))
53+
+ (n * (p + 1) * (2 * r - p - 1))
54+
- (r * ((2 * p) - r + 3))
55+
) / 2.0
56+
57+
values = values[cp.argsort(node_ids)]
58+
59+
values[top_node_ids] /= bet_max_top
60+
values[bottom_node_ids] /= bet_max_bot
61+
62+
return G._nodearray_to_dict(values)

0 commit comments

Comments
 (0)