[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [igraph] problems with edges
From: |
Tamas Nepusz |
Subject: |
Re: [igraph] problems with edges |
Date: |
Thu, 16 Jul 2009 22:09:19 +0100 |
Now you can get the edge (which is an instance of an Edge object)
with:
g.es[g.get_eid(0,1)]
igraph.Edge(<igraph.Graph object at 0x8d2fd2c>,0,{})
What i don't understant is where the weight is stored [...]
Well, nowhere... When importing an adjacency matrix using simply
load(), igraph uses Graph.Read_Adjacency to read the graph. This one
has a parameter called "attribute", which is None by default, meaning
that the elements in the adjacency matrix are considered as edge
multiplicities. When you call g.to_undirected(collapse=True), all the
multiple edges are collapsed to a single one, but no weight
information is derived. The solution is to set the "attribute"
parameter of Graph.Read_Adjacency to an attribute name which will then
be used to store the weights:
>>> g = Graph.Read_Adjacency("myGraph.txt", attribute="weight")
>>> g.es["weight"]
[1.0, 2.0, 3.0, 1.0, 4.0, 5.0, 2.0, 4.0, 6.0, 3.0, 5.0, 6.0]
>>> g.es[g.get_eid(0,1)]["weight"]
1.0
Now, the problem is that the graph above is directed, and the weight
information is lost again if you cast it into an undirected graph.
Therefore, you have to specify that you want an undirected graph:
>>> g = Graph.Read_Adjacency("myGraph.txt", attribute="weight",
mode="undirected")
>>> print g.es["weight"]
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
Finally, you should note that load() simply infers which graph loader
method (Read_Adjacency in the above case) should be used and then
passes all its arguments intact to the corresponding loader method, so
you can simply do the following:
>>> g = load("myGraph.txt", attribute="weight", mode="undirected")
--
T.