EVOLUTION-MANAGER
Edit File: graph_from_adjacency_matrix.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Create graphs from adjacency matrices</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for graph_from_adjacency_matrix {igraph}"><tr><td>graph_from_adjacency_matrix {igraph}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Create graphs from adjacency matrices</h2> <h3>Description</h3> <p><code>graph_from_adjacency_matrix</code> is a flexible function for creating <code>igraph</code> graphs from adjacency matrices. </p> <h3>Usage</h3> <pre> 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(...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>adjmatrix</code></td> <td> <p>A square adjacency matrix. From igraph version 0.5.1 this can be a sparse matrix created with the <code>Matrix</code> package.</p> </td></tr> <tr valign="top"><td><code>mode</code></td> <td> <p>Character scalar, specifies how igraph should interpret the supplied matrix. See also the <code>weighted</code> argument, the interpretation depends on that too. Possible values are: <code>directed</code>, <code>undirected</code>, <code>upper</code>, <code>lower</code>, <code>max</code>, <code>min</code>, <code>plus</code>. See details below.</p> </td></tr> <tr valign="top"><td><code>weighted</code></td> <td> <p>This argument specifies whether to create a weighted graph from an adjacency matrix. If it is <code>NULL</code> 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 <code>weighted</code> argument. If it is <code>TRUE</code> then a weighted graph is created and the name of the edge attribute will be <code>weight</code>. See also details below.</p> </td></tr> <tr valign="top"><td><code>diag</code></td> <td> <p>Logical scalar, whether to include the diagonal of the matrix in the calculation. If this is <code>FALSE</code> then the diagonal is zerod out first.</p> </td></tr> <tr valign="top"><td><code>add.colnames</code></td> <td> <p>Character scalar, whether to add the column names as vertex attributes. If it is ‘<code>NULL</code>’ (the default) then, if present, column names are added as vertex attribute ‘name’. If ‘<code>NA</code>’ then they will not be added. If a character constant, then it gives the name of the vertex attribute to add.</p> </td></tr> <tr valign="top"><td><code>add.rownames</code></td> <td> <p>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 ‘<code>add.rownames</code>’ and ‘<code>add.colnames</code>’ specify the same vertex attribute, then the former is ignored.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>Passed to <code>graph_from_adjacency_matrix</code>.</p> </td></tr> </table> <h3>Details</h3> <p>The order of the vertices are preserved, i.e. the vertex corresponding to the first row will be vertex 0 in the graph, etc. </p> <p><code>graph_from_adjacency_matrix</code> operates in two main modes, depending on the <code>weighted</code> argument. </p> <p>If this argument is <code>NULL</code> 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 <code>mode</code> argument: </p> <dl> <dt>"directed"</dt><dd><p>The graph will be directed and a matrix element gives the number of edges between two vertices.</p> </dd> <dt>"undirected"</dt><dd><p>This is exactly the same as <code>max</code>, for convenience. Note that it is <em>not</em> checked whether the matrix is symmetric.</p> </dd> <dt>"max"</dt><dd><p>An undirected graph will be created and <code>max(A(i,j), A(j,i))</code> gives the number of edges.</p> </dd> <dt>"upper"</dt><dd><p>An undirected graph will be created, only the upper right triangle (including the diagonal) is used for the number of edges.</p> </dd> <dt>"lower"</dt><dd><p>An undirected graph will be created, only the lower left triangle (including the diagonal) is used for creating the edges.</p> </dd> <dt>"min"</dt><dd><p>undirected graph will be created with <code>min(A(i,j), A(j,i))</code> edges between vertex <code>i</code> and <code>j</code>.</p> </dd> <dt>"plus"</dt><dd> <p>undirected graph will be created with <code>A(i,j)+A(j,i)</code> edges between vertex <code>i</code> and <code>j</code>.</p> </dd> </dl> <p>If the <code>weighted</code> argument is not <code>NULL</code> 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 <code>mode</code> argument: </p> <dl> <dt>"directed"</dt><dd><p>The graph will be directed and a matrix element gives the edge weights.</p> </dd> <dt>"undirected"</dt><dd><p>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.</p> </dd> <dt>"max"</dt><dd><p>An undirected graph will be created and <code>max(A(i,j), A(j,i))</code> gives the edge weights.</p> </dd> <dt>"upper"</dt><dd><p>An undirected graph will be created, only the upper right triangle (including the diagonal) is used (for the edge weights).</p> </dd> <dt>"lower"</dt><dd><p>An undirected graph will be created, only the lower left triangle (including the diagonal) is used for creating the edges.</p> </dd> <dt>"min"</dt><dd><p>An undirected graph will be created, <code>min(A(i,j), A(j,i))</code> gives the edge weights.</p> </dd> <dt>"plus"</dt><dd><p>An undirected graph will be created, <code>A(i,j)+A(j,i)</code> gives the edge weights.</p> </dd> </dl> <h3>Value</h3> <p>An igraph graph object. </p> <h3>Author(s)</h3> <p>Gabor Csardi <a href="mailto:csardi.gabor@gmail.com">csardi.gabor@gmail.com</a> </p> <h3>See Also</h3> <p><a href="make_graph.html">graph</a> and <code><a href="graph_from_literal.html">graph_from_literal</a></code> for other ways to create graphs. </p> <h3>Examples</h3> <pre> adjm <- matrix(sample(0:1, 100, replace=TRUE, prob=c(0.9,0.1)), ncol=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)), ncol=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) </pre> <hr /><div style="text-align: center;">[Package <em>igraph</em> version 1.3.5 <a href="00Index.html">Index</a>]</div> </body></html>