How do V and E iterators work in igraph using R?

I looked at the source for V and E, and I'm not quite sure how they work. Here is the code for V:

> V
function (graph)
{
    if (!is.igraph(graph)) {
        stop("Not a graph object")
    }
    vc <- vcount(graph)
    if (vc == 0) {
        res <- numeric()
    }
    else {
        res <- 0:(vc - 1)
    }
    class(res) <- "igraph.vs"
    ne <- new.env()
    assign("graph", graph, envir = ne)
    attr(res, "env") <- ne
    res
}

I'm not quite sure why the purpose of the call and attr is here. Does the graph assign a new copy of the graph? How effective / ineffective is it? That is, how many copies of the graph this expression generates in the code, for example:

V(g)$someattr <- somevector

Thanks for the help.

+5
source share
1 answer

V assign attr , . , - V(g)$color = 'blue', g. , , igraph.vs.

> methods(class='igraph.vs')
[1] [.igraph.vs     [<-.igraph.vs   $.igraph.vs     $<-.igraph.vs   print.igraph.vs

> `$.igraph.vs`
function (x, name) 
{
    get.vertex.attribute(get("graph", attr(x, "env")), name, 
        x)
}
<environment: namespace:igraph>

, $ , .

, (, , , ). , g, vs = V(g). g, g, , vs.

> g = graph(c(0:1), directed=F)
> g = set.vertex.attribute(g, 'color', value='blue')
> vs = V(g)
> vs$color
[1] "blue" "blue"
> g = set.vertex.attribute(g, 'color', value='red')
> V(g)$color
[1] "red" "red"
> vs$color
[1] "blue" "blue"
+1

All Articles