lemon/euler.h
author Peter Kovacs <kpeter@inf.elte.hu>
Wed, 15 Apr 2009 11:41:25 +0200
changeset 638 493533ead9df
parent 633 7c12061bd271
child 639 2ebfdb89ec66
permissions -rw-r--r--
Bug fix in the Euler iterators (#264)
Handle the case when the first node is isolated.
alpar@567
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@567
     2
 *
alpar@567
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@567
     4
 *
alpar@567
     5
 * Copyright (C) 2003-2009
alpar@567
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@567
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@567
     8
 *
alpar@567
     9
 * Permission to use, modify and distribute this software is granted
alpar@567
    10
 * provided that this copyright notice appears in all copies. For
alpar@567
    11
 * precise terms see the accompanying LICENSE file.
alpar@567
    12
 *
alpar@567
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@567
    14
 * express or implied, and with no claim as to its suitability for any
alpar@567
    15
 * purpose.
alpar@567
    16
 *
alpar@567
    17
 */
alpar@567
    18
alpar@567
    19
#ifndef LEMON_EULER_H
alpar@567
    20
#define LEMON_EULER_H
alpar@567
    21
alpar@567
    22
#include<lemon/core.h>
alpar@567
    23
#include<lemon/adaptors.h>
alpar@567
    24
#include<lemon/connectivity.h>
alpar@567
    25
#include <list>
alpar@567
    26
kpeter@633
    27
/// \ingroup graph_properties
alpar@567
    28
/// \file
alpar@567
    29
/// \brief Euler tour
alpar@567
    30
///
alpar@567
    31
///This file provides an Euler tour iterator and ways to check
alpar@567
    32
///if a digraph is euler.
alpar@567
    33
alpar@567
    34
alpar@567
    35
namespace lemon {
alpar@567
    36
alpar@567
    37
  ///Euler iterator for digraphs.
alpar@567
    38
kpeter@633
    39
  /// \ingroup graph_properties
alpar@567
    40
  ///This iterator converts to the \c Arc type of the digraph and using
alpar@567
    41
  ///operator ++, it provides an Euler tour of a \e directed
alpar@567
    42
  ///graph (if there exists).
alpar@567
    43
  ///
alpar@567
    44
  ///For example
alpar@567
    45
  ///if the given digraph is Euler (i.e it has only one nontrivial component
alpar@567
    46
  ///and the in-degree is equal to the out-degree for all nodes),
alpar@567
    47
  ///the following code will put the arcs of \c g
alpar@567
    48
  ///to the vector \c et according to an
alpar@567
    49
  ///Euler tour of \c g.
alpar@567
    50
  ///\code
alpar@567
    51
  ///  std::vector<ListDigraph::Arc> et;
alpar@567
    52
  ///  for(DiEulerIt<ListDigraph> e(g),e!=INVALID;++e)
alpar@567
    53
  ///    et.push_back(e);
alpar@567
    54
  ///\endcode
alpar@567
    55
  ///If \c g is not Euler then the resulted tour will not be full or closed.
alpar@567
    56
  ///\sa EulerIt
kpeter@606
    57
  template<typename GR>
alpar@567
    58
  class DiEulerIt
alpar@567
    59
  {
kpeter@606
    60
    typedef typename GR::Node Node;
kpeter@606
    61
    typedef typename GR::NodeIt NodeIt;
kpeter@606
    62
    typedef typename GR::Arc Arc;
kpeter@606
    63
    typedef typename GR::ArcIt ArcIt;
kpeter@606
    64
    typedef typename GR::OutArcIt OutArcIt;
kpeter@606
    65
    typedef typename GR::InArcIt InArcIt;
alpar@567
    66
kpeter@606
    67
    const GR &g;
kpeter@606
    68
    typename GR::template NodeMap<OutArcIt> nedge;
alpar@567
    69
    std::list<Arc> euler;
alpar@567
    70
alpar@567
    71
  public:
alpar@567
    72
alpar@567
    73
    ///Constructor
alpar@567
    74
kpeter@606
    75
    ///\param gr A digraph.
alpar@567
    76
    ///\param start The starting point of the tour. If it is not given
alpar@567
    77
    ///       the tour will start from the first node.
kpeter@606
    78
    DiEulerIt(const GR &gr, typename GR::Node start = INVALID)
kpeter@606
    79
      : g(gr), nedge(g)
alpar@567
    80
    {
kpeter@638
    81
      if (start==INVALID) {
kpeter@638
    82
        NodeIt n(g);
kpeter@638
    83
        while (n!=INVALID && OutArcIt(g,n)==INVALID) ++n;
kpeter@638
    84
        start=n;
kpeter@638
    85
      }
kpeter@638
    86
      if (start!=INVALID) {
kpeter@638
    87
        for (NodeIt n(g); n!=INVALID; ++n) nedge[n]=OutArcIt(g,n);
kpeter@638
    88
        while (nedge[start]!=INVALID) {
kpeter@638
    89
          euler.push_back(nedge[start]);
kpeter@638
    90
          Node next=g.target(nedge[start]);
kpeter@638
    91
          ++nedge[start];
kpeter@638
    92
          start=next;
kpeter@638
    93
        }
alpar@567
    94
      }
alpar@567
    95
    }
alpar@567
    96
alpar@567
    97
    ///Arc Conversion
alpar@567
    98
    operator Arc() { return euler.empty()?INVALID:euler.front(); }
alpar@567
    99
    bool operator==(Invalid) { return euler.empty(); }
alpar@567
   100
    bool operator!=(Invalid) { return !euler.empty(); }
alpar@567
   101
alpar@567
   102
    ///Next arc of the tour
alpar@567
   103
    DiEulerIt &operator++() {
alpar@567
   104
      Node s=g.target(euler.front());
alpar@567
   105
      euler.pop_front();
alpar@567
   106
      //This produces a warning.Strange.
alpar@567
   107
      //std::list<Arc>::iterator next=euler.begin();
alpar@567
   108
      typename std::list<Arc>::iterator next=euler.begin();
alpar@567
   109
      while(nedge[s]!=INVALID) {
alpar@567
   110
        euler.insert(next,nedge[s]);
alpar@567
   111
        Node n=g.target(nedge[s]);
alpar@567
   112
        ++nedge[s];
alpar@567
   113
        s=n;
alpar@567
   114
      }
alpar@567
   115
      return *this;
alpar@567
   116
    }
alpar@567
   117
    ///Postfix incrementation
alpar@567
   118
alpar@567
   119
    ///\warning This incrementation
alpar@567
   120
    ///returns an \c Arc, not an \ref DiEulerIt, as one may
alpar@567
   121
    ///expect.
alpar@567
   122
    Arc operator++(int)
alpar@567
   123
    {
alpar@567
   124
      Arc e=*this;
alpar@567
   125
      ++(*this);
alpar@567
   126
      return e;
alpar@567
   127
    }
alpar@567
   128
  };
alpar@567
   129
alpar@567
   130
  ///Euler iterator for graphs.
alpar@567
   131
kpeter@633
   132
  /// \ingroup graph_properties
alpar@567
   133
  ///This iterator converts to the \c Arc (or \c Edge)
alpar@567
   134
  ///type of the digraph and using
alpar@567
   135
  ///operator ++, it provides an Euler tour of an undirected
alpar@567
   136
  ///digraph (if there exists).
alpar@567
   137
  ///
alpar@567
   138
  ///For example
alpar@567
   139
  ///if the given digraph if Euler (i.e it has only one nontrivial component
alpar@567
   140
  ///and the degree of each node is even),
alpar@567
   141
  ///the following code will print the arc IDs according to an
alpar@567
   142
  ///Euler tour of \c g.
alpar@567
   143
  ///\code
alpar@567
   144
  ///  for(EulerIt<ListGraph> e(g),e!=INVALID;++e) {
alpar@567
   145
  ///    std::cout << g.id(Edge(e)) << std::eol;
alpar@567
   146
  ///  }
alpar@567
   147
  ///\endcode
alpar@567
   148
  ///Although the iterator provides an Euler tour of an graph,
alpar@567
   149
  ///it still returns Arcs in order to indicate the direction of the tour.
alpar@567
   150
  ///(But Arc will convert to Edges, of course).
alpar@567
   151
  ///
alpar@567
   152
  ///If \c g is not Euler then the resulted tour will not be full or closed.
alpar@567
   153
  ///\sa EulerIt
kpeter@606
   154
  template<typename GR>
alpar@567
   155
  class EulerIt
alpar@567
   156
  {
kpeter@606
   157
    typedef typename GR::Node Node;
kpeter@606
   158
    typedef typename GR::NodeIt NodeIt;
kpeter@606
   159
    typedef typename GR::Arc Arc;
kpeter@606
   160
    typedef typename GR::Edge Edge;
kpeter@606
   161
    typedef typename GR::ArcIt ArcIt;
kpeter@606
   162
    typedef typename GR::OutArcIt OutArcIt;
kpeter@606
   163
    typedef typename GR::InArcIt InArcIt;
alpar@567
   164
kpeter@606
   165
    const GR &g;
kpeter@606
   166
    typename GR::template NodeMap<OutArcIt> nedge;
kpeter@606
   167
    typename GR::template EdgeMap<bool> visited;
alpar@567
   168
    std::list<Arc> euler;
alpar@567
   169
alpar@567
   170
  public:
alpar@567
   171
alpar@567
   172
    ///Constructor
alpar@567
   173
kpeter@606
   174
    ///\param gr An graph.
alpar@567
   175
    ///\param start The starting point of the tour. If it is not given
alpar@567
   176
    ///       the tour will start from the first node.
kpeter@606
   177
    EulerIt(const GR &gr, typename GR::Node start = INVALID)
kpeter@606
   178
      : g(gr), nedge(g), visited(g, false)
alpar@567
   179
    {
kpeter@638
   180
      if (start==INVALID) {
kpeter@638
   181
        NodeIt n(g);
kpeter@638
   182
        while (n!=INVALID && OutArcIt(g,n)==INVALID) ++n;
kpeter@638
   183
        start=n;
kpeter@638
   184
      }
kpeter@638
   185
      if (start!=INVALID) {
kpeter@638
   186
        for (NodeIt n(g); n!=INVALID; ++n) nedge[n]=OutArcIt(g,n);
kpeter@638
   187
        while(nedge[start]!=INVALID) {
kpeter@638
   188
          euler.push_back(nedge[start]);
kpeter@638
   189
          visited[nedge[start]]=true;
kpeter@638
   190
          Node next=g.target(nedge[start]);
kpeter@638
   191
          ++nedge[start];
kpeter@638
   192
          start=next;
kpeter@638
   193
          while(nedge[start]!=INVALID && visited[nedge[start]]) ++nedge[start];
kpeter@638
   194
        }
alpar@567
   195
      }
alpar@567
   196
    }
alpar@567
   197
alpar@567
   198
    ///Arc Conversion
alpar@567
   199
    operator Arc() const { return euler.empty()?INVALID:euler.front(); }
alpar@567
   200
    ///Arc Conversion
alpar@567
   201
    operator Edge() const { return euler.empty()?INVALID:euler.front(); }
alpar@567
   202
    ///\e
alpar@567
   203
    bool operator==(Invalid) const { return euler.empty(); }
alpar@567
   204
    ///\e
alpar@567
   205
    bool operator!=(Invalid) const { return !euler.empty(); }
alpar@567
   206
alpar@567
   207
    ///Next arc of the tour
alpar@567
   208
    EulerIt &operator++() {
alpar@567
   209
      Node s=g.target(euler.front());
alpar@567
   210
      euler.pop_front();
alpar@567
   211
      typename std::list<Arc>::iterator next=euler.begin();
alpar@567
   212
alpar@567
   213
      while(nedge[s]!=INVALID) {
alpar@567
   214
        while(nedge[s]!=INVALID && visited[nedge[s]]) ++nedge[s];
alpar@567
   215
        if(nedge[s]==INVALID) break;
alpar@567
   216
        else {
alpar@567
   217
          euler.insert(next,nedge[s]);
alpar@567
   218
          visited[nedge[s]]=true;
alpar@567
   219
          Node n=g.target(nedge[s]);
alpar@567
   220
          ++nedge[s];
alpar@567
   221
          s=n;
alpar@567
   222
        }
alpar@567
   223
      }
alpar@567
   224
      return *this;
alpar@567
   225
    }
alpar@567
   226
alpar@567
   227
    ///Postfix incrementation
alpar@567
   228
alpar@567
   229
    ///\warning This incrementation
alpar@567
   230
    ///returns an \c Arc, not an \ref EulerIt, as one may
alpar@567
   231
    ///expect.
alpar@567
   232
    Arc operator++(int)
alpar@567
   233
    {
alpar@567
   234
      Arc e=*this;
alpar@567
   235
      ++(*this);
alpar@567
   236
      return e;
alpar@567
   237
    }
alpar@567
   238
  };
alpar@567
   239
alpar@567
   240
alpar@568
   241
  ///Checks if the graph is Eulerian
alpar@567
   242
kpeter@633
   243
  /// \ingroup graph_properties
alpar@568
   244
  ///Checks if the graph is Eulerian. It works for both directed and undirected
alpar@567
   245
  ///graphs.
alpar@568
   246
  ///\note By definition, a digraph is called \e Eulerian if
alpar@567
   247
  ///and only if it is connected and the number of its incoming and outgoing
alpar@567
   248
  ///arcs are the same for each node.
alpar@568
   249
  ///Similarly, an undirected graph is called \e Eulerian if
alpar@567
   250
  ///and only if it is connected and the number of incident arcs is even
alpar@568
   251
  ///for each node. <em>Therefore, there are digraphs which are not Eulerian,
alpar@568
   252
  ///but still have an Euler tour</em>.
kpeter@606
   253
  template<typename GR>
alpar@567
   254
#ifdef DOXYGEN
alpar@567
   255
  bool
alpar@567
   256
#else
kpeter@606
   257
  typename enable_if<UndirectedTagIndicator<GR>,bool>::type
kpeter@606
   258
  eulerian(const GR &g)
alpar@567
   259
  {
kpeter@606
   260
    for(typename GR::NodeIt n(g);n!=INVALID;++n)
alpar@567
   261
      if(countIncEdges(g,n)%2) return false;
alpar@567
   262
    return connected(g);
alpar@567
   263
  }
kpeter@606
   264
  template<class GR>
kpeter@606
   265
  typename disable_if<UndirectedTagIndicator<GR>,bool>::type
alpar@567
   266
#endif
kpeter@606
   267
  eulerian(const GR &g)
alpar@567
   268
  {
kpeter@606
   269
    for(typename GR::NodeIt n(g);n!=INVALID;++n)
alpar@567
   270
      if(countInArcs(g,n)!=countOutArcs(g,n)) return false;
kpeter@606
   271
    return connected(Undirector<const GR>(g));
alpar@567
   272
  }
alpar@567
   273
alpar@567
   274
}
alpar@567
   275
alpar@567
   276
#endif