Aleph-w 7.10.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]

Classes

struct  Frame
 

Public Member Functions

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

Private Member Functions

GT::Node * put_in_tree (typename GT::Node *gnode, typename GT::Arc *garc, typename GT::Node *tnode)
 
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
 
GT * gptr = nullptr
 
GT * tptr = nullptr
 

Detailed Description

template<AlephGraph GT, ArcFilter< GT > 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, build the tree in depth
  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 the stack of the search + O(V + E) for output tree; the search does not recurse, so that a long path does not overflow the call stack

Usage example:

// ... populate graph ...
Find_Depth_First_Spanning_Tree<decltype(g)> dfs_tree;
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:439
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
__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:4138
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
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 116 of file tpl_spanning_tree.H.

Constructor & Destructor Documentation

◆ Find_Depth_First_Spanning_Tree()

template<AlephGraph GT, ArcFilter< GT > 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 283 of file tpl_spanning_tree.H.

Member Function Documentation

◆ build_tree() [1/2]

◆ build_tree() [2/2]

◆ operator()() [1/2]

template<AlephGraph GT, ArcFilter< GT > 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 303 of file tpl_spanning_tree.H.

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

◆ operator()() [2/2]

template<AlephGraph GT, ArcFilter< GT > 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 332 of file tpl_spanning_tree.H.

References ah_invalid_argument_if, build_tree(), and NODE_COOKIE.

◆ put_in_tree()

Member Data Documentation

◆ gptr

◆ sa

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
SA Aleph::Find_Depth_First_Spanning_Tree< GT, SA >::sa
private

◆ tptr


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