Skip to contents

It returns the neighboring nodes of a specified node in a graph. The neighboring nodes can be returned in various formats, depending on the `return_type` parameter.

Usage

get_neighbors(
  adj_matrix,
  node_id,
  neighbor_type = c("both", "forward", "backward"),
  return_type = c("binary", "name", "id")
)

Arguments

adj_matrix

A square matrix (or dgCMatrix representing the background graph. Inside this adjacency matrix, each row and column of the matrix represents a node in the graph. The values of the matrix should be either 0 or 1 (or either 0 or larger than 0), where a value of 0 indicates no relations between two nodes. The diagonal of the matrix should be 0, as there are no self-edges in the graph.

node_id

The ID of the node whose neighbors are being retrieved. Node name is also acceptable.

neighbor_type

The `neighbor_type` parameter specifies the type of neighbors to include in the output: - **both**: return all neighbors of the specific node, regarding the graph as undirected. - **forward**: return only the neighbors that are reachable from the specified node, specifically means downstream neighbors in the directed networks. - **backward**: return the upstream neighbors from the specific nodes. Specifically, it will reverse the direction of all edges in the graph and then return the downstream neighbors.

return_type

The `return_type` paramater specifies the type of return: - **binary**: return a binary vector of length equal to the total number of all nodes in the graph. The elements of vector will be set to 1 for nodes that are neighbors of the specific nodes, and 0 for all other nodes. - **name**: return a character vector containing the names of all neighbor nodes. - **id**: return a integer vector containing IDs of all neighbor nodes.

Value

A vector based on `return_type`.

References

https://www.ibm.com/docs/en/cognos-analytics/11.1.0? topic=terms-modified-z-score

See also

Examples

# make an adjacency matrix and randomly fill some cells with 1s
mat <- matrix(sample(c(0,1), 100, replace = TRUE), 10, 10)
diag(mat) <- 0 # remove self-loops

library(labyrinth)
# Get neighbors
get_neighbors(mat, 2)
#>  [1] 1 0 0 0 0 1 0 1 1 1