/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2009
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
#include<lemon/adaptors.h>
#include<lemon/connectivity.h>
///This file provides an Euler tour iterator and ways to check
///if a digraph is euler.
///Euler iterator for digraphs.
///This iterator converts to the \c Arc type of the digraph and using
///operator ++, it provides an Euler tour of a \e directed
///graph (if there exists).
///if the given digraph is Euler (i.e it has only one nontrivial component
///and the in-degree is equal to the out-degree for all nodes),
///the following code will put the arcs of \c g
///to the vector \c et according to an
/// std::vector<ListDigraph::Arc> et;
/// for(DiEulerIt<ListDigraph> e(g),e!=INVALID;++e)
///If \c g is not Euler then the resulted tour will not be full or closed.
typedef typename Digraph::Node Node;
typedef typename Digraph::NodeIt NodeIt;
typedef typename Digraph::Arc Arc;
typedef typename Digraph::ArcIt ArcIt;
typedef typename Digraph::OutArcIt OutArcIt;
typedef typename Digraph::InArcIt InArcIt;
typename Digraph::template NodeMap<OutArcIt> nedge;
///\param start The starting point of the tour. If it is not given
/// the tour will start from the first node.
DiEulerIt(const Digraph &_g,typename Digraph::Node start=INVALID)
if(start==INVALID) start=NodeIt(g);
for(NodeIt n(g);n!=INVALID;++n) nedge[n]=OutArcIt(g,n);
while(nedge[start]!=INVALID) {
euler.push_back(nedge[start]);
Node next=g.target(nedge[start]);
operator Arc() { return euler.empty()?INVALID:euler.front(); }
bool operator==(Invalid) { return euler.empty(); }
bool operator!=(Invalid) { return !euler.empty(); }
DiEulerIt &operator++() {
Node s=g.target(euler.front());
//This produces a warning.Strange.
//std::list<Arc>::iterator next=euler.begin();
typename std::list<Arc>::iterator next=euler.begin();
while(nedge[s]!=INVALID) {
euler.insert(next,nedge[s]);
Node n=g.target(nedge[s]);
///Postfix incrementation
///\warning This incrementation
///returns an \c Arc, not an \ref DiEulerIt, as one may
///Euler iterator for graphs.
///This iterator converts to the \c Arc (or \c Edge)
///type of the digraph and using
///operator ++, it provides an Euler tour of an undirected
///digraph (if there exists).
///if the given digraph if Euler (i.e it has only one nontrivial component
///and the degree of each node is even),
///the following code will print the arc IDs according to an
/// for(EulerIt<ListGraph> e(g),e!=INVALID;++e) {
/// std::cout << g.id(Edge(e)) << std::eol;
///Although the iterator provides an Euler tour of an graph,
///it still returns Arcs in order to indicate the direction of the tour.
///(But Arc will convert to Edges, of course).
///If \c g is not Euler then the resulted tour will not be full or closed.
typedef typename Digraph::Node Node;
typedef typename Digraph::NodeIt NodeIt;
typedef typename Digraph::Arc Arc;
typedef typename Digraph::Edge Edge;
typedef typename Digraph::ArcIt ArcIt;
typedef typename Digraph::OutArcIt OutArcIt;
typedef typename Digraph::InArcIt InArcIt;
typename Digraph::template NodeMap<OutArcIt> nedge;
typename Digraph::template EdgeMap<bool> visited;
///\param start The starting point of the tour. If it is not given
/// the tour will start from the first node.
EulerIt(const Digraph &_g,typename Digraph::Node start=INVALID)
: g(_g), nedge(g), visited(g,false)
if(start==INVALID) start=NodeIt(g);
for(NodeIt n(g);n!=INVALID;++n) nedge[n]=OutArcIt(g,n);
while(nedge[start]!=INVALID) {
euler.push_back(nedge[start]);
visited[nedge[start]]=true;
Node next=g.target(nedge[start]);
while(nedge[start]!=INVALID && visited[nedge[start]]) ++nedge[start];
operator Arc() const { return euler.empty()?INVALID:euler.front(); }
operator Edge() const { return euler.empty()?INVALID:euler.front(); }
bool operator==(Invalid) const { return euler.empty(); }
bool operator!=(Invalid) const { return !euler.empty(); }
Node s=g.target(euler.front());
typename std::list<Arc>::iterator next=euler.begin();
while(nedge[s]!=INVALID) {
while(nedge[s]!=INVALID && visited[nedge[s]]) ++nedge[s];
if(nedge[s]==INVALID) break;
euler.insert(next,nedge[s]);
Node n=g.target(nedge[s]);
///Postfix incrementation
///\warning This incrementation
///returns an \c Arc, not an \ref EulerIt, as one may
///Checks if the graph is Eulerian
///Checks if the graph is Eulerian. It works for both directed and undirected
///\note By definition, a digraph is called \e Eulerian if
///and only if it is connected and the number of its incoming and outgoing
///arcs are the same for each node.
///Similarly, an undirected graph is called \e Eulerian if
///and only if it is connected and the number of incident arcs is even
///for each node. <em>Therefore, there are digraphs which are not Eulerian,
///but still have an Euler tour</em>.
typename enable_if<UndirectedTagIndicator<Digraph>,bool>::type
eulerian(const Digraph &g)
for(typename Digraph::NodeIt n(g);n!=INVALID;++n)
if(countIncEdges(g,n)%2) return false;
typename disable_if<UndirectedTagIndicator<Digraph>,bool>::type
eulerian(const Digraph &g)
for(typename Digraph::NodeIt n(g);n!=INVALID;++n)
if(countInArcs(g,n)!=countOutArcs(g,n)) return false;
return connected(Undirector<const Digraph>(g));