如何使用权重初始化 igraph?
这是我的
cr = csv.reader(open("atlas.csv","rb"))
mapping = {}
for row in cr:
if int(row[2]) == 1:
continue
source = int(row[0])
target = int(row[1])
if source in mapping:
list = mapping[source]
list.append(target)
else:
mapping[source] = [target]
print mapping
G = Graph(edges = [(v, a) for v in mapping.keys() for a in mapping[v]])
print G
我尝试使用 edges = (v, a, w) 添加权重,但这不起作用。
请您参考如下方法:
根据文档,您可以使用 TupleList方法:
If you have a weighted graph, you can use items where the third item contains the weight of the edge by setting
edge_attrsto"weight"or["weight"].
您还可以将 weights 参数设置为 True:
weights- alternative way to specify that the graph is weighted. If you set weights to true andedge_attrsis not given, it will be assumed thatedge_attrsis["weight"]andigraphwill parse the third element from each item into an edge weight. If you setweightsto a string, it will be assumed thatedge_attrscontains that string only, andigraphwill store the edge weights in that attribute.
