Learn R Programming

cograph (version 2.7.2)

spanning_tree: Minimum or Maximum Spanning Tree

Description

Prim's algorithm on each connected component, so a disconnected network yields a spanning forest.

Usage

spanning_tree(
  x,
  weights = c("weight", "none"),
  maximum = FALSE,
  keep_format = FALSE,
  directed = NULL
)

Value

An undirected cograph_network holding the spanning tree (or forest), or the input format when keep_format = TRUE. Every node is kept.

Arguments

x

Network input.

weights

"weight" (default) uses the edge weights as costs; "none" treats every edge as cost 1.

maximum

Logical. Find the maximum spanning tree instead of the minimum. Default FALSE. Set TRUE when the weights are similarities.

keep_format

Logical. Return the input format when TRUE.

directed

Logical or NULL. Directedness to read the input with; the tree itself is undirected.

References

Prim, R. C. (1957). Shortest connection networks and some generalizations. Bell System Technical Journal, 36(6), 1389--1401.

See Also

disparity_filter, threshold_edges

Examples

Run this code
adj <- matrix(c(0, .5, .8, 0,
                .5, 0, .3, .6,
                .8, .3, 0, .4,
                 0, .6, .4, 0), 4, 4, byrow = TRUE)
rownames(adj) <- colnames(adj) <- c("A", "B", "C", "D")

spanning_tree(adj)
spanning_tree(adj, maximum = TRUE)

Run the code above in your browser using DataLab