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

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

#include <tpl_spanning_tree.H>

Public Member Functions

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

Private Member Functions

void build_tree (GT &g, typename GT::Node *gp, GT &tree)
 

Private Attributes

SA sa
 

Detailed Description

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

Compute a breadth-first spanning tree of a graph.

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

Unlike DFS spanning trees, BFS spanning trees have the property that the path from the root to any node in the tree is the shortest path (in terms of number of edges) in the original graph.

Algorithm:

  1. Start from a source node and add its adjacent arcs to a queue
  2. Process arcs from the queue, adding unvisited nodes to the tree
  3. For each newly added node, enqueue its adjacent arcs
  4. Continue until all reachable nodes are visited

Complexity:

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

Usage example:

// ... populate graph ...
auto start = g.get_first_node();
bfs_tree(g, start, tree);
Compute a breadth-first spanning tree of a graph.
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:428
Node * get_first_node() const
Return any node in the graph.
Definition tpl_graph.H:576
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.
Template Parameters
GTGraph type (must satisfy Aleph graph concept).
SAArc filter type (default: Dft_Show_Arc<GT>).
Exceptions
std::bad_allocIf memory allocation fails for tree or internal queue.
See also
Find_Depth_First_Spanning_Tree For DFS-based spanning tree

Definition at line 314 of file tpl_spanning_tree.H.

Constructor & Destructor Documentation

◆ Find_Breadth_First_Spanning_Tree()

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

Construct a BFS spanning tree builder with optional arc filter.

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

Definition at line 395 of file tpl_spanning_tree.H.

Member Function Documentation

◆ build_tree()

◆ operator()() [1/2]

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

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

Constructs a spanning tree using the first node of the graph as root.

Parameters
[in]gSource graph to compute spanning tree from.
[out]treeOutput graph that will contain the spanning tree.
Returns
Pointer to the tree node corresponding to the root.
Exceptions
std::bad_allocIf memory allocation fails.
std::domain_errorIf the graph is empty.

Definition at line 434 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(), Aleph::maps(), and NODE_COOKIE.

◆ operator()() [2/2]

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

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

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

Parameters
[in]gSource graph to compute spanning tree from.
[in]gnodeStarting node for the BFS traversal.
[out]treeOutput graph that will contain the spanning tree. Will be cleared before construction.
Exceptions
std::bad_allocIf memory allocation fails.
std::invalid_argumentIf gnode is nullptr.
Note
Uses the Spanning_Tree control bit on nodes and arcs.
After execution, nodes and arcs in tree are mapped to their corresponding elements in g via cookies.

Definition at line 414 of file tpl_spanning_tree.H.

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

Member Data Documentation

◆ sa

template<class GT , class SA = Dft_Show_Arc<GT>>
SA Aleph::Find_Breadth_First_Spanning_Tree< GT, SA >::sa
private

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