graph_from_adjacency_matrix

0th

Percentile

Create graphs from adjacency matrices

graph_from_adjacency_matrix is a flexible function for creating igraph graphs from adjacency matrices.

Keywords
graphs
Usage
graph_from_adjacency_matrix(adjmatrix, mode = c("directed", "undirected",
  "max", "min", "upper", "lower", "plus"), weighted = NULL, diag = TRUE,
  add.colnames = NULL, add.rownames = NA)

from_adjacency(...)

Arguments
adjmatrix
A square adjacency matrix. From igraph version 0.5.1 this can be a sparse matrix created with the Matrix package.
mode
Character scalar, specifies how igraph should interpret the supplied matrix. See also the weighted argument, the interpretation depends on that too. Possible values are: directed, undirected, upper,
weighted
This argument specifies whether to create a weighted graph from an adjacency matrix. If it is NULL then an unweighted graph is created and the elements of the adjacency matrix gives the number of edges between the vertices. If it is a charact
diag
Logical scalar, whether to include the diagonal of the matrix in the calculation. If this is FALSE then the diagonal is zerod out first.
add.colnames
Character scalar, whether to add the column names as vertex attributes. If it is NULL (the default) then, if present, column names are added as vertex attribute name. If NA then
add.rownames
Character scalar, whether to add the row names as vertex attributes. Possible values the same as the previous argument. By default row names are not added. If add.rownames and add.colnames specif
...
Passed to graph_from_adjacency_matrix.
Details

The order of the vertices are preserved, i.e. the vertex corresponding to the first row will be vertex 0 in the graph, etc.

graph_from_adjacency_matrix operates in two main modes, depending on the weighted argument.

If this argument is NULL then an unweighted graph is created and an element of the adjacency matrix gives the number of edges to create between the two corresponding vertices. The details depend on the value of the mode argument: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

If the weighted argument is not NULL then the elements of the matrix give the weights of the edges (if they are not zero). The details depend on the value of the mode argument: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Value

  • An igraph graph object.

See Also

graph and graph_from_literal for other ways to create graphs.

Aliases
  • from_adjacency
  • graph.adjacency
  • graph_from_adjacency_matrix
Examples
adjm <- matrix(sample(0:1, 100, replace=TRUE, prob=c(0.9,0.1)), nc=10)
g1 <- graph_from_adjacency_matrix( adjm )
adjm <- matrix(sample(0:5, 100, replace=TRUE,
                      prob=c(0.9,0.02,0.02,0.02,0.02,0.02)), nc=10)
g2 <- graph_from_adjacency_matrix(adjm, weighted=TRUE)
E(g2)$weight

## various modes for weighted graphs, with some tests
nzs <- function(x) sort(x [x!=0])
adjm <- matrix(runif(100), 10)
adjm[ adjm<0.5 ] <- 0
g3 <- graph_from_adjacency_matrix((adjm + t(adjm))/2, weighted=TRUE,
                      mode="undirected")

g4 <- graph_from_adjacency_matrix(adjm, weighted=TRUE, mode="max")
all(nzs(pmax(adjm, t(adjm))[upper.tri(adjm)]) == sort(E(g4)$weight))

g5 <- graph_from_adjacency_matrix(adjm, weighted=TRUE, mode="min")
all(nzs(pmin(adjm, t(adjm))[upper.tri(adjm)]) == sort(E(g5)$weight))

g6 <- graph_from_adjacency_matrix(adjm, weighted=TRUE, mode="upper")
all(nzs(adjm[upper.tri(adjm)]) == sort(E(g6)$weight))

g7 <- graph_from_adjacency_matrix(adjm, weighted=TRUE, mode="lower")
all(nzs(adjm[lower.tri(adjm)]) == sort(E(g7)$weight))

g8 <- graph_from_adjacency_matrix(adjm, weighted=TRUE, mode="plus")
d2 <- function(x) { diag(x) <- diag(x)/2; x }
all(nzs((d2(adjm+t(adjm)))[lower.tri(adjm)]) == sort(E(g8)$weight))

g9 <- graph_from_adjacency_matrix(adjm, weighted=TRUE, mode="plus", diag=FALSE)
d0 <- function(x) { diag(x) <- 0 }
all(nzs((d0(adjm+t(adjm)))[lower.tri(adjm)]) == sort(E(g9)$weight))

## row/column names
rownames(adjm) <- sample(letters, nrow(adjm))
colnames(adjm) <- seq(ncol(adjm))
g10 <- graph_from_adjacency_matrix(adjm, weighted=TRUE, add.rownames="code")
summary(g10)
Documentation reproduced from package igraph, version 1.0.0, License: GPL (>= 2)

Community examples

Looks like there are no examples yet.