Reputation: 809
How can I initialise an igraph with weights?
This is what I have
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
I tried adding weight using edges = (v, a, w)
but that doesn't work.
Upvotes: 3
Views: 862
Reputation: 33701
According to the documentation, you may use TupleList
method:
If you have a weighted graph, you can use items where the third item contains the weight of the edge by setting
edge_attrs
to"weight"
or["weight"]
.
You may also set weights
parameter to True
:
weights
- alternative way to specify that the graph is weighted. If you set weights to true andedge_attrs
is not given, it will be assumed thatedge_attrs
is["weight"]
andigraph
will parse the third element from each item into an edge weight. If you setweights
to a string, it will be assumed thatedge_attrs
contains that string only, andigraph
will store the edge weights in that attribute.
Upvotes: 3