Reputation: 20329
Does the igraph
package provide an interface to retrieve the original graph from a sub selection of vertices? There is for sure some internal way of doing it, as a simple print
shows the total number of vertices in the graph as well as the number of selected vertices and through retro-engineering I was able to get the original graph, but I was wondering whether there is a way supported by the API rather than relying on internal commands.
Code speaks more than words, so here's what I mean:
library(igraph)
G <- make_ring(10)
(v <- V(G)[1:3])
### This output tells me that `v` "knows" about the graph it is taken from
### (namely that it has 10 vertices)
## + 3/10 vertices, from 3fe4e83:
## [1] 1 2 3
The relevant part can be extracted from igraph:::print.igraph.vs
### igraph:::print.igraph.vs
graph <- get_vs_graph(x)
### igraph:::get_vs_graph
at <- attr(seq, "env")
if (class(at) == "weakref") {
weak_ref_key(at)$me
}
That is, I could do something like:
all.equal(G, igraph:::get_vs_graph(v))
## TRUE
But of course we are using an internal function. So I was wondering whether the API would provide a dedicated way of getting the original graph or whether this is not foreseen?
Actually, I have a list of vertex sequences coming all from different graphs. Ideally I would like to get a ratio of the number of vertices in my list by the number of vertices in the respective original graph:
set.seed(1)
Gs <- lapply(10 ^ (1:4), make_ring)
Vs <- lapply(Gs, function(G) V(G)[seq(1, sample(gorder(G), 1))])
sapply(Vs, function(v) length(v) / gorder(igraph:::get_vs_graph(v)))
## [1] 0.9000 0.6800 0.6790 0.9725
Upvotes: 4
Views: 97
Reputation: 48061
Indeed, igraph.vs
objects (and igraph.es
as well) contain a weak reference that points back to the original graph, and we use igraph:::get_vs_graph()
internally to get hold of the original graph.
I can imagine exposing it as a public function in some future version; please file an issue for it in our issue tracker and we can discuss the implications further in the issue tracker.
Upvotes: 2