R igraph manual pages

Use this if you are using igraph from R

Convert a graph to an adjacency matrix


Sometimes it is useful to work with a standard representation of a graph, like an adjacency matrix.


as_adjacency_matrix(graph, type = c("both", "upper", "lower"),
  attr = NULL, edges = FALSE, names = TRUE,
  sparse = igraph_opt("sparsematrices"))

as_adj(graph, type = c("both", "upper", "lower"), attr = NULL,
  edges = FALSE, names = TRUE, sparse = igraph_opt("sparsematrices"))



The graph to convert.


Gives how to create the adjacency matrix for undirected graphs. It is ignored for directed graphs. Possible values: upper: the upper right triangle of the matrix is used, lower: the lower left triangle of the matrix is used. both: the whole matrix is used, a symmetric matrix is returned.


Either NULL or a character string giving an edge attribute name. If NULL a traditional adjacency matrix is returned. If not NULL then the values of the given edge attribute are included in the adjacency matrix. If the graph has multiple edges, the edge attribute of an arbitrarily chosen edge (for the multiple edges) is included. This argument is ignored if edges is TRUE.

Note that this works only for certain attribute types. If the sparse argumen is TRUE, then the attribute must be either logical or numeric. If the sparse argument is FALSE, then character is also allowed. The reason for the difference is that the Matrix package does not support character sparse matrices yet.


Logical scalar, whether to return the edge ids in the matrix. For non-existant edges zero is returned.


Logical constant, whether to assign row and column names to the matrix. These are only assigned if the name vertex attribute is present in the graph.


Logical scalar, whether to create a sparse matrix. The ‘Matrix’ package must be installed for creating sparse matrices.


as_adjacency_matrix returns the adjacency matrix of a graph, a regular matrix if sparse is FALSE, or a sparse matrix, as defined in the ‘Matrix’ package, if sparse if TRUE.


A vcount(graph) by vcount(graph) (usually) numeric matrix.

See Also

graph_from_adjacency_matrix, read_graph


g <- sample_gnp(10, 2/10)
V(g)$name <- letters[1:vcount(g)]
E(g)$weight <- runif(ecount(g))
as_adjacency_matrix(g, attr="weight")

[Package igraph version 1.2.3 Index]