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

Finds and constructs an Eulerian path or cycle using Hierholzer's algorithm. More...

#include <eulerian.H>

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

Classes

struct  Result
 Result type: path and its classification. More...
 

Public Member Functions

 Find_Eulerian_Path (SN &&__sn=SN(), SA &&__sa=SA())
 Construct an Eulerian path finder with optional filters.
 
Result operator() (GT &g)
 Find an Eulerian path or cycle in the graph.
 
DynList< typename GT::Arc * > find_path (GT &g)
 Get only the arc list (convenience method).
 
DynList< typename GT::Node * > find_node_sequence (GT &g)
 Get the node sequence of the Eulerian path/cycle.
 

Private Member Functions

GT::Node * find_start_vertex (GT &g, Eulerian_Type type)
 Find starting vertex for Eulerian path/cycle.
 
DynList< typename GT::Arc * > hierholzer_undirected (GT &g, typename GT::Node *start)
 Hierholzer's algorithm for undirected graphs.
 
DynList< typename GT::Arc * > hierholzer_directed (GT &g, typename GT::Node *start)
 Hierholzer's algorithm for directed graphs.
 

Private Attributes

Kept_Filter< SN > sn
 
Kept_Filter< SA > sa
 

Detailed Description

template<AlephGraph GT, NodeFilter< GT > SN = Dft_Show_Node<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
class Aleph::Find_Eulerian_Path< GT, SN, SA >

Finds and constructs an Eulerian path or cycle using Hierholzer's algorithm.

Hierholzer's algorithm efficiently constructs an Eulerian path or cycle in O(E) time. It works by:

  1. Starting at an appropriate vertex (odd-degree for path, any for cycle)
  2. Following edges until returning to start or getting stuck
  3. Splicing in sub-tours from vertices with unused edges
Template Parameters
GTGraph type (List_Graph, Array_Graph, etc.)
SNNode filter for iteration (default: show all nodes)
SAArc filter for iteration (default: show all arcs)

Complexity

  • Time: O(E)
  • Space: O(E) for storing the path
Example
// Build triangle
auto n1 = g.insert_node();
auto n2 = g.insert_node();
auto n3 = g.insert_node();
g.insert_arc(n1, n2);
g.insert_arc(n2, n3);
g.insert_arc(n3, n1);
Find_Eulerian_Path<decltype(g)> finder;
auto [path, type] = finder(g);
for (auto arc : path)
// Process arc...
Finds and constructs an Eulerian path or cycle using Hierholzer's algorithm.
Definition eulerian.H:477
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:439
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
@ Cycle
Graph has an Eulerian cycle.
Author
Leandro R. León

Definition at line 476 of file eulerian.H.

Constructor & Destructor Documentation

◆ Find_Eulerian_Path()

template<AlephGraph GT, NodeFilter< GT > SN = Dft_Show_Node<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Aleph::Find_Eulerian_Path< GT, SN, SA >::Find_Eulerian_Path ( SN &&  __sn = SN(),
SA &&  __sa = SA() 
)
inline

Construct an Eulerian path finder with optional filters.

Parameters
__snNode filter (default: show all nodes)
__saArc filter (default: show all arcs)

Definition at line 666 of file eulerian.H.

Member Function Documentation

◆ find_node_sequence()

template<AlephGraph GT, NodeFilter< GT > SN = Dft_Show_Node<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
DynList< typename GT::Node * > Aleph::Find_Eulerian_Path< GT, SN, SA >::find_node_sequence ( GT &  g)
inline

◆ find_path()

template<AlephGraph GT, NodeFilter< GT > SN = Dft_Show_Node<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
DynList< typename GT::Arc * > Aleph::Find_Eulerian_Path< GT, SN, SA >::find_path ( GT &  g)
inline

Get only the arc list (convenience method).

Parameters
gThe graph to search
Returns
List of arcs in Eulerian order, empty if none exists

Definition at line 733 of file eulerian.H.

◆ find_start_vertex()

template<AlephGraph GT, NodeFilter< GT > SN = Dft_Show_Node<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
GT::Node * Aleph::Find_Eulerian_Path< GT, SN, SA >::find_start_vertex ( GT &  g,
Eulerian_Type  type 
)
inlineprivate

Find starting vertex for Eulerian path/cycle.

For cycles: any vertex with edges For paths: a vertex with odd degree (undirected) or out-in=1 (directed)

Parameters
gThe graph
typeThe Eulerian type (Cycle or Path)
Returns
Starting vertex, or nullptr if graph has no edges

Definition at line 491 of file eulerian.H.

References Aleph::eulerian_detail::degree(), GraphCommon< GT, Node, Arc >::is_digraph(), Aleph::Filter_Iterator< Container, It, Show_Item >::next_ne(), NODE_COUNTER, Aleph::Path, GraphCommon< GT, Node, Arc >::reset_counter_nodes(), Aleph::Find_Eulerian_Path< GT, SN, SA >::sa, and Aleph::Find_Eulerian_Path< GT, SN, SA >::sn.

Referenced by Aleph::Find_Eulerian_Path< GT, SN, SA >::find_node_sequence(), and Aleph::Find_Eulerian_Path< GT, SN, SA >::operator()().

◆ hierholzer_directed()

template<AlephGraph GT, NodeFilter< GT > SN = Dft_Show_Node<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
DynList< typename GT::Arc * > Aleph::Find_Eulerian_Path< GT, SN, SA >::hierholzer_directed ( GT &  g,
typename GT::Node *  start 
)
inlineprivate

◆ hierholzer_undirected()

template<AlephGraph GT, NodeFilter< GT > SN = Dft_Show_Node<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
DynList< typename GT::Arc * > Aleph::Find_Eulerian_Path< GT, SN, SA >::hierholzer_undirected ( GT &  g,
typename GT::Node *  start 
)
inlineprivate

◆ operator()()

template<AlephGraph GT, NodeFilter< GT > SN = Dft_Show_Node<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Result Aleph::Find_Eulerian_Path< GT, SN, SA >::operator() ( GT &  g)
inline

Find an Eulerian path or cycle in the graph.

Parameters
gThe graph to search
Returns
Result containing the path (if exists) and its type. Without arcs, the type is Cycle and the path is empty.
Example
auto result = finder(g);
if (result.type != Eulerian_Type::None)
{
cout << "Found " << (result.type == Eulerian_Type::Cycle ? "cycle" : "path")
<< " with " << result.path.size() << " edges\n";
}
@ None
Graph is not Eulerian.

Definition at line 700 of file eulerian.H.

References Aleph::Test_Eulerian< GT, SN, SA >::compute(), Aleph::Find_Eulerian_Path< GT, SN, SA >::find_start_vertex(), Aleph::Find_Eulerian_Path< GT, SN, SA >::hierholzer_directed(), Aleph::Find_Eulerian_Path< GT, SN, SA >::hierholzer_undirected(), GraphCommon< GT, Node, Arc >::is_digraph(), Aleph::None, Aleph::Find_Eulerian_Path< GT, SN, SA >::Result::path, Aleph::Find_Eulerian_Path< GT, SN, SA >::sa, Aleph::Find_Eulerian_Path< GT, SN, SA >::sn, and Aleph::Find_Eulerian_Path< GT, SN, SA >::Result::type.

Member Data Documentation

◆ sa

◆ sn

template<AlephGraph GT, NodeFilter< GT > SN = Dft_Show_Node<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Kept_Filter<SN> Aleph::Find_Eulerian_Path< GT, SN, SA >::sn
private

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