View source: R/find_minimum_spanning_edges.R
find_minimum_spanning_edges | R Documentation |
Returns edges of a minimum spanning tree given a distance matrix.
find_minimum_spanning_edges(distance_matrix)
distance_matrix |
A square matrix of distances between objects. |
This function is a wrapper for mst in the ape package, but returns a vector of edges rather than a square matrix of links.
A vector of named edges (X->Y) with their distances. The sum of this vector is the length of the minimum spanning tree.
Graeme T. Lloyd graemetlloyd@gmail.com
# Create a simple square matrix of distances:
distance_matrix <- matrix(c(0, 1, 2, 3, 1, 0, 1, 2, 2, 1, 0, 1, 3, 2, 1, 0),
nrow = 4,
dimnames = list(LETTERS[1:4], LETTERS[1:4])
)
# Show matrix to confirm that the off diagonal has the shortest
# distances:
distance_matrix
# Use find_minimum_spanning_edges to get the edges for the minimum spanning
# tree:
find_minimum_spanning_edges(distance_matrix)
# Use sum of find_minimum_spanning_edges to get the length of the minimum
# spanning tree:
sum(find_minimum_spanning_edges(distance_matrix))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.