Learn R Programming

DiagrammeR (version 0.8.4)

get_connected_components: Get all nodes associated with connected components

Description

Determine which nodes in a graph belong to different connected components (i.e., distinct sets of nodes with traversable paths to and from each node in the set).

Usage

get_connected_components(graph, return_type = "df")

Arguments

graph
a graph object of class dgr_graph that is created using create_graph.
return_type
using df (the default), a data frame containing the component ID and the nodes associated with each connected component is provided. With list, an anologous list object is provided.

Value

a data frame or a list object, depending on the value given to return_type.

Examples

Run this code
## Not run: 
# library(magrittr)
# 
# # Create a random graph
# graph <-
#   create_random_graph(36, 50, set_seed = 1)
# 
# # Check if the graph is connected
# is_graph_connected(graph)
# #> [1] TRUE
# 
# # Modify the graph so that it becomes disconnected
# # (with two clusters of nodes)
# graph <-
#   graph %>%
#   delete_edge(10, 36) %>%
#   delete_edge(25, 27) %>%
#   delete_edge(28, 29) %>%
#   delete_edge(4, 29) %>%
#   delete_edge(24, 32)
# 
# # Verify that the graph is disconnected
# is_graph_connected(graph)
# #> [1] FALSE
# 
# # Get the graph's connected components
# get_connected_components(graph)
# #>    component node
# #> 1          1    1
# #> 2          1    3
# #> 3          1    4
# #> 4          1    5
# #> 5          1    6
# #> 6          1    7
# #> 7          1    8
# #> 8          1    9
# #> 9          1   11
# #> 10         1   14
# #> ..         ..  ..
# ## End(Not run)

Run the code above in your browser using DataLab