I am working with a sparse network dataset. The vertex id in the dataset may hold a value, between 1 and 4000. For example, a small selection of the data may include the values 2, 300, 400, 700, 3000, etc.
It's not clear to me if the igraph library only accepts a sparse dataset or expects a range of values.
Do I have to redefine/remap my dataset into a continuous data range, before I call any igraph function, e.g remapping the data values as follows:
Then call an igraph function and do a reverse map after the Igraph function has finished or can I just call the igraph function on the range 2,300,400,700,3000
without doing any remapping?
igraph
vertex and edge indices are always continuous ranges of integers, i.e. if you have 20 nodes in R igraph
vertex indices will go from 1
to 20
and in Python igraph
from 0
to 19
. In addition the indices are not guaranteed to remain unchanged if you add or remove elements. Consequently the solution is to create a vertex attribute with your indices. For example:
require(igraph)
vertices <- sample(1:4000, 20, replace=F)
source <- sample(vertices, 40, replace = T)
target <- sample(vertices, 40, replace = T)
edges <- data.frame(source = source, target = target)
g <- graph.data.frame(edges)
g
IGRAPH DN-- 20 40 --
+ attr: name (v/c)
+ edges (vertex names):
[1] 2943->3671 822 ->1587 922 ->1694 822 ->1096 1694->1739 922 ->1096
[7] 922 ->1739 1739->1587 2943->2666 2891->2956 1096->822 267 ->839
[13] 2891->2666 767 ->839 767 ->2956 1694->523 839 ->1739 1096->3641
[19] 1348->1739 350 ->839 1096->267 523 ->922 3641->1739 267 ->699
[25] 523 ->1739 1587->699 267 ->1096 1587->3641 523 ->1587 1739->839
[31] 1587->3641 1348->267 1694->350 1587->755 1348->922 839 ->2666
[37] 1739->699 922 ->1348 3641->2891 1096->2943
Notice that igrap
automatically created the vertex attribute name
to store the original indices, while the internal indices are 1:20
as usual. The vertex 2943
will always have this name unless you change it, while its current index is 1
but it can change upon reindexing.
vertex.attributes(g)
$name
[1] "2943" "822" "922" "1694" "1739" "2891" "1096" "267" "767" "839"
[11] "1348" "350" "523" "3641" "1587" "3671" "2666" "2956" "699" "755"
V(g)[1]
+ 1/20 vertex, named:
[1] 2943
V(g)[V(g)$name == 2943]
+ 1/20 vertex, named:
[1] 2943