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

Tests whether a graph or digraph is Eulerian. More...

#include <eulerian.H>

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

Public Member Functions

 Test_Eulerian (SN &&__sn=SN(), SA &&__sa=SA())
 Construct an Eulerian tester with optional filters.
 
Eulerian_Type compute (GT &g)
 Compute detailed Eulerian classification.
 
bool operator() (GT &g)
 Test if a graph has an Eulerian cycle.
 
bool has_eulerian_path (GT &g)
 Test if a graph has an Eulerian path.
 
bool test_degree_only (GT &g)
 Check only degree conditions (without connectivity check).
 

Private Member Functions

Eulerian_Type analyze_graph (GT &g)
 Analyze Eulerian properties of an undirected graph.
 
Eulerian_Type analyze_digraph (GT &g)
 Analyze Eulerian properties of a directed graph.
 

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::Test_Eulerian< GT, SN, SA >

Tests whether a graph or digraph is Eulerian.

A graph is Eulerian if it contains an Eulerian cycle (a cycle that visits every edge exactly once). This class tests the necessary and sufficient conditions for Eulerian graphs.

Conditions Tested

Undirected graphs:

  • Eulerian cycle: All vertices have even degree AND graph is connected
  • Eulerian path: Exactly 2 vertices have odd degree AND graph is connected

Directed graphs:

  • Eulerian cycle: in-degree == out-degree for all vertices AND strongly connected
  • Eulerian path: At most one vertex with out-in=1, at most one with in-out=1
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(V + E) including connectivity check
  • Space: O(V) for connectivity check
Example
// Build a graph where all vertices have even degree
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); // Triangle - all degrees are 2
Test_Eulerian<decltype(g)> test;
assert(test(g) == true); // Graph is Eulerian
// Or use detailed result:
assert(test.compute(g) == Eulerian_Type::Cycle);
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
Tests whether a graph or digraph is Eulerian.
Definition eulerian.H:241
@ Cycle
Graph has an Eulerian cycle.
void test()
Definition test-comb.C:40
See also
hamiltonian.H For Hamiltonian path/cycle testing
tpl_test_connectivity.H For connectivity testing
Author
Leandro R. León

Definition at line 240 of file eulerian.H.

Constructor & Destructor Documentation

◆ Test_Eulerian()

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

Construct an Eulerian tester with optional filters.

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

Definition at line 329 of file eulerian.H.

Member Function Documentation

◆ analyze_digraph()

template<AlephGraph GT, NodeFilter< GT > SN = Dft_Show_Node<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Eulerian_Type Aleph::Test_Eulerian< GT, SN, SA >::analyze_digraph ( GT &  g)
inlineprivate

Analyze Eulerian properties of a directed graph.

With every node balanced, or all but a start and an end, the arcs must be together; balanced, they are then strongly connected.

Parameters
gThe directed graph to test
Returns
Eulerian_Type classification

Definition at line 279 of file eulerian.H.

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

Referenced by Aleph::Test_Eulerian< GT, SN, SA >::compute().

◆ analyze_graph()

template<AlephGraph GT, NodeFilter< GT > SN = Dft_Show_Node<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Eulerian_Type Aleph::Test_Eulerian< GT, SN, SA >::analyze_graph ( GT &  g)
inlineprivate

Analyze Eulerian properties of an undirected graph.

The degrees count a loop twice; the arcs must be together.

Parameters
gThe undirected graph to test
Returns
Eulerian_Type classification

Definition at line 253 of file eulerian.H.

References Aleph::and, Aleph::eulerian_detail::arcs_are_together(), Aleph::Cycle, Aleph::eulerian_detail::degree(), GraphCommon< GT, Node, Arc >::is_digraph(), Aleph::Filter_Iterator< Container, It, Show_Item >::next_ne(), Aleph::None, Aleph::Path, Aleph::Test_Eulerian< GT, SN, SA >::sa, and Aleph::Test_Eulerian< GT, SN, SA >::sn.

Referenced by Aleph::Test_Eulerian< GT, SN, SA >::compute().

◆ compute()

template<AlephGraph GT, NodeFilter< GT > SN = Dft_Show_Node<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Eulerian_Type Aleph::Test_Eulerian< GT, SN, SA >::compute ( GT &  g)
inline

Compute detailed Eulerian classification.

Determines whether the graph has an Eulerian cycle, path, or neither. This method checks both degree conditions AND connectivity.

Parameters
gThe graph to test
Returns
Eulerian_Type classification (Cycle, Path, or None)
Example
Test_Eulerian<decltype(g)> test;
auto result = test.compute(g);
if (result == Eulerian_Type::Cycle)
std::cout << "Has Eulerian cycle\n";
Eulerian_Type compute(GT &g)
Compute detailed Eulerian classification.
Definition eulerian.H:352

Definition at line 352 of file eulerian.H.

References Aleph::Test_Eulerian< GT, SN, SA >::analyze_digraph(), Aleph::Test_Eulerian< GT, SN, SA >::analyze_graph(), and GraphCommon< GT, Node, Arc >::is_digraph().

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

◆ has_eulerian_path()

template<AlephGraph GT, NodeFilter< GT > SN = Dft_Show_Node<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
bool Aleph::Test_Eulerian< GT, SN, SA >::has_eulerian_path ( GT &  g)
inline

Test if a graph has an Eulerian path.

An Eulerian path visits every edge exactly once but may not return to the starting vertex.

Parameters
gThe graph to test
Returns
true if the graph contains an Eulerian path (cycle or path)
Note
Returns true for graphs with Eulerian cycles too.

Definition at line 388 of file eulerian.H.

References Aleph::Test_Eulerian< GT, SN, SA >::compute(), Aleph::Cycle, and Aleph::Path.

Referenced by demo_eulerian_type().

◆ operator()()

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

Test if a graph has an Eulerian cycle.

Automatically detects whether the graph is directed or undirected and applies the appropriate test. This method checks both degree conditions AND connectivity.

Parameters
gThe graph to test
Returns
true if the graph contains an Eulerian cycle
Note
For detailed information (cycle vs path vs none), use compute().

Definition at line 372 of file eulerian.H.

References Aleph::Test_Eulerian< GT, SN, SA >::compute(), and Aleph::Cycle.

◆ test_degree_only()

template<AlephGraph GT, NodeFilter< GT > SN = Dft_Show_Node<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
bool Aleph::Test_Eulerian< GT, SN, SA >::test_degree_only ( GT &  g)
inline

Check only degree conditions (without connectivity check).

This is faster but may return true for disconnected graphs that don't actually have Eulerian cycles/paths.

Parameters
gThe graph to test
Returns
true if degree conditions for Eulerian cycle are met
Warning
Does not verify connectivity. Use operator() or compute() for a complete test.

Definition at line 406 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, GraphCommon< GT, Node, Arc >::reset_counter_nodes(), Aleph::Test_Eulerian< GT, SN, SA >::sa, and Aleph::Test_Eulerian< GT, SN, SA >::sn.

Member Data Documentation

◆ sa

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

◆ sn

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

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