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

Build a mapped subgraph from a graph starting at a given node. More...

#include <tpl_components.H>

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

Classes

struct  Arc_Frame
 
struct  Map_Frame
 

Public Member Functions

 Build_Subgraph (SA arc_filter=SA())
 Construct a subgraph builder with optional arc filter.
 
void operator() (const GT &g, GT &sg, typename GT::Node *g_src)
 Build a mapped subgraph starting from a specific node.
 
GT operator() (const GT &g, typename GT::Node *src)
 Build and return a subgraph containing all nodes reachable from src.
 
void operator() (const GT &g, DynList< typename GT::Node * > &list, typename GT::Node *src)
 Build a list of all nodes reachable from a source node.
 

Private Member Functions

GT::Node * visit (GT &sg, typename GT::Node *p)
 
GT::Node * next_to_visit (GT &sg, Map_Frame &f)
 
GT::Node * visit_through (GT &sg, Map_Frame &f, typename GT::Node *g_tgt)
 
void build_subgraph (GT &sg, typename GT::Node *g_src)
 
template<template< class > class List>
void build_subgraph (List< typename GT::Node * > &l, typename GT::Node *p)
 

Static Private Member Functions

static void copy_arc (GT &sg, typename GT::Arc *arc, typename GT::Node *sg_src, typename GT::Node *sg_tgt)
 

Private Attributes

SA sa
 
const GT * gptr = nullptr
 
size_t count = 0
 

Detailed Description

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
class Aleph::Build_Subgraph< GT, SA >

Build a mapped subgraph from a graph starting at a given node.

This class performs a depth-first traversal of graph g starting from a source node and constructs a mapped copy of all reachable nodes and arcs (the entire graph if connected, or a connected component if not).

The resulting subgraph maintains bidirectional mappings via cookies between the original graph elements and their copies in the subgraph.

Algorithm:

  1. Start from source node and mark it as visited
  2. Create a copy of the node in the subgraph and establish mapping
  3. For each unvisited adjacent arc, copy it and recurse on target node
  4. Continue until all reachable nodes are visited

Complexity:

  • Time: O(V + E) where V is vertices and E is edges in the component
  • Space: O(V) for the stack of the search + O(V + E) for output subgraph; the search does not recurse, so that a long path does not overflow the call stack

Usage example:

// ... populate graph ...
Build_Subgraph<decltype(g)> builder;
builder(g, subgraph, g.get_first_node());
Build a mapped subgraph from a graph starting at a given node.
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:439
Node * get_first_node() const
Return any node in the graph.
Definition tpl_graph.H:590
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
Note
Uses the Build_Subtree control bit on nodes and arcs, which takes a node or an arc for visited: the bits must be clear, as g.reset_nodes() and g.reset_arcs() leave them, except those of the components built before, as in Unconnected_Components.
The cookies that the nodes and arcs of g had are not read: after the build each node and arc reached maps to its copy, and the copy to it.
Template Parameters
GTGraph type (must satisfy Aleph graph concept).
SAArc filter type (default: Dft_Show_Arc<GT>).
See also
Inconnected_Components For computing all connected components
copy_graph() For copying an entire graph

Definition at line 113 of file tpl_components.H.

Constructor & Destructor Documentation

◆ Build_Subgraph()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Aleph::Build_Subgraph< GT, SA >::Build_Subgraph ( SA  arc_filter = SA())
inline

Construct a subgraph builder with optional arc filter.

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

Definition at line 126 of file tpl_components.H.

Member Function Documentation

◆ build_subgraph() [1/2]

◆ build_subgraph() [2/2]

◆ copy_arc()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
static void Aleph::Build_Subgraph< GT, SA >::copy_arc ( GT &  sg,
typename GT::Arc *  arc,
typename GT::Node *  sg_src,
typename GT::Node *  sg_tgt 
)
inlinestaticprivate

◆ next_to_visit()

◆ operator()() [1/3]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
void Aleph::Build_Subgraph< GT, SA >::operator() ( const GT &  g,
DynList< typename GT::Node * > &  list,
typename GT::Node *  src 
)
inline

Build a list of all nodes reachable from a source node.

Performs DFS from src and collects all reachable nodes into list.

Parameters
[in]gSource graph to traverse.
[out]listOutput list to store reachable node pointers.
[in]srcStarting node for the traversal.
Exceptions
std::bad_allocIf memory allocation fails.
std::invalid_argumentIf src is nullptr.
Note
Uses the Build_Subtree control bit on nodes and arcs.

Definition at line 400 of file tpl_components.H.

References ah_invalid_argument_if, Aleph::Build_Subgraph< GT, SA >::count, and Aleph::Build_Subgraph< GT, SA >::gptr.

◆ operator()() [2/3]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
void Aleph::Build_Subgraph< GT, SA >::operator() ( const GT &  g,
GT &  sg,
typename GT::Node *  g_src 
)
inline

Build a mapped subgraph starting from a specific node.

Performs a DFS traversal from g_src and constructs a mapped copy of all reachable nodes and arcs in sg.

Parameters
[in]gSource graph to traverse.
[out]sgOutput subgraph (should be empty; will contain mapped copy).
[in]g_srcStarting node for the traversal.
Exceptions
std::bad_allocIf memory allocation fails.
std::invalid_argumentIf g_src is nullptr.
Note
Uses the Build_Subtree control bit on nodes and arcs.
After execution, nodes/arcs in sg are mapped to g via cookies.

Definition at line 355 of file tpl_components.H.

References ah_invalid_argument_if, Aleph::Build_Subgraph< GT, SA >::build_subgraph(), Aleph::Build_Subgraph< GT, SA >::count, and Aleph::Build_Subgraph< GT, SA >::gptr.

◆ operator()() [3/3]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
GT Aleph::Build_Subgraph< GT, SA >::operator() ( const GT &  g,
typename GT::Node *  src 
)
inline

Build and return a subgraph containing all nodes reachable from src.

Parameters
[in]gSource graph to traverse.
[in]srcStarting node for the traversal.
Returns
A new graph containing mapped copies of all reachable nodes/arcs.
Exceptions
std::bad_allocIf memory allocation fails.
std::invalid_argumentIf src is nullptr.

Definition at line 375 of file tpl_components.H.

References ah_invalid_argument_if, Aleph::Build_Subgraph< GT, SA >::build_subgraph(), Aleph::Build_Subgraph< GT, SA >::count, and Aleph::Build_Subgraph< GT, SA >::gptr.

◆ visit()

◆ visit_through()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
GT::Node * Aleph::Build_Subgraph< GT, SA >::visit_through ( GT &  sg,
Map_Frame &  f,
typename GT::Node *  g_tgt 
)
inlineprivate

Member Data Documentation

◆ count

◆ gptr

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
const GT* Aleph::Build_Subgraph< GT, SA >::gptr = nullptr
private

◆ sa

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

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