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

ENH : added square_clustering #34

Merged
merged 16 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
17 changes: 17 additions & 0 deletions benchmarks/benchmarks/bench_cluster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from .common import (
backends,
num_nodes,
edge_prob,
get_cached_gnp_random_graph,
Benchmark,
)
import networkx as nx


class Cluster(Benchmark):
params = [(backends), (num_nodes), (edge_prob)]
param_names = ["backend", "num_nodes", "edge_prob"]

def time_square_clustering(self, backend, num_nodes, edge_prob):
G = get_cached_gnp_random_graph(num_nodes, edge_prob)
_ = nx.square_clustering(G, backend=backend)
1 change: 1 addition & 0 deletions nx_parallel/algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from .isolate import *
from .tournament import *
from .vitality import *
from .cluster import *
46 changes: 46 additions & 0 deletions nx_parallel/algorithms/cluster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from itertools import combinations
from joblib import Parallel, delayed
import nx_parallel as nxp

__all__ = ["square_clustering"]


def square_clustering(G, nodes=None):
"""The squares clustering coefficient for nodes for all nodes
are computed in parallel over all available CPU cores.

networkx.square_clustering: https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.cluster.square_clustering.html
"""

def _compute_clustering(v):
clustering = 0
potential = 0
for u, w in combinations(G[v], 2):
squares = len((set(G[u]) & set(G[w])) - {v})
clustering += squares
degm = squares + 1
if w in G[u]:
degm += 1
potential += (len(G[u]) - degm) + (len(G[w]) - degm) + squares
if potential > 0:
clustering /= potential
return (v, clustering)

if hasattr(G, "graph_object"):
G = G.graph_object

if nodes is None:
node_iter = G
else:
node_iter = G.nbunch_iter(nodes)

total_cores = nxp.cpu_count()

result = Parallel(n_jobs=total_cores)(
delayed(_compute_clustering)(v) for v in node_iter
)
clustering = dict(result)

if nodes in G:
return clustering[nodes]
return clustering
4 changes: 4 additions & 0 deletions nx_parallel/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
tournament_is_strongly_connected,
)
from nx_parallel.algorithms.vitality import closeness_vitality
from nx_parallel.algorithms.cluster import square_clustering

__all__ = ["Dispatcher", "ParallelGraph"]

Expand Down Expand Up @@ -49,6 +50,9 @@ class Dispatcher:
# Shortest Paths : all pairs shortest paths(bellman_ford)
all_pairs_bellman_ford_path = all_pairs_bellman_ford_path

# Clustering
square_clustering = square_clustering

# =============================

@staticmethod
Expand Down
Loading