Create a graph consisting of separate components

create_components_graph(
  n_vertices,
  directed = FALSE,
  membership = NULL,
  graph = c("igraph", "network", "matrix")
)

Arguments

n_vertices

a single number for a one-mode graph, a vector with two numbers for a two-mode graph

directed

logical, should the connections be directed or not (default)

membership

numeric vector, with the group membership for each vertex

graph

output class, either "igraph" (the default), "network", or "matrix"

Value

a graph of the specified class

Details

Creates a graph where only vertices (and all) vertices are connected that are a member of the ame component.

If n_vertices has length 1, a one-mode graph is created. Each component has internal density 1 and there are no connections between the components.

If n_vertices has length 1, a two-mode graph is created. An edge between two vertices only occurs for between vertices that are both of dissimilar type and and both in thesame component. Regardless of the choice for directed, the ensuing graph will be undirected by definition.

The vertices in the final graph will be numerically named, from 1 to n_vertices for one-mode graphs or from 1 to sum(n_vertices) for two-mode graphs.

Note

Much of the code comes from create_components in the manynet package.

Examples

create_components_graph(10, membership = c(1,1,1,2,2,2,3,3,3,3))
#> IGRAPH c81f127 UN-- 10 12 -- 
#> + attr: name (v/c)
#> + edges from c81f127 (vertex names):
#>  [1] 1--2  1--3  2--3  4--5  4--6  5--6  7--8  7--9  7--10 8--9  8--10 9--10
create_components_graph(10, membership = c(1,1,1,2,2,2,3,3,3,3), directed = TRUE) 
#> IGRAPH c8200bb DN-- 10 12 -- 
#> + attr: name (v/c)
#> + edges from c8200bb (vertex names):
#>  [1] 1->2  1->3  2->3  4->5  4->6  5->6  7->8  7->9  7->10 8->9  8->10 9->10

# bipartite
if (FALSE) { # \dontrun{
create_components_graph(c(4, 6), membership = c(1,1,1,2,2,2,3,3,3,3))  |> 
  snafun::plot()
} # }
# the row vertices are numbered 1-4, the column vertices are 5-10
create_components_graph(c(4, 6), membership = c(1,1,2,3,2,2,3,3,3,1), graph = "matrix")
#>   5 6 7 8 9 10
#> 1 0 0 0 0 0  1
#> 2 0 0 0 0 0  1
#> 3 1 1 0 0 0  0
#> 4 0 0 1 1 1  0