Aleph-w 3.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>

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::Nodefind_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

SNsn
 
SAsa
 

Detailed Description

template<class GT, class SN = Dft_Show_Node<GT>, class 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);
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:561
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:428
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
@ Cycle
Graph has an Eulerian cycle.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Author
Leandro R. León

Definition at line 560 of file eulerian.H.

Constructor & Destructor Documentation

◆ Find_Eulerian_Path()

template<class GT , class SN = Dft_Show_Node<GT>, class 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 754 of file eulerian.H.

Member Function Documentation

◆ find_node_sequence()

◆ find_path()

template<class GT , class SN = Dft_Show_Node<GT>, class 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 822 of file eulerian.H.

◆ find_start_vertex()

template<class GT , class SN = Dft_Show_Node<GT>, class 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 575 of file eulerian.H.

References GraphCommon< GT, Node, Arc >::get_num_arcs(), GraphCommon< GT, Node, Arc >::is_digraph(), Aleph::maps(), 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<class GT , class SN = Dft_Show_Node<GT>, class 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<class GT , class SN = Dft_Show_Node<GT>, class 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<class GT , class SN = Dft_Show_Node<GT>, class 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
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";
}
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
@ None
Graph is not Eulerian.

Definition at line 787 of file eulerian.H.

References 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::maps(), 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<class GT , class SN = Dft_Show_Node<GT>, class SA = Dft_Show_Arc<GT>>
SN& Aleph::Find_Eulerian_Path< GT, SN, SA >::sn
private

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