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

Compute a depth-first spanning tree of a graph. More...

#include <tpl_spanning_tree.H>

Collaboration diagram for Aleph::Find_Depth_First_Spanning_Tree< GT, SA >:
[legend]

Public Member Functions

 Find_Depth_First_Spanning_Tree (SA arc_filter=SA())
 Construct a DFS spanning tree builder with optional arc filter.
 
GT::Nodeoperator() (GT &g, GT &tree)
 Build a depth-first spanning tree starting from the first node.
 
GT::Nodeoperator() (GT &g, typename GT::Node *gnode, GT &tree)
 Build a depth-first spanning tree starting from a specific node.
 

Private Member Functions

bool build_tree (typename GT::Node *gnode, typename GT::Arc *garc, typename GT::Node *tnode)
 
bool build_tree (GT &g, typename GT::Node *gnode, GT &tree)
 

Private Attributes

SA sa
 
GTgptr = nullptr
 
GTtptr = nullptr
 

Detailed Description

template<class GT, class SA = Dft_Show_Arc<GT>>
class Aleph::Find_Depth_First_Spanning_Tree< GT, SA >

Compute a depth-first spanning tree of a graph.

This class performs a depth-first traversal starting from a specified node and constructs the spanning tree according to the DFS visit order.

A spanning tree is a subgraph that includes all vertices of the original graph connected by the minimum number of edges (V-1 edges for V vertices) without forming any cycles.

Algorithm:

  1. Start from a source node and mark it as visited
  2. For each unvisited adjacent node, recursively build the tree
  3. Copy visited nodes and traversed arcs to the output tree
  4. Maintain node/arc mappings between original graph and tree

Complexity:

  • Time: O(V + E) where V is vertices and E is edges
  • Space: O(V) for recursion stack + O(V + E) for output tree

Usage example:

// ... populate graph ...
auto root = dfs_tree(g, tree);
if (root != nullptr)
std::cout << "Spanning tree has " << tree.get_num_nodes() << " nodes\n";
Compute a depth-first spanning tree of a graph.
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:428
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:695
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4060
DynList< T > maps(const C &c, Op op)
Classic map operation.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Note
The algorithm uses the Spanning_Tree control bit on nodes and arcs. These bits are reset at the beginning of the algorithm.
After execution, nodes and arcs in tree are mapped to their corresponding elements in g via cookies.
Template Parameters
GTGraph type (must satisfy Aleph graph concept).
SAArc filter type (default: Dft_Show_Arc<GT>).
See also
Find_Breadth_First_Spanning_Tree For BFS-based spanning tree
graph_to_tree_node() To convert to Tree_Node representation

Definition at line 111 of file tpl_spanning_tree.H.

Constructor & Destructor Documentation

◆ Find_Depth_First_Spanning_Tree()

template<class GT , class SA = Dft_Show_Arc<GT>>
Aleph::Find_Depth_First_Spanning_Tree< GT, SA >::Find_Depth_First_Spanning_Tree ( SA  arc_filter = SA())
inline

Construct a DFS spanning tree builder with optional arc filter.

Parameters
arc_filterArc filter to control which arcs are traversed (default: show all arcs).

Definition at line 209 of file tpl_spanning_tree.H.

Member Function Documentation

◆ build_tree() [1/2]

◆ build_tree() [2/2]

◆ operator()() [1/2]

template<class GT , class SA = Dft_Show_Arc<GT>>
GT::Node * Aleph::Find_Depth_First_Spanning_Tree< GT, SA >::operator() ( GT g,
GT tree 
)
inline

Build a depth-first spanning tree starting from the first node.

Constructs a spanning tree of the graph using depth-first traversal. After execution, tree contains the spanning tree with nodes and arcs mapped to their corresponding elements in g.

Parameters
[in]gSource graph to compute spanning tree from.
[out]treeOutput graph that will contain the spanning tree. Will be cleared before construction.
Returns
Pointer to the root node (first node of g) if the graph is connected and a spanning tree exists; nullptr otherwise.
Exceptions
std::bad_allocIf memory allocation fails.
std::domain_errorIf the graph is empty.
Note
Uses the Spanning_Tree control bit on nodes and arcs.

Definition at line 229 of file tpl_spanning_tree.H.

References ah_domain_error_if, build_tree(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::get_first_node(), GraphCommon< GT, Node, Arc >::get_num_nodes(), and Aleph::maps().

◆ operator()() [2/2]

template<class GT , class SA = Dft_Show_Arc<GT>>
GT::Node * Aleph::Find_Depth_First_Spanning_Tree< GT, SA >::operator() ( GT g,
typename GT::Node gnode,
GT tree 
)
inline

Build a depth-first spanning tree starting from a specific node.

Constructs a spanning tree of the graph using depth-first traversal starting from the specified source node.

Parameters
[in]gSource graph to compute spanning tree from.
[in]gnodeStarting node for the DFS traversal.
[out]treeOutput graph that will contain the spanning tree. Will be cleared before construction.
Returns
Pointer to the tree node corresponding to gnode.
Exceptions
std::bad_allocIf memory allocation fails.
std::invalid_argumentIf gnode is nullptr.
Note
Uses the Spanning_Tree control bit on nodes and arcs.

Definition at line 258 of file tpl_spanning_tree.H.

References ah_invalid_argument_if, build_tree(), Aleph::maps(), and NODE_COOKIE.

Member Data Documentation

◆ gptr

◆ sa

◆ tptr


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