Leiden find communities for a weighted graph

65 Views Asked by At

I have a graph that I want to cluster with Leiden. The graph is weighted.

My question is, how do I apply weights in the Leiden algorithm?

import leidenalg as la

def leiden(igraph, leiden_partition, pandas_df):

    if leiden_partition=="Modularity":
        partition = la.ModularityVertexPartition(igraph, weights=pandas_df['weight'])
    elif leiden_partition=="CPM":
        partition = la.CPMVertexPartition(igraph, resolution_parameter=1, weights=pandas_df['weight'])

    part = la.find_partition(graph, partition, weights=pandas_df['weight'])
   
    return part

Currently, the igraph object has no weights, since I want to repeatedly apply different weighting schema and cluster the network. Rather, I want to have the unweighted graph loaded and only calculate and apply weights for the clustering.

These are the questions I have:

  1. Is my approach of passing weights separately correct? I ensured that the edges and weight orders were the same. The problem with that is as with the documentation, the adjacency matrix is binary, but edges are weighted (say between 0 and 100) but even in that sense I never achieved negative modularity, so I guess this should not be a problem.

  2. Where to apply the weights? Given the above documentation, both the partitions (CPM and modularity) accept weights, and so does the find_partition function. shall I pass it to both?

Thanks.

0

There are 0 best solutions below