在python中使用“igraph”读取加权图

时间:2014-04-28 17:07:54

标签: python igraph

我需要从外部文件中读取加权图

在其他帖子中建议使用ncol,但我尝试了“ncol”格式而不能正常工作:

g = igraph.Graph.Read_Ncol("small.ncol")
for vertex in g.vs():
    print vertex["weight"]

small.ncol

0   1   0.47
2   0   0.67
2   1   0.94
3   0   0.98
3   1   0.05
3   2   0.24
4   0   0.12
4   1   0.22
4   2   0.36
4   3   0.69
5   0   0.82
6   5   0.97
7   5   0.43
7   6   0.83
8   5   0.44
8   6   0.49
8   7   0.39
9   5   0.37
9   6   0.55
9   7   0.73
9   8   0.68
10  0   0.34
11  10  0.22
12  11  0.40
13  12  0.78
14  10  0.59
14  13  0.81

输出:

Traceback (most recent call last):
  File "stackoverflow.py", line 54, in <module>
    print vertex["weight"]
KeyError: 'Attribute does not exist'

我试图从Nexus读取加权图:

例如:这是加权图:http://nexus.igraph.org/api/dataset_info?format=xml&id=1

<id>1</id>
<sid>karate</sid>
<tags>
    <tag>social network</tag>
    <tag>undirected</tag>
    **<tag>weighted</tag>**
</tags>

但是也没有工作:

g = igraph.Nexus.get("karate")
for vertex in g.vs():
    print vertex["weight"]

输出:

Traceback (most recent call last):
  File "stackoverflow.py", line 54, in <module>
    print vertex["weight"]
KeyError: 'Attribute does not exist'

我不知道如何阅读加权图表,有人可以提供帮助吗?

1 个答案:

答案 0 :(得分:3)

您正在尝试读取顶点的权重,但在这些图表中,权重对应于边缘

g = igraph.Nexus.get("karate")
for edge in g.es:
    print edge["weight"]
相关问题