What is the stablished value for the modularity of the Karate Club?

105 Views Asked by At

The question speaks for itself. In the literature, we find that the karate club graph has a modularity value of 0.42. A python library to compute the modularity is networkx. To obtain the modularity of a graph we need to run the Louvain Community Detection Algorithm (LCDA). I provide a code snipped for reproducibility:

!pip install networkx
import networkx as nx
from networkx.algorithms.community import louvain_communities, modularity
import numpy as np
from joblib import Parallel, delayed
G = nx.karate_club_graph()

def seed_run(seed, runs=100):
  communities_lcda = []
  results_lcda = []
  Ncomms = 0
  for i in range(runs):
    communities_lcda.append(louvain_communities(G, seed=seed, resolution=1))
    results_lcda.append(modularity(G,communities_lcda[i]))
    Ncomms += len(communities_lcda[i])/100
  results_lcda = np.array(results_lcda)
  return results_lcda.mean(), results_lcda.std()/np.sqrt(runs)

results = Parallel(n_jobs=4)(delayed(seed_run)(seed, runs=100) for seed in range(10000))

results_mod = np.array(results)[:,0]
target = 0.42
import matplotlib.pylab as plt
%matplotlib inline
plt.plot(results_mod, 'o')

It can take a while to run all the 10000 seeds. In the original reference it is mentioned that the node ordering can influence the results so we would not be surprised to see some variability. However, we find it strange that everyone is reporting a modularity of 0.42. What are we doing wrong? Or maybe, what is happending inside networkx that we can't see?

https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.community.louvain.louvain_communities.html#networkx.algorithms.community.louvain.louvain_communities

Scatter plot of the results