Skip to contents

given a graph and a query, creates a sub graph with the node of reference and its Nth neighbors

Usage

get_neighbors(graph, query, n = 1)

Arguments

graph

a tibble graph created with tidygraph::tbl_graph() or converted with tidygraph::as_tbl_graph()

query

a query to be searched

n

number of node neighbors since the query

Examples

# Building a graph dataframe
# Node data frame:
nodes <- data.frame(name = c("Alice", "Bob", "Charlie", "David", "John", "Mary"))
edges <- data.frame(from = c(1, 1, 2, 3, 2, 6), to = c(2, 3, 4, 4, 5, 5))
g <- tidygraph::tbl_graph(nodes = nodes, edges = edges)
g
#> # A tbl_graph: 6 nodes and 6 edges
#> #
#> # A directed acyclic simple graph with 1 component
#> #
#> # Node Data: 6 × 1 (active)
#>   name   
#>   <chr>  
#> 1 Alice  
#> 2 Bob    
#> 3 Charlie
#> 4 David  
#> 5 John   
#> 6 Mary   
#> #
#> # Edge Data: 6 × 2
#>    from    to
#>   <int> <int>
#> 1     1     2
#> 2     1     3
#> 3     2     4
#> # ℹ 3 more rows
get_neighbors(g, "Charlie", 1)
#> # A tbl_graph: 3 nodes and 2 edges
#> #
#> # A rooted tree
#> #
#> # Edge Data: 2 × 3 (active)
#>    from    to .tidygraph_edge_index
#>   <int> <int>                 <int>
#> 1     1     2                     2
#> 2     2     3                     4
#> #
#> # Node Data: 3 × 2
#>   name    .tidygraph_node_index
#>   <chr>                   <int>
#> 1 Alice                       1
#> 2 Charlie                     3
#> 3 David                       4
get_neighbors(g, "Charlie", 2)
#> # A tbl_graph: 4 nodes and 4 edges
#> #
#> # A directed acyclic simple graph with 1 component
#> #
#> # Edge Data: 4 × 3 (active)
#>    from    to .tidygraph_edge_index
#>   <int> <int>                 <int>
#> 1     1     2                     1
#> 2     1     3                     2
#> 3     2     4                     3
#> 4     3     4                     4
#> #
#> # Node Data: 4 × 2
#>   name    .tidygraph_node_index
#>   <chr>                   <int>
#> 1 Alice                       1
#> 2 Bob                         2
#> 3 Charlie                     3
#> # ℹ 1 more row
get_neighbors(g, "Alice", 1)
#> # A tbl_graph: 3 nodes and 2 edges
#> #
#> # A rooted tree
#> #
#> # Edge Data: 2 × 3 (active)
#>    from    to .tidygraph_edge_index
#>   <int> <int>                 <int>
#> 1     1     2                     1
#> 2     1     3                     2
#> #
#> # Node Data: 3 × 2
#>   name    .tidygraph_node_index
#>   <chr>                   <int>
#> 1 Alice                       1
#> 2 Bob                         2
#> 3 Charlie                     3
get_neighbors(g, "Alice", 2)
#> # A tbl_graph: 5 nodes and 5 edges
#> #
#> # A directed acyclic simple graph with 1 component
#> #
#> # Edge Data: 5 × 3 (active)
#>    from    to .tidygraph_edge_index
#>   <int> <int>                 <int>
#> 1     1     2                     1
#> 2     1     3                     2
#> 3     2     4                     3
#> 4     3     4                     4
#> 5     2     5                     5
#> #
#> # Node Data: 5 × 2
#>   name    .tidygraph_node_index
#>   <chr>                   <int>
#> 1 Alice                       1
#> 2 Bob                         2
#> 3 Charlie                     3
#> # ℹ 2 more rows