\HeaderA{conversion}{Convert a graph to an adjacency matrix or an edge list}{conversion}
\aliasA{get.adjacency}{conversion}{get.adjacency}
\aliasA{get.edgelist}{conversion}{get.edgelist}
\keyword{graphs}{conversion}
\begin{Description}\relax
Sometimes it is useful to have a standard representation of
a graph, like an adjacency matrix or an edge list.
\end{Description}
\begin{Usage}
\begin{verbatim}
get.adjacency(graph, type=c("both", "upper", "lower"),
       attr=NULL, names=TRUE, binary=FALSE, sparse=FALSE)
get.edgelist(graph, names=TRUE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{graph}] The graph to convert.
\item[\code{type}] Gives how to create the adjacency matrix for undirected
graphs. It is ignored for directed graphs. Possible values:
\code{upper}: the upper right triangle of the matrix is used,
\code{lower}: the lower left triangle of the matrix is
used. \code{both}: the whole matrix is used, a symmetric matrix is
returned.
\item[\code{attr}] Either \code{NULL} or a character string giving an edge
attribute name. If \code{NULL} a traditional adjacency matrix is
returned. If not \code{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.
\item[\code{names}] Logical constant.

For \code{graph.adjacenct} it gives whether to assign row and column names
to the matrix. These are only assigned if the \code{name} vertex
attribute is present in the graph.

for \code{get.edgelist} it gives whether to return a character
matrix containing vertex names (ie. the \code{name} vertex
attribute) if they exist or numeric vertex ids.

\item[\code{binary}] Logical, whether to return a binary matrix. This argument
is ignored if \code{attr} is not \code{NULL}.
\item[\code{sparse}] Logical scalar, whether to create a sparse matrix. The
\sQuote{\code{Matrix}} package must be installed for creating sparse
matrices.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
\code{get.adjacency} returns the adjacency matrix of a graph, a
regular \R{} matrix if \code{sparse} is \code{FALSE}, or a sparse
matrix, as defined in the \sQuote{\code{Matrix}} package, if
\code{sparse} if \code{TRUE}.

\code{get.edgelist} returns the list of edges in a graph.
\end{Details}
\begin{Value}
A \code{vcount(graph)} by \code{vcount(graph)} (usually) numeric
matrix for \code{get.adjacency}. (This can be huge!) Note that a
non-numeric matrix might be returned if \code{attr} is a non-numeric
edge attribute.

A \code{ecount(graph)} by 2 numeric matrix for \code{get.edgelist}.
\end{Value}
\begin{Author}\relax
Gabor Csardi \email{csardi@rmki.kfki.hu}
\end{Author}
\begin{SeeAlso}\relax
\code{\LinkA{graph.adjacency}{graph.adjacency}}, \code{\LinkA{read.graph}{read.graph}}
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
g <- erdos.renyi.game(10, 2/10)
get.edgelist(g)
get.adjacency(g)
V(g)$name <- letters[1:vcount(g)]
get.adjacency(g)
E(g)$weight <- runif(ecount(g))
get.adjacency(g, attr="weight")
\end{ExampleCode}
\end{Examples}

