Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initial node's ids when creating graph from edge list

Tags:

python

igraph

I wonder, where does the function Read_Edgelist store the original id's from the the edge list? or under which attribute name?

Assume that I am reading an edge list like:

1 2
2 1 
1 3

where the numbers 1,2,3 are the ids (or names) of the nodes. Where does the iGraph (python version) stores these ids? I tried retrieving these Ids from the attribute name or id but it did not work, as those two attributes, seemingly, must be explicitly defined.

like image 324
M.M Avatar asked Jul 25 '16 14:07

M.M


1 Answers

Read_Edgelist assumes the node ids are the consecutive integers from 0 to m, where m is the maximum integer in the edge list. So there is "no need to store node ids."

For example, if your edgelist.txt is 1 3, this code

import igraph as ig
g = ig.Graph.Read_Edgelist("edgelist.txt")
print g.get_adjacency()

creates a graph with four nodes (0, 1, 2, 3) and prints

[[0, 0, 0, 0]
 [0, 0, 0, 1]
 [0, 0, 0, 0]
 [0, 0, 0, 0]]

See this answer if you do not want "intermediate" nodes to be created.

While the following is unnecessary for a graph with consecutive node ids starting with 0, one could access the node ids using VertexSeq and Vertex:

for v in g.vs:
  print v.index   # node id
like image 63
Ulrich Stern Avatar answered Nov 02 '22 23:11

Ulrich Stern