Learn R Programming

Claddis (version 0.3.4)

MinSpanTreeEdges: Get edges of minimum spanning tree

Description

Returns edges of a minimum spanning tree given a distance matrix.

Usage

MinSpanTreeEdges(dist.matrix)

Arguments

dist.matrix

A square matrix of distances between objects.

Value

A vector of named edges (X->Y) with their distances. The sum of this vector is the length of the minimum spanning tree.

Details

This function is a wrapper for mst in the ape package, but returns a vector of edges rather than a square matrix of links.

Examples

Run this code
# NOT RUN {
# Create a simple square matrix of distances:
dist.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:
dist.matrix

# Use MinSpanTreeEdges to get the edges for the minimum spanning
# tree:
MinSpanTreeEdges(dist.matrix)

# Use sum of MinSpanTreeEdges to get the length of the minimum
# spanning tree:
sum(MinSpanTreeEdges(dist.matrix))

# }

Run the code above in your browser using DataLab