getMinimumSpanningTree: Computes a minimum cost spanning tree

Description Usage Arguments Details Value References Examples

Description

Given a connected weighted undirected graph, getMinimumSpanningTree computes a minimum cost spanning tree. This function provides methods to find a minimum cost spanning tree with the three most commonly used algorithms: "Prim", "Kruskal" and "Boruvka".

Usage

1
2
getMinimumSpanningTree(nodes, arcs, algorithm, start.node = 1,
  show.data = TRUE, show.graph = TRUE, check.graph = FALSE)

Arguments

nodes

vector containing the nodes of the graph, identified by a number that goes from 1 to the order of the graph.

arcs

matrix with the list of arcs of the graph. Each row represents one arc. The first two columns contain the two endpoints of each arc and the third column contains their weights.

algorithm

denotes the algorithm used to find a minimum spanning tree: "Prim", "Kruskal" or "Boruvka".

check.graph

logical value indicating if it is necesary to check the graph. Is FALSE by default.

start.node

number which indicates the first node in Prim's algorithm. If none is specified node 1 is used by default.

show.data

logical value indicating if the function displays the console output (TRUE) or not (FALSE). The default is TRUE.

show.graph

logical value indicating if the function displays a graphical representation of the graph and its minimum spanning tree (TRUE) or not (FALSE). The default is TRUE.

Details

Given a connected weighted undirected graph, a minimum spanning tree is a spanning tree such that the sum of the weights of the arcs is minimum. There may be several minimum spanning trees of the same weight in a graph. Several algorithms were proposed to find a minimum spanning tree in a graph.

Prim's algorithm was developed in 1930 by the mathematician Vojtech Jarnik, independently proposed by the computer scientist Robert C. Prim in 1957 and rediscovered by Edsger Dijkstra in 1959. This is a greedy algorithm that can find a minimum spanning tree in a connected weighted undirected graph by adding minimum cost arcs leaving visited nodes recursively.

Kruskal's algorithm was published for first time in 1956 by mathematician Joseph Kruskal. This is a greedy algorithm that finds a minimum cost spanning tree in a connected weighted undirected graph by adding, without form cycles, the minimum weight arc of the graph in each iteration.

Boruvka's algorithm was published for first time in 1926 by mathematician Otakar Boruvka. This algorithm go through a connected weighted undirected graph, reviewing each component and adding the minimum weight arcs without repeat it until one minimum spanning tree is complete.

Value

getMinimumSpanningTree returns a list with:

tree.nodes

vector containing the nodes of the minimum cost spanning tree.

tree.arcs

matrix containing the list of arcs of the minimum cost spanning tree.

weight

value with the sum of weights of the arcs.

stages

number of stages required.

stages.arcs

stages in which each arc was added.

time

time needed to find the minimum cost spanning tree.

This function also represents the graph and the minimum spanning tree and prints to the console the results whit additional information (number of stages, computational time, etc.).

References

Prim, R. C. (1957), "Shortest Connection Networks And Some Generalizations", Bell System Technical Journal, 36 (1957), pp. 1389-1401.

Kruskal, Joshep B. (1956), "On the Shortest Spanning Subtree of a Graph and the Traveling Salesman Problem", Proceedings of the American Mathematical Society, Vol. 7, No. 1 (Feb., 1956), pp. 48-50.

Boruvka, Otakar (1926). "O jistem problemu minimalnim (About a certain minimal problem)". Prace mor. prirodoved. spol. v Brne III (in Czech, German summary) 3: 37-58.

Examples

1
2
3
4
5
6
7
8
# Graph
nodes <- 1:4
arcs <- matrix(c(1,2,2, 1,3,15, 1,4,3, 2,3,1, 2,4,9, 3,4,1),
               ncol = 3, byrow = TRUE)
# Minimum cost spanning tree with several algorithms
getMinimumSpanningTree(nodes, arcs, algorithm = "Prim")
getMinimumSpanningTree(nodes, arcs, algorithm = "Kruskal")
getMinimumSpanningTree(nodes, arcs, algorithm = "Boruvka")

Example output

Loading required package: igraph

Attaching package: 'igraph'

The following objects are masked from 'package:stats':

    decompose, spectrum

The following object is masked from 'package:base':

    union


 Minimum Cost Spanning Tree 
 Algorithm: Prim 
 Stages:  3 | Time:  0 
 ------------------------------
      ept1     ept2    weight 
         1        2         2
         2        3         1
         3        4         1
 ------------------------------
                   Total = 4 


 Minimum Cost Spanning Tree 
 Algorithm: Kruskal 
 Stages:  3 | Time:  0 
 ------------------------------
      ept1     ept2    weight 
         2        3         1
         3        4         1
         1        2         2
 ------------------------------
                   Total = 4 


 Minimum Cost Spanning Tree 
 Algorithm: Boruvka 
 Stages:  1 | Time:  0 
 ------------------------------
      ept1     ept2    weight 
         1        2         2
         2        3         1
         3        4         1
 ------------------------------
                   Total = 4 

optrees documentation built on May 2, 2019, 8:15 a.m.