R/nei.R

Defines functions nei

Documented in nei

############################
############################
##### Return and plot if requested the neighbours of an undirected graph
#####
############################
############################
nei <- function(G, node) {
  ## G is the adjacency matrix of an UN-DIRECTED graph
  ## node is either one number between 1 and the number of nodes
  ## or more numbers, corresponding to two or more nodes
  ## it is a node whose neighbors you want to find
  ## it can also be a vector with more than one nodes
  if ( is.null( colnames(G) ) ) {
    p <- ncol(G)
    nama <- paste("X", 1:p, sep = "")
  } else  nama <- colnames(G)
   
  n <- length(node)
  if ( n == 1 ) {
    ind = which( G[node, ] == 1 ) 
    if ( length(ind) == 0 ) {
      geit <- paste( "The chosen node has no neighbours" );
    } else {
      ind <- as.vector( ind[ ind>0 ] )
      geit <- c(node, ind)
      names(geit) <- nama[ c(node, ind) ]
      Gnei <- G[ geit, geit ]
    }

  } else if ( n == 2 ) { 
    geit <- undir.path(G, node[1], node[2])
    geit <- unique( geit[geit > 0] )
    names(geit) <- nama[geit]
    Gnei <- G[geit, geit]    

  } else if ( n > 2 ) {
    posa <- t( combn(node, 2) )
    geit <- list()
    for ( i in 1:nrow(posa) ) {
      geiton <- undir.path(G, posa[i, 1], posa[i, 2])
      geit[[ i ]] <- unique( geiton[geiton > 0] )
    }
    geit <- unlist(geit)   
    geit <- unique( geit[geit > 0] )
    Gnei <- G[geit, geit]
    names(geit) <- nama[geit]
  }

  geit
}

Try the MXM package in your browser

Any scripts or data that you put into this service are public.

MXM documentation built on Aug. 25, 2022, 9:05 a.m.