igraph-help
[Top][All Lists]
Advanced

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

Re: [igraph] basic graph construction


From: Tamas Nepusz
Subject: Re: [igraph] basic graph construction
Date: Tue, 20 Nov 2007 17:38:21 +0100

Hi Kurt,

> >>> g = igraph.Graph(directed=True)
> >>> g.add_vertices(1)
> 
> Then how can i access that vertex object to add a UID attribute?  Or
> is there a better way?
You can use the vs attribute of the graph object to access the vertex set of
the graph. vs usually behaves as a Python array, so to access the first vertex
of the graph, you simply type g.vs[0]. The result of g.vs[0] is a Vertex
object which usually behaves as a Python dict, so you can use the following:

g.vs[0]["uid"] = "whatever"
print g.vs[0]

Mass-assignment is also possible (but I can't really remember whether it's
available in igraph 0.4.4 or only in the development branch):

g.vs["uid"] = ["list", "of", "uids", "to", "assign"]

So, to access vertex attributes, you use g.vs[vertexindex][attributename] or
g.vs[attributename]. Edge attributes are available as
g.es[edgeindex][attributename].

> Previously i was using networkx which had a nice syntax allowing each
> node to be named as you want.
The reason why it's implemented differently in igraph is because almost all
relevant parts of igraph are written in C, but it's much easier in C to use
numerical vertex/edge indices instead of names. The basic concept is that you
refer to vertices and edges by index in igraph. If you want to look up a
particular vertex by a given attribute, you can use:

g.vs["attributename"].index("value_being_sought"]

which returns the index of the vertex. If mass-assignment and mass-retrieval
of attributes is not available yet in your build (I can't recall whether it's
included in 0.4.4 or not, I'm using the dev branch :)), you can still use:

g.get_vertex_attributes("attributename").index("value_being_sought")

I hope this helped, if not, feel free to post again.

Best,
-- 
T.




reply via email to

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