## S3 method for class 'igraph':
[(x, i, j, \dots, from, to,
sparse=getIgraphOpt("sparsematrices"),
edges=FALSE, drop=TRUE,
attr=if (is.weighted(x)) "weight" else NULL)
## S3 method for class 'igraph':
[[(x, i, j, \dots, directed=TRUE, edges=FALSE, exact=TRUE)
## S3 method for class 'igraph':
[(x, i, j, \dots, from, to,
attr=if (is.weighted(x)) "weight" else NULL) <- value ## S3 method for class 'igraph':
+(e1, e2)
## S3 method for class 'igraph':
-(e1, e2)
vertex(...)
vertices(...)
edge(...)
edges(...)
path(...)
add.edges(graph, edges, ..., attr=list())
add.vertices(graph, nv, ..., attr=list())
delete.edges(graph, edges)
delete.vertices(graph, v)
vertex, vertices, edge, edges and
path see details below.
For add.edges and add.vertices theto argument, it can be used to query/set
a sequence of edges. See details below. This argument cannot be present together with any of the
i
from argument, it can be used to query/set
a sequence of edges. See details below. This argument cannot be present together with any of the
i
NULL. If
FALSE, NULL or zero, then the specified edges will be
deleted. If TRUE or a non-zero numeric value, then the
specified edges will be added. (Only ifNULL, then it
should be the name of an edge attribute. This attribute is queried,
or updated to the given value. For add.edges and
add.vertices: additional edge/verte[[[
fromandtoarguments can be used to check
the existence of many edges. In this case, bothfromandtomust be present and they must have the same length. They
must contain vertex ids or names. A numeric vector is returned, of
the same length asfromandto, it contains ones
for existing edges edges and zeros for non-existing ones.
Example:graph[from=1:3, to=c(2,3,5)].[operator returns the edge
weights. For non-esistent edges zero weights are returned. Other
edge attributes can be queried as well, by giving theattrargument.TRUE):graph[1, 2] <- 1
graph[1:3,1] <- 1
graph[from=1:3, to=c(2,3,5)] <- TRUEThis does not affect edges that are already present in the graph,
i.e. no multiple edges are created.attrargument
contains the name of the edge attribute to set, so it does not
have to beattr="weight"setting is implicit, and
one does not need to give it explicitly.FALSEorNULLas the
replacement value:graph[v, w] <- FALSEremoves the edge from vertex$v$to vertex$w$.
As this can be used to delete edges between two sets of vertices,
either pairwise:graph[from=v, to=w] <- FALSEor not:graph[v, w] <- FALSEif$v$and$w$are vectors of edge ids or names.The double bracket operator indexes the (imaginary) adjacency list of the graph. This can used for the following operations:
edgesargument is set toTRUE:graph[[1:3, , edges=TRUE]]
graph[[, 1:3, edges=TRUE]] Both the [[[
Of course, the indexing operators support vertex names,
so instead of a numeric vertex id a vertex can also be given to
[[[
graph.disjoint.union.vertexorverticesfunction, then new vertices are added to the
graph. This form is appropriate when one wants to add some vertex
attributes as well. The operands of theverticesfunction
specifies the number of vertices to add and their attributes as
well. The unnamed arguments ofverticesare concatenated and
used as thenamevertexis just an alias tovertices, and it is
provided for readability. The user should use it if a single vertex
is added to the graph.
edgeoredgesfunction, then new edges will be added to the graph. The new edges
and possibly their attributes can be specified as the arguments of
theedgesfunction. The unnamed arguments ofedgesare concatenated and used
as vertex ids of the end points of the new edges. The named
arguments will be added as edge attributes.
Examples:g <- graph.empty() + vertices(letters[1:10]) +
vertices("foo", "bar", "bar2", "foobar2")
g <- g + edge("a", "b")
g <- g + edges("foo", "bar", "bar2", "foobar2")
g <- g + edges(c("bar", "foo", "foobar2", "bar2"), color="red", weight=1:2)See more examples below.edgeis just an alias toedgesand it is provided
for readability. The user should use it if a single edge is added to
the graph.
pathfunction, then
new edges that form a path are added. The edges and possibly their
attributes are specified as the arguments to thepathfunction. The non-named arguments are concatenated and interpreted
as the vertex ids along the path. The remaining arguments are added
as edge attributes.Examples:g <- graph.empty() + vertices(letters[1:10]) g <- g + path("a", "b", "c", "d") g <- g + path("e", "f", "g", weight=1:2, color="red") g <- g + path(c("f", "c", "j", "d"), width=1:3, color="green")
It is important to note that, although the plus operator is commutative, i.e. is possible to write graph <- "foo" + graph.empty() it is not associative, e.g. graph <- "foo" + "bar" + graph.empty() results a syntax error, unless parentheses are used: graph <- "foo" + ( "bar" + graph.empty() ) For clarity, we suggest to always put the graph object on the left hand side of the operator: graph <- graph.empty() + "foo" + "bar"
-graph.difference.e2is a vertex sequence (e.g. created by theVfunction), then these vertices will be deleted from
the graph.Efunction), then these edges will be deleted from the graph.vertex(or thevertices) function, then all arguments ofverticesare
concatenated and the result is interpreted as a vector of vertex
ids. These vertices will be removed from the graph.edge(or theedges) function, then all arguments ofedgesare
concatenated and then interpreted as edges to be removed from the
graph.
Example:g <- graph.ring(10)
V(g)$name <- letters[1:10]
E(g)$name <- LETTERS[1:10]
g <- g - edge("e|f")
g <- g - edge("H")pathfunction,
then allpatharguments are concatenated and then interpreted
as a path along which edges will be removed from the graph.
Example:g <- graph.ring(10)
V(g)$name <- letters[1:10]
g <- g - path("a", "b", "c", "d")add.edges adds the specified edges to the graph. The ids of the
vertices are preserved. The additionally supplied named arguments will
be added as edge attributes for the new edges. If an attribute was not
present in the original graph, its value for the original edges will
be NA. add.vertices adds the specified number of isolate vertices to
the graph. The ids of the old vertices are preserved. The additionally
supplied named arguments will be added as vertex attributes for the
new vertices. If an attribute was not present in the original graph,
its value is set to NA for the original vertices.
delete.edges removes the specified edges from the graph. If a
specified edge is not present, the function gives an error message,
and the original graph remains unchanged.
The ids of the vertices are preserved.
delete.vertices removes the specified vertices from the graph
together with their adjacent edges. The ids of the vertices are
not preserved.
[[[[) or an adjacency
list ([). The single bracket indexes the (possibly weighted)
adjacency matrix of the graph. The double bracket operator is
similar, but queries the adjacencly list of the graph. The details
on how to use the indexing operators are
discussed below. The addition (+-
In addition, the four functions, add.vertices, add.edges,
delete.vertices and delete.edges can also be used
to manipulate the structure.
# 10 vertices named a,b,c,... and no edges
g <- graph.empty() + vertices(letters[1:10])
# Add edges to make it a ring
g <- g + path(letters[1:10], letters[1], color="grey")
# Add some extra random edges
g <- g + edges(sample(V(g), 10, replace=TRUE), color="red")
g$layout <- layout.circle
if (interactive()) {
plot(g)
}
# The old-style operations
g <- graph.ring(10)
add.edges(g, c(2,6,3,7) )
delete.edges(g, E(g, P=c(1,10, 2,3)) )
delete.vertices(g, c(2,7,8) )Run the code above in your browser using DataLab