## Create graphs from adjacency matrices

### Description

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

### Usage

```graph.adjacency(adjmatrix, mode=c("directed", "undirected", "max",
"min", "upper", "lower", "plus"), weighted=NULL, diag=TRUE)
```

### Arguments

 `adjmatrix` A square adjacency matrix. `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`, `lower`, `max`, `min`, `plus`. See details below. `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 character constant then for every non-zero matrix entry an edge is created and the value of the entry is added as an edge attribute named by the `weighted` argument. If it is `TRUE` then a weighted graph is created and the name of the edge attribute will be `weight`. See also details below. `diag` Logical scalar, whether to include the diagonal of the matrix in the calculation. If this is `FALSE` then the diagonal is zero-d out first.

### Details

`graph.adjacency` creates a graph from an adjacency matrix. It 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@rmki.kfki.hu

graph and `graph.formula` for other ways to create graphs.

### Examples

```adjm <- matrix(sample(0:1, 100, replace=TRUE, prob=c(0.9,0.1)), nc=10)
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])
mode="undirected")