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