From 1697a473950e53a4265f2308b8babfcf3382a079 Mon Sep 17 00:00:00 2001 From: cosimoagostinelli <127212527+cosimoagostinelli@users.noreply.github.com> Date: Mon, 21 Oct 2024 16:48:26 +0200 Subject: [PATCH] Update clustering.py Fixing local_clustering_coefficient function --- xgi/algorithms/clustering.py | 46 ++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/xgi/algorithms/clustering.py b/xgi/algorithms/clustering.py index ebf60820..a845c435 100644 --- a/xgi/algorithms/clustering.py +++ b/xgi/algorithms/clustering.py @@ -1,6 +1,7 @@ """Algorithms for computing nodal clustering coefficients.""" import numpy as np +from itertools import combinations from ..exception import XGIError from ..linalg import adjacency_matrix @@ -134,30 +135,29 @@ def local_clustering_coefficient(H): else: total_eo = 0 # go over all pairs of edges pairwise - for e1 in range(dv): + for e1, e2 in combinations(ev, 2): edge1 = members[e1] - for e2 in range(e1): - edge2 = members[e2] - # set differences for the hyperedges - D1 = set(edge1) - set(edge2) - D2 = set(edge2) - set(edge1) - # if edges are the same by definition the extra overlap is zero - if len(D1.union(D2)) == 0: - eo = 0 - else: - # otherwise we have to look at their neighbours - # the neighbours of D1 and D2, respectively. - neighD1 = {i for d in D1 for i in H.nodes.neighbors(d)} - neighD2 = {i for d in D2 for i in H.nodes.neighbors(d)} - # compute extra overlap [len() is used for cardinality of edges] - eo = ( - len(neighD1.intersection(D2)) - + len(neighD2.intersection(D1)) - ) / len( - D1.union(D2) - ) # add it up - # add it up - total_eo = total_eo + eo + edge2 = members[e2] + # set differences for the hyperedges + D1 = set(edge1) - set(edge2) + D2 = set(edge2) - set(edge1) + # if edges are the same by definition the extra overlap is zero + if len(D1.union(D2)) == 0: + eo = 0 + else: + # otherwise we have to look at their neighbours + # the neighbours of D1 and D2, respectively. + neighD1 = {i for d in D1 for i in H.nodes.neighbors(d)} + neighD2 = {i for d in D2 for i in H.nodes.neighbors(d)} + # compute extra overlap [len() is used for cardinality of edges] + eo = ( + len(neighD1.intersection(D2)) + + len(neighD2.intersection(D1)) + ) / len( + D1.union(D2) + ) # add it up + # add it up + total_eo = total_eo + eo # include normalisation by degree k*(k-1)/2 result[n] = 2 * total_eo / (dv * (dv - 1))