How to find vertex id if we have 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 do this?

+6
source share
2 answers

You can find the vertex identifier by referring to a specific vertexSeq vertex, since "a" is an object of the vertex sequence.

Something like this should do the trick.

 a[0].index 
+7
source

Well, it looks like we have two questions. There is one question in the title of the question: "How to find the identifier of the vertices if we have the Vertex object"? Siddhart answered correctly: you can simply use the index property for the top. Another question is in the question: "I want to add a line between a and b, how should I go?" The answer is simply to use the add_edge method, which accepts Vertex objects as well as vertex identifiers:

 g.add_edge(a, b) 

Here I suggested that a and b are objects of type Vertex . However, judging by your code snippet, you essentially want to do is add a border between the two vertices for which you know the names. You can also do this using the find method of VertexSeq objects, which works the way you do, but returns only the first vertex that VertexSeq . So you can simply:

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

Even better, if you use the name vertex attribute to store vertex names (rather than name - pay attention to the capital letter), you can even use the name itself without calling g.vs.find , since igraph considers the name vertex attribute:

 g.add_edge("A", "B") 
+11
source

Source: https://habr.com/ru/post/946927/


All Articles