|
ADS
Algorithms and Data Structures
|
The Eulerian cycles and trails of a graph, oriented or not. More...
Files | |
| file | ADS/graph/EulerianCycle.h |
| 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. | |
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 | |
The Eulerian cycles and trails of a graph, oriented or not.
The definitions, the invariants and the costs of the area are in Theory of graphs.
| 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
In pseudocode (Hierholzer's algorithm, beyond the book): the walk from a node with unused edges always ends where it started, since every node has as many edges entering as leaving, and the walks found along the way are spliced in.
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{eulerianCycle}( G ) \cr 2 & \quad \textbf{if}\ \text{a node has } \mathit{indeg} \ne \mathit{outdeg} \text{ (an odd degree, if unoriented) } \ \textbf{then return}\ \textbf{nil} \cr 3 & \quad \textbf{if}\ m = 0\ \textbf{then return}\ () \cr 4 & \quad C \gets \mathrm{walk}( G , \text{the last node with an edge} )\quad \triangleright\ \text{as } \mathrm{eulerianTrail} \cr 5 & \quad \textbf{if}\ |C| = m + 1\ \textbf{then return}\ C \ \textbf{else return}\ \textbf{nil}\quad \triangleright \ \text{the edges are not connected} \end{array}} \]
| 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
In pseudocode (Hierholzer's algorithm, beyond the book): the start \( s \) is the node with one edge leaving more than entering (with an odd degree, if unoriented, the first of the two), or the last node with an edge if there is none; the walk from a node with unused edges can only stop at the end of the trail, and each node is written when it has no unused edge left, so that the trail comes out backwards.
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{eulerianTrail}( G ) \cr 2 & \quad \textbf{if}\ \text{the degrees allow no trail } \ \textbf{then return}\ \textbf{nil}\quad \triangleright \ \text{more than two unbalanced nodes} \cr 3 & \quad \textbf{if}\ m = 0\ \textbf{then return}\ () \cr 4 & \quad S \gets ( s );\ C \gets () \cr 5 & \quad \textbf{while}\ S \ne \emptyset\ \textbf{do} \cr 6 & \qquad u \gets \mathrm{top}( S ) \cr 7 & \qquad \textbf{if}\ u \text{ has an unused edge } e = ( u , v ) \ \textbf{then} \cr 8 & \qquad\quad \text{mark } e \text{ used (both its arcs, if } \text{unoriented)};\ \mathrm{push}( S , v ) \cr 9 & \qquad \textbf{else} \cr 10 & \qquad\quad \mathrm{pop}( S );\ C \gets ( u ) \cdot C \cr 11 & \quad \textbf{if}\ |C| = m + 1\ \textbf{then return}\ C \ \textbf{else return}\ \textbf{nil} \end{array}} \]
| 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
In pseudocode: the walk of eulerianTrail() from \( s \), if \( s \) has an edge and either no node is unbalanced or \( s \) is the start of the open trail (either node of odd degree, if unoriented).
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{eulerianTrail}( G , s ) \cr 2 & \quad \textbf{if}\ m = 0\ \textbf{then return}\ () \cr 3 & \quad \textbf{if}\ s \text{ can start no trail }\ \textbf{then} \ \textbf{return}\ \textbf{nil} \cr 4 & \quad \textbf{return}\ \text{the walk of lines 4 to 11 of } \mathrm{eulerianTrail}( G ) \text{ from } s \end{array}} \]
| 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
In pseudocode:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{isEulerianCycle}( G , c ) \cr 2 & \quad \textbf{if}\ m > 0\ \textbf{and}\ c_1 \ne c_{|c|} \ \textbf{then return false} \cr 3 & \quad \textbf{return}\ \mathrm{isEulerianTrail}( G , c ) \end{array}} \]
| 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
In pseudocode:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{isEulerianTrail}( G , c ) \cr 2 & \quad \textbf{if}\ m = 0\ \textbf{then return}\ c = () \cr 3 & \quad \textbf{if}\ |c| \ne m + 1\ \textbf{then return false} \cr 4 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ |c| - 1\ \textbf{do} \cr 5 & \qquad \textbf{if}\ ( c_i , c_{i+1} ) \notin E\ \textbf{or} \ \text{it was used before }\ \textbf{then return false} \cr 6 & \qquad \text{mark } ( c_i , c_{i+1} ) \text{ used} \cr 7 & \quad \textbf{return true} \end{array}} \]