Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA > Class Template Reference

Computes the minimum spanning tree of a graph using Kruskal's algorithm. More...

#include <Kruskal.H>

Classes

struct  Init_Node
 Helper struct for initializing node counters during DFS. More...
 
struct  Paint_Filt
 Filter for arcs painted by Kruskal's algorithm. More...
 

Public Member Functions

 Kruskal_Min_Spanning_Tree (Distance __dist=Distance(), SA __sa=SA())
 Constructor.
 
 Kruskal_Min_Spanning_Tree (Distance &__dist, SA __sa=SA())
 Constructor with reference to external distance accessor.
 
bool is_painted () const noexcept
 Returns true if the spanning tree has been painted on the graph.
 
void paint_min_spanning_tree (const GT &g)
 Paints the minimum spanning tree arcs on the graph.
 
void paint_min_spanning_tree (const GT &g, GT &tree)
 Paints the MST on g and copies it to a separate tree graph.
 
void operator() (const GT &g, GT &tree)
 Computes the minimum spanning tree using Kruskal's algorithm.
 
void operator() (const GT &g)
 Paints the minimum spanning tree on the graph using Kruskal's algorithm.
 

Static Private Member Functions

static bool arc_is_in_tree (Fixed_Relation &tree, long i, long j) noexcept
 

Private Attributes

Distance dist_val
 
Distancedist
 
SA sa
 
bool painted
 

Detailed Description

template<class GT, class Distance = Dft_Dist<GT>, class SA = Dft_Show_Arc<GT>>
class Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA >

Computes the minimum spanning tree of a graph using Kruskal's algorithm.

Kruskal's algorithm is recommended for sparse graphs. It works by sorting all arcs by weight and greedily adding arcs that don't form a cycle.

The procedure is parameterized with the following specifications:

  1. GT: The graph type based on List_Graph.
  2. Distance<GT>: The arc weight accessor class that must export the following attributes:
    1. typedef Distance<GT>::Distance_Type: The data type that represents an arc weight.
    2. Distance<GT>::Distance_Type operator()(typename GT::Arc *a): Returns the weight value of arc a.
    3. Distance<GT>::Max_Distance: Static constant representing the maximum distance value that an algorithm would consider as infinity.
    4. typename Distance<GT>::Zero_Distance: Static constant corresponding to the neutral element of addition. Traditionally, in the vast majority of cases, this is zero.
  3. SA: Arc filter for arc iterators.

Time complexity: O(E log E) where E is the number of arcs. Space complexity: O(V) for the Union-Find structure.

See also
Prim_Min_Spanning_Tree

Definition at line 89 of file Kruskal.H.

Constructor & Destructor Documentation

◆ Kruskal_Min_Spanning_Tree() [1/2]

template<class GT , class Distance = Dft_Dist<GT>, class SA = Dft_Show_Arc<GT>>
Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA >::Kruskal_Min_Spanning_Tree ( Distance  __dist = Distance(),
SA  __sa = SA() 
)
inline

Constructor.

Parameters
__distDistance accessor for arc weights.
__saArc filter for arc iterators.

Definition at line 121 of file Kruskal.H.

◆ Kruskal_Min_Spanning_Tree() [2/2]

template<class GT , class Distance = Dft_Dist<GT>, class SA = Dft_Show_Arc<GT>>
Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA >::Kruskal_Min_Spanning_Tree ( Distance __dist,
SA  __sa = SA() 
)
inline

Constructor with reference to external distance accessor.

Definition at line 128 of file Kruskal.H.

Member Function Documentation

◆ arc_is_in_tree()

template<class GT , class Distance = Dft_Dist<GT>, class SA = Dft_Show_Arc<GT>>
static bool Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA >::arc_is_in_tree ( Fixed_Relation tree,
long  i,
long  j 
)
inlinestaticprivatenoexcept

◆ is_painted()

template<class GT , class Distance = Dft_Dist<GT>, class SA = Dft_Show_Arc<GT>>
bool Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA >::is_painted ( ) const
inlinenoexcept

Returns true if the spanning tree has been painted on the graph.

Definition at line 135 of file Kruskal.H.

References Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA >::painted.

◆ operator()() [1/2]

template<class GT , class Distance = Dft_Dist<GT>, class SA = Dft_Show_Arc<GT>>
void Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA >::operator() ( const GT g)
inline

Paints the minimum spanning tree on the graph using Kruskal's algorithm.

After the algorithm completes, the arcs of g that belong to the spanning tree are marked with the Spanning_Tree bit.

Parameters
[in]gThe graph for which to compute the minimum spanning tree.
Exceptions
bad_allocIf there is not enough memory.
domain_errorIf g is a digraph.

Definition at line 256 of file Kruskal.H.

References Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA >::paint_min_spanning_tree().

◆ operator()() [2/2]

template<class GT , class Distance = Dft_Dist<GT>, class SA = Dft_Show_Arc<GT>>
void Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA >::operator() ( const GT g,
GT tree 
)
inline

Computes the minimum spanning tree using Kruskal's algorithm.

Parameters
[in]gThe graph for which to compute the minimum spanning tree.
[out]treeThe graph where the resulting minimum spanning tree will be stored. This graph is cleared before the algorithm starts.
Exceptions
bad_allocIf there is not enough memory to construct the tree. In this case, the value of tree is indeterminate and not clean.
domain_errorIf g is a digraph.

Definition at line 241 of file Kruskal.H.

References Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA >::paint_min_spanning_tree().

◆ paint_min_spanning_tree() [1/2]

template<class GT , class Distance = Dft_Dist<GT>, class SA = Dft_Show_Arc<GT>>
void Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA >::paint_min_spanning_tree ( const GT g)
inline

◆ paint_min_spanning_tree() [2/2]

template<class GT , class Distance = Dft_Dist<GT>, class SA = Dft_Show_Arc<GT>>
void Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA >::paint_min_spanning_tree ( const GT g,
GT tree 
)
inline

Paints the MST on g and copies it to a separate tree graph.

First paints the spanning tree arcs on g, then creates a copy of the MST in the tree parameter.

Parameters
[in]gThe graph for which to compute the minimum spanning tree.
[out]treeThe graph where the resulting minimum spanning tree will be stored. This graph is cleared before the algorithm starts.
Exceptions
bad_allocIf there is not enough memory to construct the tree.
domain_errorIf g is a digraph.

Definition at line 209 of file Kruskal.H.

References Aleph::clear_graph(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), GraphCommon< GT, Node, Arc >::map_arcs(), GraphCommon< GT, Node, Arc >::map_nodes(), Aleph::maps(), Aleph::Filter_Iterator< Container, It, Show_Item >::next_ne(), Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA >::paint_min_spanning_tree(), and Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA >::sa.

Member Data Documentation

◆ dist

template<class GT , class Distance = Dft_Dist<GT>, class SA = Dft_Show_Arc<GT>>
Distance& Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA >::dist
private

◆ dist_val

template<class GT , class Distance = Dft_Dist<GT>, class SA = Dft_Show_Arc<GT>>
Distance Aleph::Kruskal_Min_Spanning_Tree< GT, Distance, SA >::dist_val
private

Definition at line 91 of file Kruskal.H.

◆ painted

◆ sa


The documentation for this class was generated from the following file: