【问题标题】:How to create a graph with vertices weight in python with igraph?如何使用 igraph 在 python 中创建具有顶点权重的图?
【发布时间】:2016-11-08 21:06:45
【问题描述】:

我搜索但发现有很多关于如何创建具有边权重的图形的示例,但没有一个显示如何创建具有顶点权重的图形。我开始怀疑这是否可能。

如果可以用igraph创建顶点加权图,那么是否可以用igraph计算加权独立性或其他加权数?

【问题讨论】:

    标签: python graph igraph weighted


    【解决方案1】:

    据我所知,igraph 中没有接受加权顶点参数的函数。但是,作为 R 的 Bioconductor 套件的一部分的 SANTA 包确实具有加权顶点的例程,如果您愿意为此迁移到 R。 (好像也许你可以运行bioconductor in python。)

    另一个 hacky 选项是使用(如果可能)来自 igraph 的未加权例程,然后返回权重。例如。对于加权最大独立集是这样的:

    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)
    

    (其中 weights 是一个两列数据框,第 1 列 = 顶点 ID,第 2 列 = 权重)。这得到考虑到顶点权重的最大独立集。

    【讨论】:

    • 感谢您的回答!哪个包可以涵盖更多的功能,无论是在python还是R或其他软件中?你有什么建议吗?
    • @EdenHarder 确实没有一个包可以涵盖所有内容。我主要使用 R 中的 statnet/sna/network/ergm 软件包套件,因为我主要做社交网络分析。 R、C 和 python 中的 igraph 对我来说更倾向于图论。 Python 中的 Networkx 面向复杂的网络分析。真的,这取决于你想要达到的目标。我发现 igraph 和 statnet 的一些组合对我有用。
    • R 中有很多更小的包覆盖更具体的网络,例如 asnipe 和 enaR,它们专注于生态学家和生物学家的网络分析。
    • 谢谢,我正在寻找面向图论的,看来 Sagemath 是一个不错的解决方案。
    【解决方案2】:

    您想使用vs 类在igraph 中定义顶点及其属性。

    作为设置顶点权重的示例,取自文档:

    http://igraph.org/python/doc/igraph.VertexSeq-class.html

    g=Graph.Full(3) # generate a full graph as example
    
    for idx, v in enumerate(g.vs):
        v["weight"] = idx*(idx+1) # set the 'weight' of vertex to integer, in function of a progressive index
    
    >>> g.vs["weight"]
    [0, 2, 6]
    

    注意,顶点序列是通过 g.vs 调用的,这里 g 是 Graph 对象的实例。

    我向您推荐了这个页面,我发现在这里寻找 iGraph 方法很实用: http://igraph.org/python/doc/identifier-index.html

    【讨论】:

      猜你喜欢
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多