# R igraph manual pages

Use this if you are using igraph from R

## Create graphs from adjacency matrices

### Description

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

### Usage

mode = c("directed", "undirected", "max", "min", "upper", "lower", "plus"),
weighted = NULL,
diag = TRUE,
)

### 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:

"directed"

The graph will be directed and a matrix element gives the number of edges between two vertices.

"undirected"

This is exactly the same as max, for convenience. Note that it is not checked whether the matrix is symmetric.

"max"

An undirected graph will be created and max(A(i,j), A(j,i)) gives the number of edges.

"upper"

An undirected graph will be created, only the upper right triangle (including the diagonal) is used for the number of edges.

"lower"

An undirected graph will be created, only the lower left triangle (including the diagonal) is used for creating the edges.

"min"

undirected graph will be created with min(A(i,j), A(j,i)) edges between vertex i and j.

"plus"

undirected graph will be created with A(i,j)+A(j,i) edges between vertex i and j.

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:

"directed"

The graph will be directed and a matrix element gives the edge weights.

"undirected"

First we check that the matrix is symmetric. It is an error if not. Then only the upper triangle is used to create a weighted undirected graph.

"max"

An undirected graph will be created and max(A(i,j), A(j,i)) gives the edge weights.

"upper"

An undirected graph will be created, only the upper right triangle (including the diagonal) is used (for the edge weights).

"lower"

An undirected graph will be created, only the lower left triangle (including the diagonal) is used for creating the edges.

"min"

An undirected graph will be created, min(A(i,j), A(j,i)) gives the edge weights.

"plus"

An undirected graph will be created, A(i,j)+A(j,i) gives the edge weights.

### Value

An igraph graph object.

### Author(s)

Gabor Csardi csardi.gabor@gmail.com

graph and graph_from_literal for other ways to create graphs.

### Examples

adjm <- matrix(sample(0:1, 100, replace=TRUE, prob=c(0.9,0.1)), nc=10)
adjm <- matrix(sample(0:5, 100, replace=TRUE,
prob=c(0.9,0.02,0.02,0.02,0.02,0.02)), nc=10)
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))

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))

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))