What is the node order of Graph.indegree() ? Is it the same with Graph vertex dataframe?
igraph identifies vertices with contiguous integers indices starting at 0. All functions that return quantities associated with vertices produce a list ordered the same way as the vertices. You can use the vertex index as and index into the list returned by Graph.indegree()
. I hope this is clear.
igraph does not use dataframes internally. If you’ve assigned vertex names, you can check the vertex ordering using g.vs['name']
.
If you assigned names based on a dataframe and the Graph.DataFrame
method, then the first column was supposed to contain the vertex indices. If these were not in order, then the vertices in the graph won’t follow the same order as in the dataframe either.
Thank you. Then how to get the indegree according to a specific name of a node?
Pass the name to the vertices
parameter of the method. See igraph.GraphBase
In [1]: import igraph as ig
In [2]: g = ig.Graph.Formula('a->b<-c')
In [3]: g.vs['name']
Out[3]: ['a', 'b', 'c']
Here you can see that vertices 0, 1 and 2 are called 'a'
, 'b'
and 'c'
. In most places you can refer to vertices either by index (also called “vertex ID”) or by name.
In [4]: g.indegree() # degrees of all vertices in order
Out[4]: [0, 2, 0]
In [5]: g.indegree(1) # degree of vertex with ID 1
Out[5]: 2
In [6]: g.indegree('b') # we can also refer to it by name instead of ID
Out[6]: 2
In [7]: g.indegree(['b', 'c']) # get the degrees of two vertices
Out[7]: [2, 0]
Keep in mind that using vertex names is slower than using vertex IDs and doesn’t work reliably if you have duplicate names (which you might create accidentally). There are some places where only vertex IDs work, but names don’t.