Nikhil
Nikhil

Reputation: 141

How do I find the vertex id if we have the vertex object in python Igraph v1.7?

a=g.vs(Name_eq="A")
b=g.vs(Name_eq="B")

I want to add an edge between a and b, how do I go about?

Upvotes: 8

Views: 9238

Answers (2)

Tamás
Tamás

Reputation: 48101

Okay, it seems like we have two questions here. One is in the question title: "how do I find the vertex ID if we have the Vertex object"? This is correctly answered by Siddharth: you can simply use the index property of the vertex. The other question is in the question body: "I want to add an edge between a and b, how do I go about?". The answer is simply to use the add_edge method which accepts Vertex objects as well as vertex IDs:

g.add_edge(a, b)

Here I assumed that a and b are objects of type Vertex. However, judging from your code snippet, what you essentially want to do is to add an edge between two vertices for which you know the names. This can also be done using the find method of VertexSeq objects, which works like the selection you do but returns only the first matching vertex. So, you can simply do:

g.add_edge(g.vs.find(Name="A"), g.vs.find(Name="B"))

Even better, if you use the name vertex attribute to store the vertex names (and not Name - note the capital letter), you can even use the name itself without invoking g.vs.find since igraph treats the name vertex attribute specially:

g.add_edge("A", "B")

Upvotes: 16

Siddharth Kumar
Siddharth Kumar

Reputation: 134

You can find the vertex id by accessing a particular vertex of the vertexSeq as 'a' is a vertex sequence object.

Something like this, should do the trick.

a[0].index

Upvotes: 10

Related Questions