ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MinimumSpanningTree.h File Reference

The minimum spanning tree of an unoriented connected graph with weights on the edges (section 14.3): a tree on all the nodes, of \( n - 1 \) edges, of least total weight. More...

#include "ADS/Capabilities.h"
#include "ADS/Contract.h"
#include "ADS/graph/Graph.h"
#include "ADS/graph/ShortestPaths.h"
#include "ADS/mfset/MFSet.h"
#include "ADS/priority/FibonacciHeapPriorityQueue.h"
#include "ADS/priority/HeapPriorityQueue.h"
#include "ADS/priority/ListPriorityQueue.h"
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <memory>
#include <tuple>
#include <type_traits>
#include <vector>
#include "ADS/graph/impl/MinimumSpanningTree.tpp"

Go to the source code of this file.

Classes

struct  ADS::WeightedEdge< W >
 an edge \( [ u , v ] \) of weight \( w \) More...
struct  ADS::MergeCounts
 what a run of kruskal() did: the merges tried and those that joined two components More...

Namespaces

namespace  ADS

Concepts

concept  ADS::Mergeable
 a union of disjoint sets as kruskal() needs it: merge( u , v ) joins the components of u and v and tells whether they were apart

Functions

template<Graph G>
std::vector< WeightedEdge< typename G::Weight > > ADS::edges (const G &g)
 the edges of an unoriented graph, each once, from its smaller end
template<class W>
std::vector< WeightedEdge< W > > ADS::kruskal (int n, std::vector< WeightedEdge< W > > A, long k=0)
 the edges of a minimum spanning forest of the nodes \( [ 0 , n ) \); with \( k > 0 \) the weights are integers in \( [ 0 , k ) \), sorted by counting
template<class W, Mergeable M>
std::vector< WeightedEdge< W > > ADS::kruskal (int n, std::vector< WeightedEdge< W > > A, M &sets, long k=0, MergeCounts *c=nullptr)
 the minimum spanning forest of kruskal() above, with the MFSET sets given, whose merge( u , v ) joins the components of u and v and tells whether they were apart [see MFSet.h]; its work in *c, if given
template<Graph G>
auto ADS::kruskal (const G &g, long k=0)
 the minimum spanning forest of kruskal() above, on the edges of an unoriented graph
template<Graph G>
std::vector< int > ADS::prim (const G &g, int r, Frontier how=Frontier::HEAP)
 the parents of the tree from \( r \), NIL for \( r \) and the nodes out of its component; how is LIST, HEAP or FIBONACCI
template<Graph G, FrontierQueue Q>
std::vector< int > ADS::prim (const G &g, int r, Q &q, FrontierCounts *c=nullptr)
 the tree of prim() above, on the priority queue \( q \) given [see PriorityQueue.h], whose priority is the weight or the pair ( weight , node ), which breaks the ties by the names of the nodes; its work in *c, if given.
template<class W>
bool ADS::isMinimumSpanningForest (int n, const std::vector< WeightedEdge< W > > &A, const std::vector< WeightedEdge< W > > &T)
 whether \( T \) is a minimum spanning forest of the edges \( A \) on the nodes \( [ 0 , n ) \): its edges are in A, it has no cycle, as many edges as n minus the components of A, and no edge of A weighs less than the heaviest edge of the path of T between its ends, \( O( m n ) \)

Detailed description

The minimum spanning tree of an unoriented connected graph with weights on the edges (section 14.3): a tree on all the nodes, of \( n - 1 \) edges, of least total weight.

Both algorithms are greedy, and correct by the Theorem 14.1: if A is part of a minimum spanning tree and no edge of A crosses the cut ( S , V - S ), then the lightest edge across it can be added to A.

  • kruskal() scans the edges by increasing weight and keeps one if its ends are in two components of the forest, an MFSET with the rank and the compression of the paths: \( O( m \log n ) \) for the sort and almost \( O( m ) \) for the rest; it stops after n - 1 edges. With integer weights in [ 0 , k ) the edges are sorted by Counting Sort, \( O( m + n + k ) \) (exercise 14.2).
  • prim() grows the tree from a root r, adding the lightest edge that leaves it, the nodes in a priority queue by the weight of their lightest edge to the tree: \( O( m \log n ) \) with a binary heap, \( O( n^2 ) \) with an unsorted list, better on dense graphs, and \( O( m + n \log n ) \) with a Fibonacci heap (exercise 14.4).

A graph that is not connected gives a minimum spanning forest by kruskal(), and the tree of the component of r by prim(). Both take their structure as well: kruskal() any MFSET with merge() [see Mergeable], prim() any priority queue of section 10.1, whose priority may be the pair ( weight , node ) so that every queue gives the same tree with the same counts [see FrontierCounts, MergeCounts].

The contracts [see Contract.h]: the graph is unoriented; the edges given to kruskal() join nodes in [ 0 , n ), with weights in [ 0 , k ) when k > 0; prim() starts from a node, with a priority queue as frontier. At the level of the audits the result is a minimum spanning forest, which isMinimumSpanningForest() checks by the property of the cycles: a spanning forest is minimum if and only if no edge out of it weighs less than an edge of the path of the forest between its ends. The functions are defined in MinimumSpanningTree.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 14.3, https://algoritmicamente.it/materiali/libro/capitoli/cap14.pdf
J. B. Kruskal, On the shortest spanning subtree of a graph and the traveling salesman problem, Proceedings of the American Mathematical Society 7(1), 48-50, 1956, doi:10.1090/S0002-9939-1956-0078686-7
R. C. Prim, Shortest connection networks and some generalizations, Bell System Technical Journal 36(6), 1389-1401, 1957, doi:10.1002/j.1538-7305.1957.tb01515.x
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa