Coercions between matrix and graph representationspackage:graphR Documentation

_C_o_e_r_c_i_o_n_s _b_e_t_w_e_e_n _m_a_t_r_i_x _a_n_d _g_r_a_p_h _r_e_p_r_e_s_e_n_t_a_t_i_o_n_s

_D_e_s_c_r_i_p_t_i_o_n:

     A collection of functions and methods to convert various forms of
     matrices into graph objects.

_U_s_a_g_e:

     aM2bpG(aM)
     ftM2adjM(ft, W=NULL, V=NULL, edgemode="directed")
     ftM2graphNEL(ft, W=NULL, V=NULL, edgemode="directed")
     ## S4 method for signature 'graphNEL, matrix':
     coerce(g,m)
     ## S4 method for signature 'matrix, graphNEL':
     coerce(m,g)

_A_r_g_u_m_e_n_t_s:

      ft: A 2xn or nx2 matrix containing the 'from/to' representation
          of graph edges.

       W: An optional vector of edge weights.

       V: An optional vector of node names.

      aM: An affiliation matrix for a bipartite graph.

edgemode: Character. Specifies if the resulting graph is to be directed
          or undirected.

       g: Object of class graphNEL.

       m: Matrix.

_D_e_t_a_i_l_s:

     In the functions 'ftM2adjM' and 'ftM2graphNEL', a 'from/to' matrix
     'ft' is converted into an 'adjacency' matrix or a 'graphNEL'
     object respectively.  If 'ft' is a '2xn' matrix, then the first
     row represents the 'from' nodes and the second row the 'to' nodes.
      If 'ft' is an 'nx2' matrix, then the first column represents the
     'from' nodes and the second column the 'to' nodes.  If it is 2x2,
     then the 'from/to' nodes are taken to be represented by the rows.

     This representation does not allow for unconnected nodes except
     with the 'V' argument (see below). The 'edgemode' parameter can be
     used to specify if the desired output is a directed or undirected
     graph. 

     The same edge must not occur twice in the 'from/to' matrix. If
     'edgemode' is 'undirected', the edge '(u,v)' and '(v,u)' must only
     be specified once.

     'W' is an optional vector of edge weights.  The order of the edge
     weights in the vector should correspond to the order of the edges
     recorded in 'ft'.  If it is not specified, edge weights of 1 are
     assigned by default.

     'V' is an optional vector of node names.  All elements of 'ft'
     must be contained in 'V', but not all names in 'V' need to be
     contained in 'ft'.  If 'V' is not specified, it is set to all
     nodes represented in 'ft'.  Specifying 'V' is most useful for
     creating a graph that includes nodes with degree 0.

     'aM' is an affiliation matrix as frequently used in social
     networks analysis.  The rows of 'aM' represent actors, and the
     columns represent events.  An entry of "1" in the ith row and jth
     column represents affiliation of the ith actor with the jth event.
      Weighted entries may also be used.  'aM2bpG' returns a 'graphNEL'
     object with nodes consisting of the set of actors and events, and
     directed (possibly weighted) edges from the actors to their
     corresponding events.  If plotted using 'Rgraphviz' and the 'dot'
     layout, the bipartite structure of the graph returned by 'aM2bpG'
     should be evident. 

     An 'adjacency' matrix can be coerced into a 'graphNEL' using the
     'as' method.  If the matrix is a symmetric matrix, then the
     resulting graph will be 'undirected', otherwise it will be
     'directed'.

_V_a_l_u_e:

     For 'ftM2graphNEL' and 'aM2bpG', an object of class 'graphNEL'.
     For 'ftM2adjM', a matrix (the adjacency matrix representation).

_A_u_t_h_o_r(_s):

     Denise Scholtens, Wolfgang Huber

_E_x_a_m_p_l_e_s:

     ## From-To matrix

     From <- c("A","A","C","C")
     To   <- c("B","C","B","D")
     L <- cbind(From,To)

     W  <- 1:4
     M1 <- ftM2adjM(L, W, edgemode="directed")
     M2 <- ftM2adjM(L, W, edgemode="undirected")
     stopifnot(all(M1+t(M1)==M2))

     G1 <- ftM2graphNEL(L, W, edgemode="directed")
     G2 <- ftM2graphNEL(L, W, edgemode="undirected")

     ## Adjacency matrix

     From <- matrix(runif(100), nrow=10, ncol=10)
     From <- (From+t(From)) > pi/4
     rownames(From) <- colnames(From) <- LETTERS[1:10]

     To <- as(From,"graphNEL")
     Back <- as(To,"matrix")

     stopifnot(all(From == Back))

