|
ADS
Algorithms and Data Structures
|
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 ) \) | |
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.
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.