Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run the louvain community detection algorithm in igraph?

Could someone please provide me with a simple example of how to run the louvain community detection algorithm in igraph using the python interface. Is there any documentation?

Thanks!

like image 747
vgoklani Avatar asked Sep 27 '14 00:09

vgoklani


2 Answers

It's called multilevel.community .

According to https://bugs.launchpad.net/igraph/+bug/925038 ... this functionality does exist it's just called igraph_community_multilevel

If you look in the github repository for igraph

https://github.com/igraph/igraph/blob/master/src/community.c

igraph_community_multilevel does exist and it's written in C. I'm not 100% positive this is the algorithm you want but it might be.

This is great news! Thanks! Is this functionality exported into R? Why does the function bear a generic name (igraph_community_multilevel) instead of the name which the authors gave is ("louvain method")? Using the name "louvain" would make it easier for the users to find the function!

like image 61
Gregory Nisbet Avatar answered Sep 30 '22 07:09

Gregory Nisbet


Here is how to estimate the modularity Q using louvain algorithm in 3 different modules in python (igraph,networkx,bct).

import numpy as np
import networkx as nx
np.random.seed(9)

# I will generate a stochastic block model using `networkx` and then extract the weighted adjacency matrix.
sizes = [50, 50, 50] # 3 communities
probs = [[0.25, 0.05, 0.02],
         [0.05, 0.35, 0.07],
         [0.02, 0.07, 0.40]]

# Build the model
Gblock = nx.stochastic_block_model(sizes, probs, seed=0)

# Extract the weighted adjacency
W = np.array(nx.to_numpy_matrix(Gblock, weight='weight'))
W[W==1] = 1.5
print(W.shape)
# (150, 150)

#* Modularity estimation using Louvain algorithm 
# 1. `igraph` package

from igraph import *
graph = Graph.Weighted_Adjacency(W.tolist(), mode=ADJ_UNDIRECTED, attr="weight", loops=False)
louvain_partition = graph.community_multilevel(weights=graph.es['weight'], return_levels=False)
modularity1 = graph.modularity(louvain_partition, weights=graph.es['weight'])
print("The modularity Q based on igraph is {}".format(modularity1))

# 2. `networkx`package using `python-louvain`
# https://python-louvain.readthedocs.io/en/latest/

import networkx as nx
import community
G = nx.from_numpy_array(W)
louvain_partition = community.best_partition(G, weight='weight')
modularity2 = community.modularity(louvain_partition, G, weight='weight')
print("The modularity Q based on networkx is {}".format(modularity2))

# 3. `bct` module
# https://github.com/aestrivex/bctpy

import bct
com, q = bct.community_louvain(W)
print("The modularity Q based on bct is {}".format(q))


This prints:

The modularity Q based on igraph is 0.4257613861340037
The modularity Q based on networkx is 0.4257613861340036
The modularity Q based on bct is 0.42576138613400366
like image 25
seralouk Avatar answered Sep 30 '22 08:09

seralouk