Coercion methods between graphs and sparse matrices
These functions provide coercions between objects that inherit from
graph class to sparse matrices from the
graph2SparseM(g, useweights=FALSE) sparseM2Graph(sM, nodeNames, edgemode=c("directed", "undirected"))
- An instance of the
- A logical value indicating whether to use the edge weights in the graph as values in the sparse matrix.
- A sparse matrix.
charactervector of the node names.
- Specifies whether the graph to be created should have
directed (default) or undirected edges. If undirected, the input
sMmust be symmetric.
A very simple coercion from one representation to another.
Currently it is presumed that the matrix is square. For other graph formats, such as bipartite graphs, some improvements will be needed; patches are welcome.
graph2SparseMtakes as input an instance of a subclass of the
graphclass and returns a sparse matrix.
sparseM2Graphtakes a sparse matrix as input and returns an instance of the
graphNELclass. By default, the
graphNELreturned will have directed edges.
set.seed(123) g1 <- randomGraph(letters[1:10], 1:4, p=.3) s1 <- graph2SparseM(g1, useweights=TRUE) g2 <- sparseM2Graph(s1, letters[1:10], edgemode="undirected") ## consistency check stopifnot(all.equal(g1, g2))