简体   繁体   中英

How to query the vertices connected to a vertex in igraph?

With the python-igraph package I am creating eg a simple graph with 4 vertices:

g = igraph.Graph()
g.add_vertices(4)
g.add_edges([(0,1),(0,2),(0,3),(1,2),(1,3)])

How can I figure out, to which vertices a vertex i is connected directly? Vertex 0 is, for example, connected to all other 3 vertices, while vertex 2 is not connected to vertex 3?

I read the tutorial and took a look at the manual, but I cannot find an appropriate function to do that. So, how to find out which verices are connected directly to a vertex?

I think you can use neighbors which returns the adjacent vertices of a given vertex.

>>> print(g.neighbors(0))
[1, 2, 3]

I hope this will help you.

If you just need the vertex ids connected to a specific vertex neighbors is the function you need. There is a more general function called neighborhood . It returns a list or list of lists of vertices at a distance from the specific vertex. Order parameter in this function is used as a distance from the specific vertex. Note that the returned list contains the specific vertex.

>>> print g.neighborhood(0, order=1)
[0, 1, 2, 3]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM