ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
EulerianCycle.h File Reference

The Eulerian cycles and trails of a graph, oriented or not: a trail is a path that uses each edge exactly once, a cycle a closed trail. More...

#include "ADS/Contract.h"
#include "ADS/graph/Graph.h"
#include <algorithm>
#include <cstddef>
#include <optional>
#include <utility>
#include <vector>
#include "ADS/graph/impl/EulerianCycle.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

template<Graph G>
std::optional< std::vector< int > > ADS::eulerianCycle (const G &g)
 the nodes of an Eulerian cycle of the graph, the first node repeated at the end, or nothing if there is none; empty with no edge
template<Graph G>
std::optional< std::vector< int > > ADS::eulerianTrail (const G &g)
 the nodes of an Eulerian trail of the graph, closed (the first node repeated at the end) if there is an Eulerian cycle and open otherwise, or nothing if there is none; empty with no edge
template<Graph G>
std::optional< std::vector< int > > ADS::eulerianTrail (const G &g, int s)
 the Eulerian trail of eulerianTrail() above from the node \( s \), or nothing if no Eulerian trail starts from it; empty with no edge
template<Graph G>
bool ADS::isEulerianCycle (const G &g, const std::vector< int > &c)
 whether the nodes \( c \), the first repeated at the end, are a closed path that uses each edge of \( g \) exactly once; empty if g has no edge
template<Graph G>
bool ADS::isEulerianTrail (const G &g, const std::vector< int > &c)
 whether the nodes \( c \) are a path, open or closed, that uses each edge of \( g \) exactly once, an unoriented edge in either direction; empty if g has no edge

Detailed description

The Eulerian cycles and trails of a graph, oriented or not: a trail is a path that uses each edge exactly once, a cycle a closed trail.

  • eulerianCycle(), the closed trail (exercise 15.3, for an oriented graph): by the theorem of Euler it exists if and only if each node has as many edges entering as leaving (an even degree, if the graph is not oriented) and all the edges are in one component, the orientation ignored. The book finds it by a local search on the partitions of the edges into closed paths, each edge choosing its successor and a swap of two successors merging two paths; here the algorithm of Hierholzer follows unused edges from a node until it comes back, and splices in the closed paths found from the nodes along the way, in \( O( n + m ) \) time;
  • eulerianTrail(), the trail, closed if there is a cycle and open otherwise: an open trail exists if and only if the edges are in one component and exactly one node s has one edge leaving more than entering and one node t one edge entering more than leaving, the others balanced (exactly two nodes of odd degree, if the graph is not oriented), and it goes from s to t; the same walk of Hierholzer, from s, finds it. A node may also be given as the start;
  • isEulerianCycle() and isEulerianTrail(), which check a sequence of nodes.

The connection of the edges is not tested apart: under the conditions on the degrees the walk of Hierholzer uses every edge reachable from its start, which is every edge if and only if they are in one component, so that a walk shorter than m edges tells that there is no trail. An unoriented edge is used in one direction, the two arcs of the adjacency paired in their order, so that parallel edges are distinct edges. The theory is in Eulerian trails and other properties.

The contracts [see Contract.h]: a start given is a node; an unoriented graph has no loop; at the level of the audits, the trail found uses each edge exactly once, which isEulerianTrail() checks. The functions are defined in EulerianCycle.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, exercise 15.3, https://algoritmicamente.it/materiali/libro/capitoli/cap15.pdf
C. Hierholzer, C. Wiener, Ueber die Moeglichkeit, einen Linienzug ohne Wiederholung und ohne Unterbrechung zu umfahren, Mathematische Annalen 6(1), 30-32, 1873, doi:10.1007/BF01442866
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa