Skip to content

Commit

Permalink
Update clustering.py
Browse files Browse the repository at this point in the history
Fixing local_clustering_coefficient function
  • Loading branch information
cosimoagostinelli authored Oct 21, 2024
1 parent 139a2bc commit 1697a47
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions xgi/algorithms/clustering.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 1697a47

Please sign in to comment.