Search code examples
pythongraphigraphweighted

How to create a graph with vertices weight in python with igraph?


I searched but found there are many examples about how to create a graph with edges weight, but none of them shows how to create a graph with vertices weight. I start to wonder if it is possible.

If a vertices-weighted graph can be created with igraph, then is it possible to calculate the weighted independence or other weighted numbers with igraph?


Solution

  • As far as I know, there are no functions in igraph that accept arguments for weighted vertices. However, the SANTA package that is a part of the Bioconductor suite for R does have routines for weighted vertices, if you are willing to move to R for this. (Seems like maybe you can run bioconductor in python.)

    Another hacky option is the use (when possible) unweighted routines from igraph and then back in the weights. E.g. something like this for weighted maximal independent sets:

    def maxset(graph,weight):
        ms = g.maximal_independent_vertex_sets()
        w = []
        t = []
        for i in range(0, 150):
            m = weights.loc[weights['ids'].isin(ms[i]),"weights"]
            w.append(m)
            s = sum(w[i])
            t.append(s) 
        return(ms[t.index(max(t))])
    maxset(g,weights)
    

    (Where weights is a two column data frame with column 1 = vertex ids and column 2 = weights). This gets the maximal independent set taking vertex weights into consideration.