igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] problems with edges


From: Figa Pelosa
Subject: Re: [igraph] problems with edges
Date: Fri, 17 Jul 2009 08:51:01 +0000 (GMT)

thanks indeed.
FP

--- Gio 16/7/09, Tamas Nepusz <address@hidden> ha scritto:

Da: Tamas Nepusz <address@hidden>
Oggetto: Re: [igraph] problems with edges
A: "Help for igraph users" <address@hidden>
Data: Giovedì 16 luglio 2009, 21:09

> 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.



_______________________________________________
igraph-help mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/igraph-help


reply via email to

[Prev in Thread] Current Thread [Next in Thread]