python - 将列表列表(表示边)中的数据加载到 python 中的 igraph 图中

标签 python igraph

我有一些列表列表格式的关系数据,我想将其导入到 iGraph.Graph() 中。列表列表包含重复边,最终,我想为重复边添加边权重。但是,目前,我无法弄清楚在尝试将边添加到图形时我做错了什么,减去权重因子。

我认为问题在于我必须先将所有顶点导入图中,然后我才能在顶点之间添加边,但事实并非如此。

我在将这些边加载到图中时做错了什么?

*我如何修改我的边加载过程以首先在图中查找边,如果没有找到,则添加边,如果找到,则将该边的权重增加 1,*

数据

In [60]:    edges

Out[60]:    [['a', 'b'],
             ['a', 'b'],
             ['a', 'b'],
             ['b', 'a'],
             ['a', 'c'],
             ['c', 'a'],
             ['c', 'd'],
             ['c', 'd'],
             ['d', 'c'],
             ['d', 'c']]

将边加载到图形中并出错的 IGRAPH 代码

In [61]:    # extract vertices for edges list
            vertices = []
            for line in edges:
                nodes.append(line[0])
                nodes.append(line[1])

            # find unique vertices
            uniq_vertices = set(sorted(nodes))

In [62]:    # create an empty graph
            g = igraph.Graph()

In [63]:    # add vertices to the graph
            g.add_vertices(uniq_vertices)

In [64]:    # for each line in the edges list, check to see if it's already in the graph, and if not, add it to the graph.
           for line in edges:
                 if not line in g.get_edgelist():
                     g.add_edges(edges)     

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-64-04a376c78860> in <module>()
      2 for line in edges:
      3     if not line in g.get_edgelist():
----> 4         g.add_edges(edges)

C:\Users\Curtis\Anaconda\lib\site-packages\igraph\__init__.pyc in add_edges(self, es)
    228           endpoints. Vertices are enumerated from zero.
    229         """
--> 230         return GraphBase.add_edges(self, es)
    231 
    232     def add_vertex(self, name=None, **kwds):

ValueError: no such vertex: 'a'

最佳答案

问题大概就在这里(至少这部分代码对我来说没有意义):

for line in edges:
    if not line in g.get_edgelist():
        g.add_edges(edges)

这里检查line是否在g.get_edgelist()中,放心不会是因为你的edges列表包含列表,而 g.get_edgelist() 返回 元组。然后添加所有边,而不仅仅是您检查过的边。

我附上了您的代码的另一个版本,它似乎可以为我完成这项工作。请注意,我在创建图形时并没有消除多条边 - 我添加了它们,然后简单地要求 igraph 将它们折叠成一个并将它们的权重加在一起:

import igraph

edges = [['a', 'b'],
         ['a', 'b'],
         ['a', 'b'],
         ['b', 'a'],
         ['a', 'c'],
         ['c', 'a'],
         ['c', 'd'],
         ['c', 'd'],
         ['d', 'c'],
         ['d', 'c']]

# collect the set of vertex names and then sort them into a list
vertices = set()
for line in edges:
    vertices.update(line)
vertices = sorted(vertices)

# create an empty graph
g = igraph.Graph()

# add vertices to the graph
g.add_vertices(vertices)

# add edges to the graph
g.add_edges(edges)

# set the weight of every edge to 1
g.es["weight"] = 1

# collapse multiple edges and sum their weights
g.simplify(combine_edges={"weight": "sum"})

关于python - 将列表列表(表示边)中的数据加载到 python 中的 igraph 图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25835976/

相关文章:

r - 图的排列测试 - igraph

python - 日期时间字符串格式对齐

python - 如何将 bool 变量从机器人框架传递给 python 函数

r - R 中 igraph 的边缘条件

r - igraph 创建加权邻接矩阵

r - 使用 igraph 将节点属性分配给边权重

r - 使用 ggraph/ggplot2 在网络图中定位节点和边

python - SwiftUI - 用 Python 和模块库打包

python - 如何更改 matplotlib 中的 x 轴以便没有空格?

python - 自己制作的游戏。 Game Over 后不执行 Else 分支