R/get_non_nbrs.R

Defines functions get_non_nbrs

Documented in get_non_nbrs

#' Get non-neighbors of a node in a graph
#'
#' Get the set of all nodes not neighboring a single graph node.
#'
#' @inheritParams render_graph
#' @param node A single-length vector containing a node ID value.
#'
#' @return A vector of node ID values.
#'
#' @examples
#' # Create a simple, directed graph with 5
#' # nodes and 4 edges
#' graph <-
#'   create_graph() %>%
#'   add_path(n = 5)
#'
#' # Find all non-neighbors of node `2`
#' graph %>% get_non_nbrs(node = 2)
#'
#' @export
get_non_nbrs <- function(graph,
                         node) {

  # Get the name of the function
  fcn_name <- get_calling_fcn()

  # Validation: Graph object is valid
  if (graph_object_valid(graph) == FALSE) {

    emit_error(
      fcn_name = fcn_name,
      reasons = "The graph object is not valid")
  }

  # Get predecessors and successors for the `node`
  node_nbrs <-
    c(get_predecessors(graph, node = node),
      get_successors(graph, node = node))

  # Get all non-neighbors to the `node`
  node_non_nbrs <-
    base::setdiff(
      base::setdiff(get_node_ids(graph), node),
      node_nbrs)

  # Get a unique set of node ID values
  node_non_nbrs <- sort(unique(node_non_nbrs))

  # If there are no non-neighbors, then return `NA`
  if (length(node_non_nbrs) == 0) {
    return(NA)
  } else {
    return(node_non_nbrs)
  }
}
davidpassmore/pulse documentation built on March 19, 2022, 4:39 a.m.