Discussion:
[igraph] finding raw vertex index of a named vertex (R igraph)
Dr Gregory Jefferis
2018-05-02 09:46:55 UTC
Permalink
Hello,

In R igraph I want to find the raw vertex index from the vertex name

For example:

library(igraph)
gt=graph.tree(1000)
gt=set.vertex.attribute(gt, name = 'name', value = 1001:2000)

Now I want to find the integer vertex index of the vertex named 1001
as.integer(V(gt)['1001'])
[1] 1

I want to do this a lot with some big graphs, so I would like to take
the most direct and efficient approach.
library(microbenchmark)
microbenchmark(as.integer(V(gt)['1001']))
Unit: microseconds
expr min lq mean median uq
max neval
as.integer(V(gt)["1001"]) 559.399 580.2445 640.5165 605.1565 663.3915
1044.307 100
microbenchmark(match(1001, names(V(gt))))
Unit: microseconds
expr min lq mean median uq
max neval
match(1001, names(V(gt))) 71.686 75.9195 86.37762 78.813 92.5785
221.455 100
match(1001, names(V(gt)))
does not seem very elegant or particularly efficient, but strangely is
the fastest approach that I have found so far.

Many thanks for any alternative suggestions,

Greg.

PS timings with CRAN igraph 1.1.2 on R 3.4.3 on macosx 10.12.6

--
Gregory Jefferis, PhD
Division of Neurobiology
MRC Laboratory of Molecular Biology
Francis Crick Avenue
Cambridge Biomedical Campus
Cambridge, CB2 OQH, UK

http://www2.mrc-lmb.cam.ac.uk/group-leaders/h-to-m/g-jefferis
http://jefferislab.org
http://flybrain.stanford.edu
Gábor Csárdi
2018-05-02 16:46:37 UTC
Permalink
I think the important thing here, is to vectorize the query, if you can do
that.

For a single vertex, I think

match('1001', vertex_attr(gt, "name"))

is as fast as this gets. igraph does not have a special index for vertex
names.

Gabor
On Wed, May 2, 2018 at 10:47 AM Dr Gregory Jefferis <
Post by Dr Gregory Jefferis
Hello,
In R igraph I want to find the raw vertex index from the vertex name
library(igraph)
gt=graph.tree(1000)
gt=set.vertex.attribute(gt, name = 'name', value = 1001:2000)
Now I want to find the integer vertex index of the vertex named 1001
as.integer(V(gt)['1001'])
[1] 1
I want to do this a lot with some big graphs, so I would like to take
the most direct and efficient approach.
library(microbenchmark)
microbenchmark(as.integer(V(gt)['1001']))
Unit: microseconds
expr min lq mean median uq
max neval
as.integer(V(gt)["1001"]) 559.399 580.2445 640.5165 605.1565 663.3915
1044.307 100
microbenchmark(match(1001, names(V(gt))))
Unit: microseconds
expr min lq mean median uq
max neval
match(1001, names(V(gt))) 71.686 75.9195 86.37762 78.813 92.5785
221.455 100
match(1001, names(V(gt)))
does not seem very elegant or particularly efficient, but strangely is
the fastest approach that I have found so far.
Many thanks for any alternative suggestions,
Greg.
PS timings with CRAN igraph 1.1.2 on R 3.4.3 on macosx 10.12.6
--
Gregory Jefferis, PhD
Division of Neurobiology
MRC Laboratory of Molecular Biology
Francis Crick Avenue
Cambridge Biomedical Campus
Cambridge, CB2 OQH, UK
http://www2.mrc-lmb.cam.ac.uk/group-leaders/h-to-m/g-jefferis
http://jefferislab.org
http://flybrain.stanford.edu
_______________________________________________
igraph-help mailing list
https://lists.nongnu.org/mailman/listinfo/igraph-help
Dr Gregory Jefferis
2018-05-03 15:25:23 UTC
Permalink
Thank you, Gabor.
Post by Gábor Csárdi
match('1001', vertex_attr(gt, "name"))
match(1001, vertex_attr(gt, "name"))

is considerably faster than:

match(1001, names(V(gt))

Best,

Greg.

--
Gregory Jefferis, PhD
Division of Neurobiology
MRC Laboratory of Molecular Biology
Francis Crick Avenue
Cambridge Biomedical Campus
Cambridge, CB2 OQH, UK

http://www2.mrc-lmb.cam.ac.uk/group-leaders/h-to-m/g-jefferis
http://jefferislab.org
http://flybrain.stanford.edu
George Vega Yon
2018-05-03 18:23:15 UTC
Permalink
As long as I understand the match function is vectorized, hence you don't
need to do a one-by-one search, i.e. instead of sapply(list_of_names,
match, vertex_attr(gt, "name")) you could simply do match(list_of_names,
vertex_attr(gt, "name"))

HIH

George G. Vega Yon
+1 (626) 381 8171
https://ggvy.cl

On Thu, May 3, 2018 at 8:25 AM, Dr Gregory Jefferis <
Post by Dr Gregory Jefferis
Thank you, Gabor.
Post by Gábor Csárdi
match('1001', vertex_attr(gt, "name"))
match(1001, vertex_attr(gt, "name"))
match(1001, names(V(gt))
Best,
Greg.
--
Gregory Jefferis, PhD
Division of Neurobiology
MRC Laboratory of Molecular Biology
Francis Crick Avenue
Cambridge Biomedical Campus
Cambridge, CB2 OQH, UK
http://www2.mrc-lmb.cam.ac.uk/group-leaders/h-to-m/g-jefferis
http://jefferislab.org
http://flybrain.stanford.edu
_______________________________________________
igraph-help mailing list
https://lists.nongnu.org/mailman/listinfo/igraph-help
Loading...