Learn R Programming

DiagrammeR (version 0.8.4)

get_all_connected_nodes: Get all nodes connected to a specified node

Description

With a single node serving as the starting point get all nodes connected (i.e., reachable with a traversible path) to that node.

Usage

get_all_connected_nodes(graph, node)

Arguments

graph
a graph object of class dgr_graph that is created using create_graph.
node
a single-length vector containing a node ID value.

Value

a vector of node ID values.

Examples

Run this code
library(magrittr)

# This graph, created using `create_random_graph()`
# is almost fully connected but there is an
# isolated node (`13`) with no edges
graph_1 <-
  create_random_graph(
    30, 50, set_seed = 1)

# There won't be any connected nodes to `13` so when
# specifying this isolated node with
# `get_all_connected_nodes()` will return NA
graph_1 %>% get_all_connected_nodes(13)
#> [1] NA

# Any other node in `graph_1` will provide a vector
# of all the nodes other than `13`
graph_1 %>% get_all_connected_nodes(2)
#> [1] "1"  "3"  "4"  "5"  "6"  "7"  "8"  "9"
#> [9] "10" "11" "12" "14" "15" "16" "17" "18"
#> [17] "19" "20" "21" "22" "23" "24" "25" "26"
#> [25] "27" "28" "29" "30"

# The following graph is fully connected
graph_2 <-
  create_random_graph(
    36, 50, set_seed = 1)

# The following graph has two clusters of nodes
# (i.e., the graph has two connected components)
graph_2 <-
  create_random_graph(
    36, 50, set_seed = 1) %>%
  delete_edge(10, 36) %>%
  delete_edge(25, 27) %>%
  delete_edge(28, 29) %>%
  delete_edge(4, 29) %>%
  delete_edge(24, 32)

# In `graph_2`, node `1` is in the larger of the two
# connected components
graph_2 %>% get_all_connected_nodes(1)
#> [1] "3"  "4"  "5"  "6"  "7"  "8"  "9"  "11"
#> [9] "14" "16" "18" "19" "21" "22" "23" "25"
#> [17] "26" "28" "30" "32" "33" "34" "35" "36"

# Also in `graph_2`, node `2` is in the smaller of
# the two connected components
graph_2 %>% get_all_connected_nodes(2)
#> [1] "10" "12" "13" "15" "17" "20" "24" "27"
#> [9] "29" "31"

Run the code above in your browser using DataLab