I have been playing around with igraph lately and think that it is a great package as it adds tremendous functionality to R. However, I continually get into trouble with basic edge and vertex operations, especially, adding and removing edges. I have added some examples of confusion at the end of this mail and attached the code.
I think that there is a certain lack of consistency in how they operate, to the degree that I would consider it a bug. Maybe I'm just wrong in expecting that the examples given below should work in some consistent way. However, it makes it extremely difficult to decide whether a certain way to operate on some elements of a graph (e.g. get all edges via E(G) or get.edges(G,...)) will work for a certain problem or not (e.g. add them to a graph via + or add.edges, ...) when writing code. A major problem that I see is that it is essentially unclear how to write iterators over the nodes or edges of an igraph object (without resorting to exporting to other classes (eg via get.edgelist)).
R 2.15.1> require(igraph)
Loading required package: igraph
R 2.15.1> # Consider the following graphs
R 2.15.1>
R 2.15.1> G4 <- graph.full(4)
R 2.15.1> G <- minimum.spanning.tree(G4)
R 2.15.1> g <- G4 - G
R 2.15.1>
R 2.15.1> G[]
Loading required package: Matrix
Loading required package: lattice
4 x 4 sparse Matrix of class "dgCMatrix"
[1,] . 1 1 1
[2,] 1 . . .
[3,] 1 . . .
[4,] 1 . . .
R 2.15.1> g[]
4 x 4 sparse Matrix of class "dgCMatrix"
[1,] . . . .
[2,] . . 1 1
[3,] . 1 . 1
[4,] . 1 1 .
R 2.15.1>
R 2.15.1> # now here comes what confuses me
R 2.15.1>
R 2.15.1> for(e in E(G))
+ print(delete.edges(G,e)[])
4 x 4 sparse Matrix of class "dgCMatrix"
[1,] . . 1 1
[2,] . . . .
[3,] 1 . . .
[4,] 1 . . .
4 x 4 sparse Matrix of class "dgCMatrix"
[1,] . 1 . 1
[2,] 1 . . .
[3,] . . . .
[4,] 1 . . .
4 x 4 sparse Matrix of class "dgCMatrix"
[1,] . 1 1 .
[2,] 1 . . .
[3,] 1 . . .
[4,] . . . .
R 2.15.1>
R 2.15.1> # works
R 2.15.1>
R 2.15.1> for(e in E(G))
+ print(add.edges(g,e)[])
Error in print(add.edges(g, e)[]) :
error in evaluating the argument 'x' in selecting a method for function 'print': Error in add.edges(g, e) :
At type_indexededgelist.c:269 : invalid (odd) length of edges vector, Invalid edge vector
R 2.15.1>
R 2.15.1> # gives an error
R 2.15.1>
R 2.15.1> # next
R 2.15.1> e <- E(G)[1]
R 2.15.1> E <- get.edge(G,1)
R 2.15.1>
R 2.15.1> delete.edges(G,e)[]
4 x 4 sparse Matrix of class "dgCMatrix"
[1,] . . 1 1
[2,] . . . .
[3,] 1 . . .
[4,] 1 . . .
R 2.15.1> # removes one edge
R 2.15.1>
R 2.15.1> delete.edges(G,E)[]
4 x 4 sparse Matrix of class "dgCMatrix"
[1,] . . . 1
[2,] . . . .
[3,] . . . .
[4,] 1 . . .
R 2.15.1> # removes two edges
R 2.15.1>
R 2.15.1> (G - e)[]
4 x 4 sparse Matrix of class "dgCMatrix"
[1,] . . 1 1
[2,] . . . .
[3,] 1 . . .
[4,] 1 . . .
R 2.15.1> # works
R 2.15.1>
R 2.15.1> (G - E)[]
2 x 2 sparse Matrix of class "dgCMatrix"
[1,] . .
[2,] . .
R 2.15.1> # empty graph with 2 nodes
R 2.15.1>
R 2.15.1> add.edges(g,e)
Error in add.edges(g, e) :
At type_indexededgelist.c:269 : invalid (odd) length of edges vector, Invalid edge vector
R 2.15.1> # error messag
R 2.15.1>
R 2.15.1> add.edges(g,E)[]
4 x 4 sparse Matrix of class "dgCMatrix"
[1,] . 1 . .
[2,] 1 . 1 1
[3,] . 1 . 1
[4,] . 1 1 .
R 2.15.1> # works
R 2.15.1>
R 2.15.1> g + E
Error in `+.igraph`(g, E) : Cannot add unknown type to igraph graph
R 2.15.1> # error
R 2.15.1>
R 2.15.1> (g + e)[]
5 x 5 sparse Matrix of class "dgCMatrix"
[1,] . . . . .
[2,] . . 1 1 .
[3,] . 1 . 1 .
[4,] . 1 1 . .
[5,] . . . . .
R 2.15.1> # adds a node ???
R 2.15.1>