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

The minimum spanning tree of an unoriented connected graph with weights on the edges (section 14.3). More...

Files

file  ADS/graph/MinimumSpanningTree.h
 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.

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

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...

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).

The definitions, the invariants and the costs of the area are in Theory of graphs.

Function documentation

◆ edges()

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

In pseudocode:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{edges}( G ) \cr 2 & \quad A \gets () \cr 3 & \quad \textbf{foreach}\ u \in V\ \textbf{do} \cr 4 & \qquad \textbf{foreach}\ v \in G.\mathrm{adj}( u )\ \textbf{with}\ u < v\ \textbf{do}\ \text{append } [ u , v ] \text{ of weight } w( u , v ) \text{ to } A \cr 5 & \quad \textbf{return}\ A \end{array}} \]

Precondition
the graph is unoriented

◆ isMinimumSpanningForest()

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 ) \)

In pseudocode (the property of the cycles):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{isMinimumSpanningForest}( n , A , T ) \cr 2 & \quad \textbf{if}\ T \not\subseteq A\ \textbf{or}\ T \text{ has a } \text{cycle }\ \textbf{then return false} \cr 3 & \quad \textbf{if}\ |T| \ne n - \text{ the components of } A \ \textbf{then return false} \cr 4 & \quad \textbf{foreach}\ [ u , v ] \in A\ \textbf{do} \cr 5 & \qquad \textbf{if}\ w( u , v ) < \text{ the heaviest edge of the path } \text{of } T \text{ from } u \text{ to } v\ \textbf{then} \cr 6 & \qquad\quad \textbf{return false} \cr 7 & \quad \textbf{return true} \end{array}} \]

◆ kruskal() [1/3]

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

In pseudocode:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{kruskal}( G ) \cr 2 & \quad \textbf{return}\ \mathrm{kruskal}( n , \mathrm{edges}( G ) ) \end{array}} \]

◆ kruskal() [2/3]

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

In pseudocode (section 14.3): by Theorem 14.1 the lightest edge between two components of the forest belongs to a minimum spanning forest.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{kruskal}( n , A ) \cr 2 & \quad \text{sort } A \text{ by increasing weight}\quad \triangleright\ \text{by Counting Sort if } k > 0 \cr 3 & \quad M \gets \text{the MFSET of } \{ 0 \} , \ldots , \{ n - 1 \};\ T \gets \emptyset \cr 4 & \quad \textbf{foreach}\ [ u , v ] \in A \text{, in that order, until } |T| = n - 1\ \textbf{do} \cr 5 & \qquad \textbf{if}\ \mathrm{find}( M , u ) = \mathrm{find}( M , v ) \ \textbf{then continue}\quad \triangleright \ \text{it would close a cycle} \cr 6 & \qquad \mathrm{merge}( M , u , v );\ T \gets T \cup \{ [ u , v ] \} \cr 7 & \quad \textbf{return}\ T \end{array}} \]

Precondition
\( n \ge 0 \), the ends of the edges in \( [ 0 , n ) \), the weights in \( [ 0 , k ) \) if \( k > 0 \)
Postcondition
a minimum spanning forest (audit)

◆ kruskal() [3/3]

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

It is kruskal( n , A ) above, with merge( u , v ) of sets in place of the test on find() and the merge.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{kruskal}( n , A , \mathit{sets} ) \cr 2 & \quad \text{as } \mathrm{kruskal}( n , A ) \text{ , with } M = \mathit{sets} \end{array}} \]

Precondition
as kruskal(), sets has the components \( \{ 0 \} , \ldots , \{ n - 1 \} \)
Postcondition
a minimum spanning forest (audit)

◆ prim() [1/2]

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

It is prim( G , r , q ) below, with q the priority queue that how names.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{prim}( G , r , \mathit{how} ) \cr 2 & \quad \textbf{return}\ \mathrm{prim}( G , r , \text{an empty priority } \text{queue of kind } \mathit{how} ) \end{array}} \]

Precondition
the graph is unoriented, \( r \) is a node, how is a priority queue
Postcondition
a minimum spanning tree of the component of \( r \) (audit)

◆ prim() [2/2]

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.

The queue is that of PriorityFrontier: a queue without a Priority holds the pairs ( weight , node ). The fall of a key (line 9) is queue_decrease() on the handle of the node if the queue lowers its keys natively, a new insertion of the node otherwise (decrease_key_of() of Capabilities.h), whose stale copies line 6 skips when they come out of \( Q \), since their node is already in the tree; the queue may then end with stale copies in it

In pseudocode (section 14.3): the key of a node out of the tree is the weight of its lightest edge to the tree, and the node of least key joins it.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{prim}( G , r , Q )\quad \triangleright \ \mathit{key}[ v ] \text{ the lightest edge from the tree to } v \cr 2 & \quad ( \mathit{key}[ v ] , p[ v ] , \mathit{in}[ v ] ) \gets ( \infty , \textbf{nil} , \textbf{false} ) \text{ for every node } v; \ \mathit{key}[ r ] \gets 0 \cr 3 & \quad \text{insert every node } v \text{ in } Q \text{ with the priority } \mathit{key}[ v ] \cr 4 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 5 & \qquad u \gets \mathrm{deleteMin}( Q );\ \mathit{in}[ u ] \gets \textbf{true} \cr 6 & \qquad \textbf{if}\ \mathit{key}[ u ] = \infty \ \textbf{then continue}\quad \triangleright \ \text{the root of another tree} \cr 7 & \qquad \textbf{foreach}\ v \in G.\mathrm{adj}( u ) \text{ out of the tree with } w( u , v ) < \mathit{key}[ v ] \ \textbf{do} \cr 8 & \qquad\quad ( \mathit{key}[ v ] , p[ v ] ) \gets ( w( u , v ) , u ); \ \mathrm{decrease}( Q , v , \mathit{key}[ v ] ) \cr 9 & \quad \textbf{return}\ p \end{array}} \]

Precondition
the graph is unoriented, \( r \) is a node, \( q \) is empty
Postcondition
a minimum spanning tree of the component of \( r \) (audit)